1. The situation when to use the consensus algorithm
The problems that these distributed consensus algorithms want to solve.
For example, there is a membership system . There are 5 servers (S1, S2, S3, S4, S5) to store the member data.
Now I want to recharge $100 for my account .
I asked my father and my mother each to recharge $50 for my account.
S1 received my father's request and S2 received my mother's request.
My initial balance is zero.
Each server has their own data copy.
This problem may happen if without applying a consensus algorithm:
S1 and S2 both store 50 in my balance. I gave out $100 and I only get $50, what the fuck!
2. The raft consensus algorithm
Assume S1 is the leader of the current term.
S1 received the request to add 50 to my balance. This kind of request is reached only when a successful payment is reached.
S2 received the request to add 50 to my balance.
All servers can process clients requests, because a lot of client requests did not require a consensus algorithm. Such as I request to see how much my balance is . And for those requests that require a consensus algorithm, only certain operations need a consensus algorithm, not all operations.
S1 want to add 50 to my balance, and this operation need consistency, so S1 will append a entry such as add 50 to my balance because S1 is the leader of the current term.
S2 want to add 50 to my balance and S2 find S1 is the leader so S1 will redirect the request to S1.