Try
↑ ↓ move↵ openesc close
In this lesson · topic 5 of 7
  1. 1. Agreeing when nodes fail
  2. 2. Elections, terms and the log
  3. 3. Crash the leader and cut the cluster
  4. 4. The leader nobody follows
  5. 5. Walking a follower's log back to the leader's
  6. 6. Adding two nodes without ever having two majorities
  7. 7. Check votes, logs and quorums

Topic 5

Walking a follower's log back to the leader's

7 min read

How a log gets wrong

A follower’s log can differ from its leader’s after a run of crashes. A leader appends entries, dies before replicating them, and a later leader appends different entries at the same indexes. A follower that held the first leader’s entries now holds a suffix that no one else has.

The leader’s log wins. The follower is repaired by the protocol’s own messages.

A worked repair

Write each log as the term of its entries by index. The new leader has ten entries:

Index 1 2 3 4 5 6 7 8 9 10
Leader 1 1 1 4 4 5 5 6 6 6
Follower 1 1 1 4 4 4 4

The shapes are like the ones in Figure 7 of the Raft paper. The follower agrees up to index 5, then holds two term-4 entries that the leader never had.

A new leader starts by guessing that every follower is up to date. Its nextIndex for this follower is 11, one past its own last entry. Each AppendEntries carries the index and term of the entry just before the new ones, called prevLogIndex and prevLogTerm.

  1. nextIndex 11. The message says prev is index 10, term 6. The follower has only 7 entries and rejects it.
  2. nextIndex 10. Prev is index 9. Rejected, no entry there.
  3. nextIndex 9. Prev is index 8. Rejected.
  4. nextIndex 8. Prev is index 7, term 5. The follower has term 4 there. Rejected.
  5. nextIndex 7. Prev is index 6, term 5. The follower has term 4. Rejected.
  6. nextIndex 6. Prev is index 5, term 4. The follower has term 4 at 5. Accepted.

On acceptance the follower deletes everything after index 5, which removes its two term-4 entries, and appends the leader’s entries 6 through 10. Five rejections and one success. The logs are now identical.

Predict first

In the repair above, the follower's entry at index 7 has term 4 and the leader's has term 5. Why can the follower not simply keep its entry and append the rest?

Why the follower’s extra entries are safe to delete

Those two term-4 entries were never committed. If they had been, a majority would hold them, and the election restriction would have stopped a leader without them from winning. Deleting only uncommitted entries is the other half of the safety argument.

Walking back one index per round trip is slow for a long gap. The paper mentions an optimisation: the follower’s rejection includes the term of the conflicting entry and the first index it holds for that term, so the leader can skip a whole term per round trip. The paper doubts it is needed, since failures are rare and logs rarely diverge by much.

Why an old-term entry is not counted

The commit rule from the previous topic comes from a specific failure. Suppose S1 is leader in term 2 and replicates index 2 to S2 only. S1 crashes. S5 wins term 3 with votes from S3, S4 and itself, and appends its own entry at index 2, then crashes. S1 returns, wins term 4 and keeps replicating its term-2 entry. When it reaches S3, the entry is on three of five servers, a majority.

If S1 treated that as committed and then crashed, S5 could win term 5. S5’s last entry is from term 3 and S1, S2 and S3 end in term 2, so S5’s log counts as more up to date. It would overwrite the term-2 entry that a majority held. So Raft refuses to count it. Once S1 commits a term-4 entry after it, log matching drags the term-2 entry along, and S5 can no longer win.

Your progress is saved in this browser. Sign in to keep it on every device. Privacy · Terms