Home > Article > Technology peripherals > A consensus algorithm that distributed systems must know: Raft
Raft algorithm
is the first choice for distributed system development Consensus algorithm
. For example, Etcd and Consul are popular now.
If you master this algorithm, you can easily handle the fault tolerance and of most scenarios. #Consistency
Requirements. For example, distributed configuration systems, distributed NoSQL storage, etc. can easily break through the single-machine limitations of the system. The Raft algorithm achieves consensus on a series of values and consistency of logs of each node through all methods based on the leader.
2. Raft Role
, silently receive messages from the leader. When the leader’s heartbeat message times out, they will take the initiative to stand up and recommend themselves as candidates. Candidate (Candidate)
:
will request voting RPC messages from other nodes to notify other nodes to vote if they win the majority Vote and be promoted to leader. Leader
:
, everything is subject to me. Process write requests, manage log replication, and continuously send heartbeat information to inform other nodes that "I am the leader, I am still alive, and you don't want to" initiate a new election without having to find a new leader to replace me. As shown in the figure below, three types of figures are used to represent followers, candidates and leaders. Role
In a multi-node cluster, how does the Raft algorithm ensure that there is only one leader in the cluster at the same time under abnormal circumstances such as node failure or partition error? Let’s begin to explain the process of leader election by the Raft algorithm.
In the initial state, all nodes in the cluster are followers status.
As shown in the figure below, there are three nodes (Node) a, b, and c, and the term (Term) is 0.
Initial state
Raft algorithm implements the characteristics of random timeout, each time The timeout interval for each node to wait for heartbeat information from the leader node is random. For example, the waiting timeout interval of node A is 150 ms, that of node B is 200 ms, and that of node C is 300 ms. Then a times out first. First, it times out because it does not wait for the leader's heartbeat information. As shown in the figure below, the timeout timers for the three nodes start running.
Timeout time
When the timeout time of node A expires, node A becomes a candidate and increases its term number. The Term value is updated from 0 to 1. And cast a vote for myself.
Become a candidate
Let’s see how a candidate can become a leader of.
Leader election
The English word is term, and leaders have terms of office.
Assuming a cluster consists of N nodes, then the majority is at least N/2 1. For example: for a cluster of 3 nodes, most are 2.
In order to prevent multiple nodes from initiating voting at the same time, each node will be assigned a random election timeout. During this time, the node cannot become a candidate and can only wait until timeout. For example, in the above example, node A times out first and becomes a candidate first. With this clever design, in most cases only one server node initiates the election first instead of initiating the election at the same time, which reduces the number of election failures due to vote division.
Become a candidate
If the leader node fails, it will Trigger a new round of elections. As shown in the figure below, if leader node A fails, node B and node C will re-elect the leader.
Leader Failure
The Raft algorithm uses the following methods to conduct leadership elections, ensuring that there is only one leader in one term, greatly reducing the chance of election failure. Condition.
This article uses animated graphics to explain how the Raft algorithm elects leaders, making it easier to understand and digest.
The above is the detailed content of A consensus algorithm that distributed systems must know: Raft. For more information, please follow other related articles on the PHP Chinese website!