Log Replication: AppendEntries Basics
You'll learn how Raft leaders use AppendEntries RPCs to replicate log entries to followers and track which entries have been safely replicated.
The Core Idea
Once a leader is elected in Raft, its primary job is to accept client commands and replicate them across the cluster. It does this by sending AppendEntries RPCs to all followers. Think of the leader as a teacher distributing homework sheets to students—it needs to ensure everyone has the same copy and track who's caught up.
How AppendEntries Works
The leader maintains a log of commands. When it receives a new client request, it:
- Appends the command to its own log
- Sends
AppendEntriesRPCs to all followers in parallel - Waits for a majority to acknowledge receipt
- Once a majority replicates the entry, it's considered committed
Each AppendEntries message contains:
- The leader's current term
- Entries to replicate (can be empty for heartbeats)
- Information about the preceding log entry (for consistency checking)
- The leader's commit index
Tracking Replication Progress
The leader maintains two key pieces of state for each follower:
- nextIndex: The next log entry to send to that follower
- matchIndex: The highest log entry known to be replicated on that follower
When a follower successfully replicates entries, the leader updates these indices. If a follower falls behind or rejects an entry (due to log inconsistency), the leader decrements nextIndex and retries with earlier entries, gradually finding where the logs match.
Key Takeaway: Leaders use AppendEntries RPCs to replicate log entries to followers, tracking each follower's progress with nextIndex and matchIndex to ensure safe, consistent replication across the cluster.