What is distributed consistency?
Let's first look at an example:
We have a single node, which can be a database or a server, and when client sends data to node, the X node receives data and logs it.
This shows that for a single node, consistency is easy to implement.
However, for multiple nodes, we can achieve consistency, which is the problem of distributed consistency.
Raft is a protocol that implements distributed consistency
Let's take a look at how it works.
Node Introduction:
Each node has three different state
(1) Follower State
(2) leader State
(3) Candidate State
Leader election
All nodes start with the follower state, and if a node of follower status is not controlled by a leader, it may become a candidate. When a node becomes a candidate, it collects votes from other nodes, and the other node sends the ballot to the candidate after receiving a signal from the candidate. If a candidate gets a majority of the votes, it will become a leader. This process is the leader election.
There is a time limit for the entire electoral process, such as:
Splite vote is because if there are two candidates to invite tickets, then through a similar overtime to solve, two candidates in a timeout, such as 300ms to each other after the wait, because the two sides get the same number of votes, half to half, then after 300ms, Then the two candidates issued an invitation, at the same time the probability is greatly reduced, then the candidate who first issued the invitation received the majority of the consent to become leader leader, and the other candidate later issued an invitation to vote, those follower voters have voted to the first candidate, can not vote for it, It became the loser, and finally the loser became a regular follower.
Log Replication
Once a leader is chosen, all changes in a system consisting of these nodes will pass through this leader and be in charge of the whole world.
Once the leader receives the message from the client, it logs each request action on the log, and the value of the other node does not change until the log record is committed.
The leader first copies the log entry set 5 to the follower node, and when most of the follower nodes have appended the log record to the log of this node, the leader begins to execute the log, which is to write 5 to the node. Then in the next heartbeat, the leader notifies followers that the log entry has been submitted and follows writes 5. The entire cluster then maintains the consistency of the data. This process is called log replication.
Repeat the process for each new log record.
If a network partition or network communication failure occurs in this process, so that leader cannot access most follwers, then leader can only update the follower servers it can access, and most of the servers follower because there is no leader , they re-elect a candidate as a leader, and then this leader as a representative of the outside world, if the outside request to add a new log, the new leader will follow the above steps to notify most followers, if the network failure to repair, Then the original leader become follower, in the phase of the loss of the old leader any update can not be counted as a commit, roll back, accept the new leader new updates.
Distributed consistency algorithm raft