Sub topic 6
Log Matching and Repairing Divergent Followers
This chapter assumes you know Raft's roles and terms, how RequestVote elects a leader, and how AppendEntries carries log entries from leader to follower. It also assumes you can read short Python. Here you will see how Raft keeps follower logs consistent with the leader's log, and how it repairs a follower whose log has drifted.
The Log Matching property
Raft guarantees two facts about logs on different servers:
- If two entries in different logs have the same index and the same term, they store the same command.
- If two entries in different logs have the same index and the same term, all entries before them are identical too.
The first fact holds because a leader creates at most one entry per index in a given term, and it never changes or moves an entry once written. The second fact holds because of a check made on every AppendEntries call. Together they let a server compare one (index, term) pair instead of comparing whole logs.
The consistency check
Each AppendEntries request names the entry that immediately precedes the new ones, using prev_log_index and prev_log_term. The follower accepts the request only if its own log has an entry at prev_log_index with term prev_log_term. Otherwise it replies False.
This works like induction. An empty log matches trivially, which is why the code keeps a sentinel entry at index 0 with term 0. Each accepted append extends a log that already matches the leader's up to prev_log_index, so the match still holds after the append.
How logs diverge
When a leader crashes, it may have written entries that only some servers received. A new leader then writes entries of its own. Followers can end up with missing entries, extra entries from an old term, or both.
Suppose the new leader, in term 6, has this log (each number is the term of the entry at that index):
index: 1 2 3 4 5 6
leader: 1 1 1 4 4 6
F-a: 1 1 1 4 4 6 (matches)
F-b: 1 1 1 4 (missing entries)
F-c: 1 1 1 2 2 3 (extra, conflicting entries)The leader never edits a follower's log directly. It forces the follower to agree by sending entries, and the follower fixes itself.
Repairing a follower
When a leader takes office, it sets next_index for every follower to its own last log index plus one. It then sends AppendEntries with that prev_log_index. If a follower replies False, the leader decrements that follower's next_index and retries. The retries stop at the latest index where the logs agree. From there the follower's code deletes any conflicting suffix and appends the leader's entries.
Look again at the follower-side handler from the plan:
def handle_append_entries(s, req):
# s.log[0] is a sentinel entry with term 0, so real entries start at index 1.
if req.term < s.current_term:
return (s.current_term, False)
if req.term > s.current_term:
s.current_term = req.term
s.voted_for = None
s.role = "follower"
s.reset_election_timer()
# Consistency check: our log must contain the entry just before the new ones.
if req.prev_log_index >= len(s.log) or s.log[req.prev_log_index].term != req.prev_log_term:
s.persist()
return (s.current_term, False)
index = req.prev_log_index
for entry in req.entries:
index += 1
if index < len(s.log) and s.log[index].term != entry.term:
del s.log[index:] # drop the conflicting entry and everything after it
if index >= len(s.log):
s.log.append(entry)
if req.leader_commit > s.commit_index:
s.commit_index = min(req.leader_commit, index)
s.persist()
return (s.current_term, True)Three details matter:
- The follower deletes entries only when an existing entry has a different term from the incoming one. If the terms match, the entries are identical by Log Matching, so it keeps them.
- The deletion removes the conflicting entry and everything after it, because later entries cannot be trusted once one entry differs.
- A stale or reordered request must not shorten a log that already matches. Because deletion happens only on a real term conflict, a delayed duplicate of an old request is harmless.
Common errors
- Truncating the log on every AppendEntries, even when entries already match. A delayed old request can then erase entries that were already acknowledged.
- Forgetting the sentinel entry, which makes
prev_log_index = 0fail the check on an empty log. - Using
req.leader_commitdirectly as the new commit index. The follower may not yet hold that many entries, so it takes the smaller ofleader_commitand the index of the last new entry. - Letting a leader delete or overwrite entries in its own log. A leader only appends.
Exercise
A follower's log has terms [0, 1, 1, 2, 2] at indexes 0 to 4 (index 0 is the sentinel). The leader sends AppendEntries with prevlogindex=2, prevlogterm=1 and entries [(term 3), (term 3)]. What is the follower's log afterward, and what does it reply?
Show answer
The check passes, because index 2 has term 1. The first new entry lands at index 3, where the follower has term 2, which conflicts with term 3. The follower deletes indexes 3 and 4 and appends both new entries. Its log becomes terms [0, 1, 1, 3, 3], and it replies success (True).