Handling Log Inconsistencies
What you'll learn: How Raft leaders detect and repair follower logs that have diverged, ensuring all replicas eventually hold identical command sequences.
The Problem
In Raft's leader-based replication, followers can fall behind or receive conflicting entries due to crashes, network partitions, or leadership changes. A follower's log might be missing entries, have extra uncommitted entries from old leaders, or both. The leader must repair these inconsistencies to maintain the log consistency guarantee.
Detection and Repair Process
When a leader sends an AppendEntries RPC, it includes:
- The index of the entry immediately before the new ones
- The term of that entry
The follower checks: "Do I have an entry at this index with this term?" This is the consistency check.
If the check fails, the follower rejects the RPC. The leader then decrements the index it's trying and retries, walking backward through the log until it finds the matching point—the latest entry both leader and follower agree on.
The Repair
Once the matching point is found, the leader:
- Overwrites all follower entries after that point
- Appends all entries from the leader's log
Think of it like two people comparing grocery lists. They start from the end, backing up until they agree ("Yes, we both wrote 'milk' as item #3"), then one person erases everything after that and copies the rest from the authoritative list.
This process leverages the matching property: if two logs have an entry with the same index and term, all preceding entries are identical. The leader's log is always authoritative because it won only by having at least a majority's worth of entries.
Key Takeaway: Leaders heal log inconsistencies by finding the latest matching entry through trial-and-error backtracking, then forcing followers to overwrite divergent entries with the leader's authoritative log tail.