This set practises running Raft’s two RPCs by hand on concrete logs. Logs are written as the list of their entries’ terms, index 1 first.
Problem 1
Problem 6.1 (Persistent or volatile).
- For each of
currentTerm,votedFor,log[],commitIndex,lastApplied,nextIndex[],matchIndex[], say whether the Raft paper keeps it on stable storage.- For each persistent variable, describe in one or two sentences an execution that goes wrong if an implementation keeps it only in memory.
- Explain how a restarted follower and a newly elected leader each find out the correct
commitIndexagain.- Why must a persistent variable be written to stable storage before the reply is sent, not after?
Solution
- Persistent:
currentTerm,votedFor,log[]. Volatile on all servers:commitIndex,lastApplied. Volatile on the leader, reset after each election:nextIndex[],matchIndex[].currentTerm: a server in term 5 restarts in term 0, accepts the requests of an old term-2 leader and may vote in term 3 a second time; several such servers can elect a second leader of an old term (lecture 08.01).votedFor: a server votes for B in term 4, crashes, restarts with no vote and votes for D in term 4; B and D can both win (Example 5.11).log[]: a follower acknowledges entry 7, the leader commits it with that follower as the third of five replicas and answers the client; the follower restarts with an empty log, so the entry is now on two servers, and a leader elected by the other three can overwrite it.- A follower relearns it from
leaderCommitin the next AppendEntries, capped at the last entry that request verified. A new leader commits an entry of its own term (the no-op entry of the Raft paper’s §8); by the commit rule this commits everything before it, so itscommitIndexcatches up.- The reply is a promise that someone else acts on: a vote is counted, an acknowledgement is counted toward commitment. If the server crashes after replying but before writing, the promise is broken although the other side has already acted on it.
Problem 2
Problem 6.2 (Who can win an election?).Five servers have these logs (terms of the entries):
- S1: 1, 1, 2, 2
- S2: 1, 1, 2
- S3: 1, 1, 1, 1, 1
- S4: 1, 1, 3
- S5: 1, 1, 2, 2, 2
Assume every server is in the same term and has not voted, and the candidate starts the next term.
- S1 becomes a candidate. Which servers grant their vote? Can S1 win?
- Answer the same for S2, S3, S4 and S5.
- A student says “the server with the longest log becomes leader”. Use your answers to refute it.
- Suppose S1 and S5 both become candidates in the same term, and S2’s request from S1 arrives first. Can S5 still win? Explain with the immutability of votes.
Solution
The last (term, index) pairs are S1 (2, 4), S2 (2, 3), S3 (1, 5), S4 (3, 3), S5 (2, 5). By Definition 6.3, a voter grants if the candidate’s last term is larger, or equal with an index at least as large.
- S2 grants (equal term 2, 4 at least 3), S3 grants (2 > 1), S4 refuses (2 < 3), S5 refuses (equal term, 4 < 5). With its own vote S1 has S1, S2, S3: three of five, so it can win.
- S2: only S3 grants (S1 and S5 have equal last term and longer logs; S4 has term 3). Two votes, cannot win. S3: nobody else grants, since every other log ends in term 2 or 3. Cannot win. S4: everyone grants (3 is the largest last term). Can win. S5: S1, S2, S3 grant, S4 refuses. Can win.
- S3 has a longest log (5 entries) but can never win, because its last term 1 is the oldest. Conversely S4, with only 3 entries, can win with every vote. The ordering compares last terms first.
- Yes, but it needs three votes from S5, S1, S3, S4 and S2. S2 voted for S1 and cannot change its vote in this term, S4 refuses S5, and S1 voted for itself. So S5 can reach at most S5 and S3: two votes. S5 cannot win this term even though its log is more up-to-date than S1’s. Whether S1 wins depends on S3 (S1 needs S1, S2, S3). This is lecture 08.02’s point: the leader need not have the best log.
Problem 3
Problem 6.3 (Running the AppendEntries receiver).A follower F is in term 3 with log 1, 1, 2, 2, 2 and commitIndex 1. It receives the following AppendEntries requests in this order (entries are written by their terms). For each, give F’s reply (term, success, matchedUpTo), its log and its commitIndex afterwards.
- term 3, prevLogIndex 3, prevLogTerm 3, entries none, leaderCommit 2.
- term 3, prevLogIndex 2, prevLogTerm 1, entries 3, 3, leaderCommit 2.
- term 3, prevLogIndex 2, prevLogTerm 1, entries 3, leaderCommit 2 (an older, delayed copy).
- term 2, prevLogIndex 4, prevLogTerm 2, entries 2, leaderCommit 5.
- term 3, prevLogIndex 4, prevLogTerm 3, entries 3, leaderCommit 5.
Finally: an implementation deletes everything after prevLogIndex on every successful request, conflict or not. Show what goes wrong in request 3.
Solution
- Check: index 3 has term 2, not 3: fail. Reply (3, false, 0). Log and commitIndex unchanged: 1, 1, 2, 2, 2 and 1.
- Check at index 2: term 1, pass. Index 3: term 2 against 3, conflict, delete indices 3 to 5, append; index 4 is now beyond the end, append. Log 1, 1, 3, 3. lastNew is 4, so commitIndex becomes max(1, min(2, 4)) = 2. Reply (3, true, 4).
- Check at index 2 passes. Index 3 already has term 3: nothing to do. No deletion, so index 4 stays. Log 1, 1, 3, 3. lastNew is 3; commitIndex stays max(2, min(2, 3)) = 2. Reply (3, true, 3). The leader keeps matchIndex at max(4, 3) = 4.
- Term 2 < 3: reject without looking at the log. Reply (3, false, 0); the old leader learns about term 3 and steps down.
- Check at index 4: term 3, pass. Index 5 is beyond the end: append. Log 1, 1, 3, 3, 3. lastNew 5, commitIndex = min(5, 5) = 5. Reply (3, true, 5).
With “delete after prevLogIndex”, request 3 would cut the log to 1, 1, 3, removing index 4, which F had already acknowledged in request 2. The leader has matchIndex 4 for F and may count F as a replica of index 4 when committing it, although F no longer holds it. The committed entry could then sit on fewer than a majority of servers, and a later leader without it could be elected and overwrite it. This is why step 2 deletes only on a conflict.
Problem 4
Problem 6.4 (Counting the walk-back).A leader of term 8 has log 1, 1, 1, 4, 4, 5, 5, 6, 6, 6 (this is the leader of the Raft paper’s Figure 7) and was just elected, so nextIndex is 11 for everyone.
- Follower X has log 1, 1, 1, 4. How many AppendEntries fail before one succeeds? What does X’s log look like afterwards?
- Follower Y has log 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3. Same questions; which entries of Y are deleted?
- Now use this optimization: on failure, if the follower has no entry at prevLogIndex it replies conflictIndex = its last index + 1; otherwise it replies conflictIndex = the first index of its log whose term equals the term of its entry at prevLogIndex. The leader sets nextIndex to conflictIndex. Redo parts 1 and 2.
Solution
- Requests check prevLogIndex 10, 9, 8, 7, 6, 5: X has no such entry, six failures. At prevLogIndex 4, both logs have term 4: success, carrying entries 5 to 10. X’s log becomes the leader’s log.
- Leader terms by index: 1, 1, 1, 4, 4, 5, 5, 6, 6, 6. Y’s: 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3. The checks at 10, 9, 8 (3 against 6), 7 (3 against 5), 6 (2 against 5), 5 and 4 (2 against 4) fail: seven failures. At 3 both have term 1: success with entries 4 to 10. Index 4 conflicts (2 against 4), so Y deletes indices 4 to 11 (all its term-2 and term-3 entries, eight entries) and appends the leader’s seven. None of the deleted entries was committed: by Leader Completeness (Module 7) the leader of term 8 would hold every committed entry.
- X: the first request (prevLogIndex 10) fails with conflictIndex 5 (X’s last index is 4). The next request has prevLogIndex 4 and succeeds: one failure. Y: the first request fails at index 10, where Y has term 3; the first index of term 3 in Y is 7, so nextIndex becomes 7. The next request checks index 6: Y has term 2 there against the leader’s 5, fail; the first index of term 2 in Y is 4, so nextIndex becomes 4. The next request checks index 3: success. Two failures instead of seven: one round trip per conflicting term instead of one per entry.
Problem 5
Problem 6.5 (Randomized timeouts).Use the model of Proposition 6.4, but let each election timer be uniform on
with width .
- For
servers, prove directly, by computing an area in the square , that the probability that the two timers fire more than apart is . - State the formula for general
(you may use the proof of Proposition 6.4 with replaced by ) and compute it for , ms and ms, and for with the same values. - Explain the trade-off in choosing
: what improves and what gets worse when grows? - The model assumes equally up-to-date logs. Give an example with 5 servers in which the round is clean but the first candidate still loses, and say what happens next.
Solution
- Shift both timers by
, so is uniform on the square of area . The bad event is a band around the diagonal. Its complement consists of two right triangles, and , each with legs of length , so total area . The probability is . , with the same proof as in Proposition 6.4. For : , so on average at most about rounds. For : , at most about rounds. Larger clusters see more contested rounds. - A wider interval spreads the timers out, so
shrinks and clean rounds become more likely. But the expected time until the first timer fires, about , and the worst-case wait grow, so each round takes longer and the cluster stays without a leader longer after a crash. The Raft paper’s choice (150 to 300 ms, so ms) balances the two for message delays of a few milliseconds. - Let A have log 1, 1 and B, C, D, E have 1, 1, 2. A times out first by a wide margin and starts a new term. A’s request reaches everyone, all adopt the new term, but B, C, D, E refuse because their last term 2 is larger than A’s 1. A has one vote and loses. Since the others did not grant a vote they did not restart their timers; one of them times out next, starts a higher term, and wins (A grants, as do the others with equal logs). The election takes an extra round, which is why the model assumes equally up-to-date logs.