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 150ms to 300ms).
  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 50ms) 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. If the majority of matchIndex[i] >= commitIndex + x with positive x and log[commitIndex + x].term == currentTerm, set commitIndex = commitIndex + x.

There are some additional rules.

RequestVote

AppendEntries

Write request

  1. A client sends a write request to the leader.
  2. The leader turns the request into a log entry (index + term number + key-value pair), appending the log entry to its WAL file on disk.
  3. The leader sends out an AppendEntries to each follower.
    1. Every follower that receives an AppendEntries checks if its log has an entry matching the previous entry in the AppendEntries.
    2. If the check succeeds, the follower adds the next entry to its log and replies with an acknowledgement.
    3. Otherwise, the follower fails the AppendEntries and the leader initiates the log repair process.
      1. The leader repeats the loop of decrementing the log previous entry and retrying the AppendEntries RPC, until the previous entry finally matches the follower's last log entry.
      2. The leader forcefully synchronizes all subsequent log entries to the follower via an AppendEntries.
  4. If the leader doesn't receive acknowledgements from a majority of the followers, it fails the client's write request. Otherwise, it continues with the below operations.
  5. The leader marks the log entry as committed, updating its commit index, and applies the log entry to its BoltDB (single-instance file-based key-value store).
  6. The leader updates its key-value cache and key-disk-location index.
  7. The leader replies to the client with a success message.
  8. When the leader sends out a new AppendEntries to each follower.
    1. Every follower that receives an AppendEntries uses the leader commit index to commit and apply the entry and then replies with an acknowledgement.

Due to the frequent writes to the log, etcd's performance heavily relies on the disk I/O speed so SSDs are often the top choice for it.

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 commits and applies E1.
  4. The leader replies to the client.
  5. Before sending out another heartbeat to inform the followers to commit and apply E1, the leader encounters a shutdown.
  6. 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).
  7. The new leader appends a no-op log entry E2 to its log.
  8. The new leader sends out a heartbeat.
  9. The new leader commits and applies both E1 and E1.
  10. The new leader sends out another heartbeat to inform the followers to commit and apply E1 and E2, and this should fix the mismatch.

Write request scaling

Read request

  1. A client sends a read request to the leader.
  2. The leader ensures its leader position via the leader lease.
    • The leader periodically issues a lease for itself via its heartbeat.
    • The lease stays valid for a short time (150 ms) and during the lease period, the leader is guaranteed to be the current leader.
  3. It checks the key-value cache. If the key is in the cache, it replies to the client.
  4. Otherwise, it checks the key-disk-location index. If the key is in the index, it reads the value from the BoltDB and replies to the client. It also updates the key-value cache.
  5. Otherwise, it searches in the BoltDB. If the key is in the BoltDB, it reads the value from the BoltDB, and replies to the client. It also updates the key-value cache and key-disk-location index.
  6. Otherwise, it fails the client's read request.

Read request scaling

Appendix

Leader election

  1. Voting
    1. The candidate votes for itself for the current term number (each node has one vote for a given term number), and sends out the RequestVote message to others to ask for their votes.
      1. Every RequestVote receiver checks if the requesting candidate's term number is larger than the receiver's current term number. If the requesting candidate's term number is greater, the receiver updates its term number and steps down to become a follower.
      2. The receiver checks if it has not yet voted in its current term.
      3. The receiver checks that whether the requesting candidate's WAL is at least as up-to-date as the receiver's WAL by comparing the last log index and term number.
      4. If all three checks pass, the receiver grants the vote and resets its election timer. Otherwise, it rejects the RequestVote with its current term number.
  2. Winning
    1. If a candidate receives votes from a majority of the nodes (ceil((node count + 1) / 2) or floor(node count / 2) + 1), it becomes the new leader. If two candidates get the same votes, neither of them satisfies the majority vote rule so no leader is elected. Every node watches its election timer and starts another round of leader election after the timer expires (the timer is randomized to avoid indefinite staleness).
  3. Heartbeat
    1. The new leader starts sending out its heartbeat constantly.

An AppendEntries contains:

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 reverts 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.

References

←Previous Next→