Course contentsShow
← System Design
Lesson 629 of 1,91917. Consensus AlgorithmsFree lesson

Log Inconsistencies and Repair

How leader election can leave logs inconsistent, and how the leader repairs follower logs by finding the divergence point.

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:

  1. Find the divergence point: The leader maintains a nextIndex for each follower (initially set to the leader's last log index + 1). When an AppendEntries fails, it decrements nextIndex and retries.

  2. Consistency check: Each AppendEntries includes the prevLogIndex and prevLogTerm of the entry immediately before the new ones. The follower rejects if its log doesn't match at that position.

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