Spreading the Word
Once a leader is elected, its job is to replicate its log to all followers. The log is an ordered, append-only sequence of commands. Every node in the cluster maintains its own copy of this log, and Raft's job is to ensure all copies are identical.
The Replication Flow
- Client request: A client sends a command (e.g.,
SET x=5) to the Leader. If a client accidentally contacts a Follower, the Follower redirects it to the Leader. - Append to local log: The Leader appends this command as a new log entry with the current term number. The entry is now in the log but not yet committed.
- Replicate in parallel: The Leader sends
AppendEntriesRPCs containing the new entry to all Followers simultaneously. - Followers acknowledge: Each Follower appends the entry to its own log and sends a success acknowledgment back to the Leader. Before appending, the Follower performs a consistency check: it verifies that the entry immediately preceding the new one in the leader's message matches its own log. This check propagates backward, ensuring logs are always consistent.
- Commit: Once the Leader receives acknowledgments from a quorum of nodes (including itself), it marks the entry as committed. A committed entry is guaranteed to be durable — it will survive any future leader changes.
- Apply and respond: The Leader applies the command to its state machine (e.g., sets
x = 5in the database), returns the result to the client, and notifies Followers in the nextAppendEntriesmessage that the entry is committed. Followers then apply it to their own state machines.
Log Consistency After Leader Changes
When a new leader is elected, some followers may have missing or extra uncommitted entries from the previous leader's tenure. Raft handles this by having the new leader overwrite followers' logs to match its own. The leader finds the point where its log and the follower's log diverge and sends all entries from that point forward. This is safe because only committed entries (which required a quorum) are ever applied to state machines, and the voting rules guarantee the new leader always has all committed entries.