Log Inconsistencies and Repair
You'll learn: How Raft leaders discover and fix mismatched logs after leader elections to maintain consistency.
Why Logs Become Inconsistent
When a leader fails mid-replication, some followers may have received entries while others didn't. When a new leader is elected, follower logs can contain:
- Missing entries (lagging behind the new leader)
- Extra uncommitted entries (from a failed leader that never committed them)
- Conflicting entries (different commands at the same log index)
Think of it like a group project where the team lead suddenly disappears. Some teammates received version 3 of the document, others still have version 2, and one person has an alternate version 3 with different edits. The new team lead must reconcile everyone's copies.
How the Leader Repairs Logs
The leader uses AppendEntries RPCs as both a probe and a repair mechanism:
-
Find the divergence point: The leader maintains a
nextIndexfor each follower (initially set to the leader's last log index + 1). When an AppendEntries fails, it decrementsnextIndexand retries. -
Consistency check: Each AppendEntries includes the
prevLogIndexandprevLogTermof the entry immediately before the new ones. The follower rejects if its log doesn't match at that position. -
Overwrite conflicts: Once the divergence point is found, the leader sends all missing entries from that point forward. The follower deletes conflicting entries and appends the leader's version.
This ensures followers converge to the leader's log, which by the Leader Completeness Property contains all committed entries.
Key Takeaway:
Raft leaders repair inconsistent follower logs by stepping backward through AppendEntries rejections to find where logs diverge, then overwriting everything from that point with the leader's authoritative version.