Raft

Raft is a consensus algorithm that ensures data consistency among a cluster of nodes. It counts on a leader-follower protocol to enforce the nodes to agree on a single, ordered sequence of data manipulation commands, known as the Write-Ahead Log (WAL). If all nodes have the same WAL, we know that they have the same state of data.

Node role

There are 3 roles and each node can only be one of them at any time.

Node state

Every node has the following states.

The leader has the following extra states which are reinitialized right after its conversion to the leader.

Operation path

flowchart TB follower -- election timeout --> candidate candidate -- discover leader or higher term --> follower candidate -- restart election after election timeout --> candidate candidate -- run election --> others others -- response --> candidate candidate -- win election --> leader leader -- sync logs --> others others -- response --> leader leader -- discover higher term --> follower client -- request --> leader leader -- response --> client
  1. A node starts with the follower role and an election timer (randomized between 150 ms and 300 ms).
  2. If the node grants its vote to a RequestVote or receives an AppendEntries, it restarts the election timer. Otherwise, when the election timer expires, it assumes the leader is gone and converts its role from follower to candidate.
  3. The new candidate node performs the following steps.
    1. Increment the currentTerm.
    2. Vote for itself.
    3. Restart the election timer.
    4. Send RequestVote to others.
      • If the node receives votes from the majority of nodes, it becomes the leader.
      • If the node receives an AppendEntries with term T >= currentTerm, it converts to follower.
      • If the election timer expires, it starts a new election.
  4. The new leader performs the following tasks.
    • Send AppendEntries as heartbeat to others periodically (every 50 ms) based on the expiration of its leader heartbeat timer.
    • Handle a client request by performing the following steps.
      1. Append an entry to its log.
      2. Send AppendEntries to others on next heartbeat.
        • For nodes with success, increase their matchIndex and nextIndex.
        • For nodes with failure, decrease their nextIndex and retry.
      3. Conduct majority check.
        1. If the majority of matchIndex[i] >= commitIndex + x with positive x and log[commitIndex + x].term == currentTerm, set commitIndex = commitIndex + x.
        2. If commitIndex > lastApplied, increment lastApplied and apply the command of log[lastApplied].
      4. Reply to the client based on majority check result.

There are some additional rules.

RequestVote

AppendEntries

Uncommitted log entry by leader shutdown scenario

  1. The leader appends a new entry E1 to its log.
  2. The leader sends out a heartbeat so every node has the uncommitted E1.
  3. The leader receives acknowledgements from the majority of nodes.
  4. The leader commits and applies E1.
  5. The leader replies to the client.
  6. Before sending out another heartbeat to inform the followers to commit and apply E1, the leader encounters a shutdown.
  7. A follower becomes the new leader. At this point, there is a conflict between the client's understanding of the data state (E1 is applied) and the actual data state (E1 is not applied).
  8. The new leader appends a no-op log entry E2 to its log.
  9. The new leader sends out a heartbeat.
  10. The leader receives acknowledgements from the majority of nodes.
  11. The new leader commits and applies both E1 and E2.
  12. The new leader sends out another heartbeat to inform the followers to commit and apply E1 and E2, and this should fix the mismatch.

Disconnection and reconnection

If a follower disconnects from the cluster and no longer receives the leader heartbeat, it converts to a candidate, incrementing its term number, and resets the election timer. Because it is isolated from the others, it cannot get enough votes to become a leader. The election timer eventually expires and it restarts the leader election process. This election failure loop can make this isolated node having a large term number.

When the node reconnects to the cluster, if its term number is larger than the cluster leader's, it rejects the AppendEntries from the leader with its own term number, forcing the leader to update its own term number and revert to the follower state.

The cluster becomes leaderless and waits for a node to start its leader election. Note that the reconnected node may have outdated logs and lose its upcoming leader election because of it.

Write request scaling

Read request scaling

Operation simplification

References

←Previous Next→