Goal

State the five safety properties of Raft (paper Figure 3) with quantifiers, prove each one from the handlers of Module 6, and explain with paper Figure 8 why the commit rule must count only entries of the leader’s current term.

Module 6 gave the mechanisms: RequestVote with the up-to-date test, AppendEntries with the consistency check, and the commit rule. This module asks the question the course exams care about: why do these few rules guarantee that no two servers ever apply different commands at the same log index, whatever the crashes, delays and partitions? The answer is a chain of five invariants. Each link is short, but the links are easy to mix up, and one condition of the commit rule (the entry must be from the current term) is the hinge on which the whole chain turns.

The proofs follow §5.3 and §5.4 of the extended Raft paper (1), in particular the safety argument of §5.4.3, written out with every step, and Ongaro’s thesis (2) for the formal version. Lecture 08.03 lists the five properties and asks the class to work out, for each, which rule guarantees it; lecture 08.04 returns to Leader Completeness and corrects a claim made in 08.02. Lecture 08.03 also walks through paper Figure 7. Kleppmann’s notes cover the same algorithm in §6.2 (3) with different variable names (the translation table is in Module 6). Election Safety was proved in Module 5; we use it here as a black box.

The five properties, stated formally

The question: what exactly does Raft promise? Lecture 08.03 puts it this way: if you wrote Raft as a TLA+ specification, these are the invariants the model checker would check in every reachable state.

We fix one execution of Raft with servers and a fixed configuration. Time is global and only used for reasoning (no server can read it). For a server and time we write for the log of at time , and for its entry at index , a pair (term, command). Two entries are equal when they have the same term and the same command. Since a term has at most one leader (Theorem 5.10), we write for the leader of term when it exists.

Raft’s notion of commitment was introduced in Definition 6.1 and made operational by the commit rule (Definition 6.8). For proofs we need a precise version that refers to the execution, not to what some server believes.

Definition 7.1 (Directly committed, committed in a term).

Let be an entry at index with term .

  1. is directly committed if at some time , while still leader of term , knows that a majority of servers store an entry with index and term (in the protocol: and a majority of values, the leader included, are at least ).
  2. An entry at index is committed in term if at some time the log of holds at index and also holds, at some index , an entry that is directly committed.

An entry is committed if it is committed in some term. Being stored on a majority is not enough: only an entry of the leader’s own term can be directly committed.

The commit rule of Module 6 sets exactly when the entry at becomes directly committed, so every index up to a leader’s holds an entry committed in that leader’s term. We repeat the rule here because it is the hypothesis everything below depends on:

The five properties of paper Figure 3, with quantifiers. In each line range over servers, over times, over terms and over log indices.

PropertyFormal statement
Election Safety
Leader Append-Only leader of at is a prefix of
Log Matchingsame term at index in two logs equal entries at all
Leader Completeness committed at in term , holds at
State Machine Safety applies at , applies at

In words: one leader per term; a leader only appends to its own log; logs that agree on one entry agree on everything before it; a committed entry is in every later leader’s log; and no two servers ever apply different entries at the same index. Note that State Machine Safety talks about an index, not a term: lecture 08.03 phrases it as “in term four everyone applies x = 5”, but the guarantee is per position in the log.

The dependency structure is a short chain. Leader Completeness is the deep one; it uses all three properties above it, and State Machine Safety follows from it in a few lines.

To proveWe use
Log MatchingElection Safety, Leader Append-Only, the consistency check
Leader CompletenessLog Matching, Leader Append-Only, Election Safety, the vote test, the commit rule
State Machine SafetyLeader Completeness, Leader Append-Only, Log Matching

The first three properties

The question: what do the handlers guarantee about logs before we think about commitment at all? The answer is three structural facts, each proved by looking at which lines of code can change a log.

Election Safety (Theorem 5.10). A candidate needs votes from a majority, two majorities of the same servers intersect (Lemma 5.8), and a server grants at most one vote per term, a promise kept across crashes because votedFor is on stable storage (Lemma 5.9). A server in the intersection of two winning vote sets would have voted twice. We also use one consequence: a server is leader of term during one contiguous time interval at most, because after a crash it restarts as a follower, and becoming leader again requires a new election, which increases its term.

Leader Append-Only (Proposition 6.7). The log changes in only three places: the leader appends a client command at the end; the AppendEntries receiver deletes a conflicting suffix; the receiver appends at the end. A leader of term never runs the deleting step, since that needs a request of term at least from a different leader, which would be either a second leader of term (impossible by Election Safety) or a higher term (which turns the leader into a follower first). Module 6 gives the proof in full.

We need two more simple facts about terms inside logs.

Lemma 7.2 (Terms inside logs).

At every time and for every server :

  1. every entry of has term at most ;
  2. the terms along are non-decreasing: for all ;
  3. entries with term are created only by , and every entry in any log is a copy, at the same index, of an entry created this way.

Log Matching

The question: if two logs agree at one position, how much else must they agree on? The answer is: everything before it.

Theorem 7.3 (Log Matching).

For all servers , all times and every index : if and both have an entry at index and these entries have the same term, then

  1. the two entries at index are equal (same command), and
  2. for every .

In words: an (index, term) pair identifies an entry and its entire history. The statement compares any two logs that ever existed, including two snapshots of the same server at different times.

The most used consequence: after AppendEntries with and entries succeeds, the follower’s log equals the leader’s log at sending time on (the step in the proof above). This is why the leader may set .

Leader Completeness

The question: why does a newly elected leader already hold every committed entry, without asking anybody? This is the property that lecture 08.02 got wrong and 08.04 put right.

Theorem 7.4 (Leader Completeness).

Assume every leader advances its commit index only by rule . For all terms , every index and every entry : if is committed at index in term and term has a leader, then at every time at which is leader, .

In words: a committed entry is present, at the same index, in the log of every leader of every later term.

The proof has two parts. Part A, the heart of §5.4.3, handles an entry that was directly committed by the leader of its own term. Part B extends this to entries committed indirectly, using Log Matching.

commit quorum: stores (i, T) vote quorum: elected leader of U S1 has (i, T) S2 has (i, T) S3 has, votes S4 votes S5 votes
Figure 7.1. The commit quorum of an entry from term T and the vote quorum of the leader of a later term U overlap; here the overlap is S3 (the voter), the key server of the Leader Completeness proof.
Remark 7.5 (Where the current-term condition is used).

Read step 7 again. It needs , the voter’s last term is at least the term of the committed entry, and this holds because the committed entry is from term , the term in which it was counted. Suppose instead that counted replicas of an older entry with term . The same argument only gives , and a candidate whose last term satisfies is not covered by the induction (its leader came before and may never have seen the entry). That is exactly server S5 in paper Figure 8: its last term is 3, between the entry’s term 2 and the counting leader’s term 4. The hypothesis of Theorem 7.4 is not a technicality; the next section shows the theorem is false without it.

Lecture correction: a new leader does not learn committed entries from followers

Lecture 08.02 (and once more 08.03) says that a committed entry may survive on a single live server that does not become leader, and that the new leader must therefore learn committed entries from followers and propagate them. Lecture 08.04 retracts this: by Leader Completeness, any server that can win an election already holds every committed entry. Raft has no follower-to-leader transfer at all; entries flow only from leaders to followers (paper §5.4.1). The 08.04 argument (if a majority stored the entry and all but two of them crashed, no candidate can collect a majority without one of those two, and they refuse anyone less up-to-date) is the special case of steps 2 and 5 above. The repair mechanism that 08.02 describes is how Paxos works; Module 10 puts the two side by side.

Lecture correction: Leader Completeness is a safety property

Lecture 08.03 calls Leader Completeness “log completeness” and says it is critical for liveness. It is a safety property: it says something bad (a later leader without a committed entry) never happens. Liveness, that commands eventually get committed, is a separate matter, discussed at the end of this module.

State Machine Safety

The question: how does a statement about leaders’ logs become a statement about what every server executes? Two more steps: what a server applies is committed, and committed entries never disagree.

Lemma 7.6 (Everything up to commitIndex is committed).

At every time, for every server and every index , the entry is committed in some term , and it stays at index of forever after.

Theorem 7.7 (State Machine Safety).

For all servers and every index : if applies entry at index and applies entry at index (at any times), then . Since every server applies indices in the order , all servers apply the same sequence of commands, each server a prefix of the longest one.

The second sentence is the bridge back to Modules 1 and 4: the sequence of applied commands is a total order broadcast that every replica delivers in the same order, so deterministic state machines stay in the same state.

The Figure 8 trap

The question: why can a leader not commit an entry from an older term once it sits on a majority, when that seems like the obvious rule? Paper Figure 8 is a five-server run in which the obvious rule loses a committed entry. Lecture 08.03 states the commit rule without the current-term condition, which is exactly this bug (Module 6 already flagged it).

Try it: tick “commit older-term entries by counting replicas (unsafe)”, choose future (d) and step to the end. S1 applies the term-2 entry at index 2 while it leads term 4, S5 then overwrites index 2, and the State Machine Safety and Leader Completeness rows turn to VIOLATED. Untick the box and replay (d): S1 never commits index 2 and nothing is violated. Finally choose future (e): S1 first replicates a term-4 entry, which commits index 2 indirectly, and S5 can no longer win.

S1 S2 S3 S4 S5 1 2 3 1 2 1 2 1 1 1 (a) S1 leads t2 1 2 3 1 2 1 2 1 1 1 3 (b) S5 leads t3 1 2 3 1 2 4 1 2 1 2 1 1 3 (c) S1 leads t4 1 2 3 1 3 1 3 1 3 1 3 1 3 (d) S5 leads t5 future 1 1 2 3 1 2 4 1 2 4 1 2 4 1 1 3 (e) S1 leads t4 future 2
Figure 7.2. Raft paper Figure 8 redrawn. Each box is a log entry labelled with its term; the dot marks the leader of the panel; the shaded box is the term-2 entry at index 2. Panels (d) and (e) are two alternative futures of panel (c), not consecutive steps.

Each panel, with the votes that make it possible. Recall that a server grants a vote only if the candidate’s log is at least as up-to-date as its own (last term first, then length).

PanelWhat happensVotes and why
(a)S1 leads term 2, appends entry (2, term 2), reaches only S2S1 won term 2 earlier
(b)S1 crashes; S5 wins term 3 and appends its own entry at index 2S3, S4, S5: their logs end in term 1, as does S5’s; S1, S2 (last term 2) would refuse
(c)S5 crashes; S1 restarts, wins term 4, copies (2, term 2) to S3, appends (3, term 4)from S2, S3, S4 (last terms 2, 1, 1, none above S1’s 2); S5 (last term 3) would refuse
(d)S1 crashes; S5 wins term 5 and overwrites index 2 on every serverS2, S3, S4: their last term is at most 2, below S5’s 3; only S1 (last term 4) would refuse, and it is down
(e)instead, S1 first copies (3, term 4) to S2 and S3S5 can no longer win: S1, S2, S3 end in term 4 and refuse; S4 and S5 are two of five

S5 needs to reach term 5 in (d): its term was 3, its first timeout takes it to term 4, which S2 and S3 reject because they are already in term 4 (they reply with term 4), so it times out again and campaigns in term 5. This detail does not matter for safety; only the logs do.

The unsafe rule. Suppose the commit rule had only its first two conditions. In panel (c), S1 (leader of term 4) sees the entry (index 2, term 2) on S1, S2, S3, three of five, and marks index 2 committed. It applies the command at index 2, say x := A, and tells its client “done”. Now continue with future (d): S1 crashes, S5 wins term 5 with the votes of S2, S3, S4 and replicates its entry (index 2, term 3), say x := B, to everyone, then commits and applies it. S1 has applied x := A at index 2 and S5 has applied x := B at index 2. State Machine Safety is broken, and the client that was told “done” lost its write.

What the correct rule does. In panel (c), S1 counts three replicas of index 2, but , so rule does not fire and nobody applies index 2. Future (d) is still possible, and it is harmless: S5 overwrites an entry that was never committed. In future (e), the term-4 entry at index 3 reaches S1, S2, S3; now , S1 commits index 3, and index 2 is committed with it. From then on any candidate needs a vote from S1, S2 or S3, all of which end in term 4, so a server with last term 3, such as S5, cannot win.

Two practical consequences. First, an entry of an old term is committed only when a later current-term entry is; if no client sends a command, it may stay uncommitted. That is why a new leader appends a blank no-op entry at the start of its term (paper §8, Module 6). Second, the rule is conservative: paper §5.4.2 notes that an old entry stored on every server could safely be committed (Homework 7), but Raft keeps the simpler rule.

Exam trap: "(d) and (e) happen one after the other"

Panels (d) and (e) are alternative futures of panel (c), not consecutive steps. In (e) S5 cannot win any more, so (d) cannot follow (e). A related wrong answer: “Figure 8 shows that Raft can lose a committed entry.” It shows that the naive rule could; in correct Raft, index 2 in panel (c) is not committed, so overwriting it in (d) loses nothing a client was promised.

Reading Figure 7: who could have produced these logs, and who could win

The question: how different can followers’ logs be when a new leader takes over, and how do we check a claimed log state for consistency with the invariants? Lecture 08.03 walks through paper Figure 7: seven servers, a freshly elected leader of term 8, and six possible follower logs (a) to (f).

Try it: read the table of possible winners before stepping. Only the leader, (a), (c) and (d) pass the up-to-date test with enough votes; compare this with the analysis below. Then let the term-8 leader run and watch nextIndex walk back for each follower until every log equals the leader’s.

1 2 3 4 5 6 7 8 9 10 11 12 leader 1 1 1 4 4 5 5 6 6 6 term 8, just elected (a) 1 1 1 4 4 5 5 6 6 missing entry; votes yes (b) 1 1 1 4 missing entries; votes yes (c) 1 1 1 4 4 5 5 6 6 6 6 extra entry; votes no (d) 1 1 1 4 4 5 5 6 6 6 7 7 extra entries; votes no (e) 1 1 1 4 4 4 4 missing and extra; yes (f) 1 1 1 2 2 2 3 3 3 3 3 missing and extra; yes
Figure 7.3. Raft paper Figure 7 redrawn: the log of the term-8 leader (shaded) and six possible follower logs in a 7-server cluster. The right column says how each follower differs and whether it could have voted for this leader.

How each follower log could arise. The paper’s caption gives one story for (f); here is one for each. Each story must respect Log Matching and the fact that only the leader of term creates term- entries.

LogLast (term, index)One way it could arise
(a)(6, 9)missed the last term-6 append (crashed, or a lost message)
(b)(4, 4)crashed early in term 4 and stayed down
(c)(6, 11)received one more term-6 entry than the term-8 leader; the term-6 leader (perhaps c) appended index 11, then lost leadership
(d)(7, 12)led term 7 (or was its only follower to receive anything), appended 11 and 12, crashed before replicating them
(e)(4, 7)took entries 6 and 7 from the term-4 leader (or was it), which lost leadership before replicating them; meanwhile term 5 overwrote index 6 elsewhere
(f)(3, 11)led term 2, appended 4 to 6, crashed; restarted, led term 3, appended 7 to 11, crashed again before committing any of them

Lecture 08.03 tells the (e) and (d) stories as “e was the leader of term 4” and “d was the leader of term 7”. Both are possible, but the logs alone do not prove it: a follower that received entries from that leader would look the same.

Who voted for the leader of term 8? Seven servers, so a winner needs four votes, its own included. The leader’s log ends in (term 6, index 10). A follower grants its vote only if that is at least as up-to-date as its own last (term, index):

  • (a) (6, 9): same last term, shorter, so yes. (b) (4, 4): lower last term, yes.
  • (c) (6, 11): same last term but longer, so no. (d) (7, 12): higher last term, no.
  • (e) (4, 7) and (f) (3, 11): lower last term, yes, even though (f) is longer.

So the leader can have collected the votes of a, b, e, f and itself (five, at least four needed), as lecture 08.03 says. Note what length does not buy: (f) has eleven entries and still loses the comparison.

Who else could win an election? Suppose the term-8 leader crashed at once and everyone campaigned in term 9 with the logs shown. Ordering the last (term, index) pairs gives d (7, 12) above c (6, 11) above the old leader (6, 10) above a (6, 9) above e (4, 7) above b (4, 4) above f (3, 11). A candidate gets votes from itself and every server ranked at or below it:

CandidateVotes it can getCan win (needs 4)?
(d)all 6 live serversyes
(c)all live but d: 5yes
(a)a, e, b, f: 4yes, just
(e)e, b, f: 3no
(b)b, f: 2no
(f)f: 1no

Is it dangerous that (d) or (c) could win and later overwrite entries of the old leader? Only if those entries were committed. Every committed entry is on a majority of four servers. Index 10 (term 6) is on the leader, c and d only, three servers, so it cannot be committed; (a) winning and dropping it is fine. Indices 1 to 9 of the leader’s log are on at least four servers and may be committed, and indeed every candidate that can win, (a), (c), (d), holds all of them. This is Leader Completeness seen from the data.

After the election. The leader initializes for every follower, so its first request checks index 10. For (a) and (b) the walk-back stops at their last index (9 and 4) and the missing entries are appended. For (c) and (d) the check at index 10 succeeds at once, because both hold the leader’s term-6 entry there; their extra entries after index 10 are not in conflict with anything sent, so they survive as a stale, harmless suffix until the leader appends its own index 11 and the next request deletes them (step 2 of the receiver in Module 6). They are never applied, since a follower’s commit index is bounded by the last entry the leader verified. For (e) the walk-back stops at index 5 and for (f) at index 3; everything after those positions is replaced by the leader’s entries.

Worked example: the proof on a concrete run

Five servers A, B, C, D, E. We follow the Leader Completeness proof step by step on one run, so that each step can be checked against concrete logs. Logs are written as lists of terms.

  1. Term 2, leader A. A appends a client command at index 3, giving A the log (1, 1, 2). It sends AppendEntries with , ; B and C accept. Now A, B, C hold (1, 1, 2). The entry at index 3 has term 2 = A’s current term and is on three of five servers, so by rule A sets . The commit quorum is .
  2. A crashes. D and E still hold (1, 1). D times out and campaigns in term 3 with last (term, index) = (1, 2).
  3. Who votes for D? E: same last term, same length, yes. B and C: their last term 2 is above D’s 1, no. So D gets two votes, D and E, short of three. D cannot win. The proof’s prediction: any winning vote set intersects , and every member of that set refuses D.
  4. C times out and campaigns in term 4 with last (2, 3). D and E (last (1, 2)) grant; B (last (2, 3), same) grants. C wins with or any three of them. Leader Completeness holds trivially: C has the entry.
  5. Map to the proof. Suppose someone claimed a leader of a term without the entry. Step 2 picks a voter in , here B or C. Step 3: B stored the entry in term 2 before voting in term 4, since after voting it would reject term-2 AppendEntries. Step 4: nothing removed it. Step 5: the candidate’s last (term, index) must be at least B’s (2, 3). Step 6: if the candidate’s last term is 2, it was created by the term-2 leader A at an index at least 3, so its log contains A’s entry at 3. Step 7: if the candidate’s last term is larger, say 3, then a term-3 leader must have created that entry, and by minimality that leader already held the entry at index 3, so the candidate does too. In this run term 3 has no leader at all (D failed and nobody else ran), so no log can even end in term 3.

The run also shows the role of minimality: in step 7 the argument leans on the leaders of the intermediate terms (here term 3, which had none), and the smallest counterexample guarantees those leaders are already known to be fine.

Safety and liveness

The question: everything above holds in a fully asynchronous network, with arbitrary delays. Then why does Raft need timeouts at all? Because safety and liveness are different promises.

Lecture 08.03 makes the point with a slogan: a safe system can be a dead system. A cluster in which every server refuses every request never violates any of the five properties. None of the proofs in this module mentions time, message delay, or the election timeout; they hold in every execution of the asynchronous model, including executions with partitions, message reordering and arbitrarily slow servers. That is the meaning of “safety does not depend on timing” (paper §5.6, Kleppmann slide 107).

Liveness, that a client command is eventually committed, needs more: a majority of servers up and connected, and a stable leader for long enough. FLP (Module 4) says no deterministic algorithm can guarantee this in a purely asynchronous system. Raft gets it under partial synchrony with randomized election timeouts and the timing requirement of paper §5.6,

where broadcastTime is one parallel round trip to all servers (0.5 to 20 ms) and MTBF the mean time between failures of one server (months). If elections keep splitting votes, or a leader keeps being deposed, Raft stays safe and makes no progress.

Figure 8 has a liveness side too. With the current-term rule, an old entry is not committed until the new leader commits an entry of its own term, so a leader that receives no client commands could leave old entries uncommitted indefinitely. The no-op entry at the start of each term removes that gap; it is a liveness fix, not a safety fix.

Summary: the invariants and where they come from

The labels in the last column refer to the handlers of Module 6 (paper Figure 2).

PropertyStatement (short)Guaranteed byHandler line
Election Safetyat most one leader per termmajority votes, one persisted vote per termRequestVote: grant only if votedFor null or same
Leader Append-Onlya leader only appends to its logdeletion only in the follower pathclient request: append at end
Log Matchingsame (index, term) means same prefixone leader per term + consistency checkAppendEntries step 1 (check), step 2 (delete only on conflict)
Leader Completenesscommitted entry is in every later leaderquorum overlap + up-to-date vote + current-term commitRequestVote up-to-date test; commit rule condition 3
State Machine Safetysame entry applied at each indexLeader Completeness + apply only up to commitIndexapply loop; follower max(commitIndex, min(leaderCommit, lastNew))

Key point

Majority intersection by itself is not the argument. It only produces a server (the voter) that has seen both events; the protocol rules then force that server to transmit what it knows: it rejects stale AppendEntries after voting (term monotonicity), it keeps the entry (Log Matching and the induction), and it refuses candidates that lack it (the vote test). Remove any one rule and the proof breaks at a specific step.

What breaks

The proofs assume crash-recovery with stable storage: currentTerm, votedFor and log[] must be on disk before a server replies. If a server forgets its vote after a restart, Election Safety fails (Module 5); if it loses log entries, the voter in step 4 may no longer hold the committed entry. They assume a fixed configuration: changing the set of servers naively can create two disjoint majorities (paper §6, joint consensus). And they assume non-Byzantine servers: lecture 08.02 stresses that a single server sending forged AppendEntries can make every follower delete its log, and a single bit flip on the leader can spread to all replicas. Finally, the guarantees are only safety; under a partition without a majority, or permanent timing chaos, nothing gets committed.

Why it matters

Leader Completeness is the reason a Raft leader can be “the truth”: it never has to ask anyone about the past, and entries only flow from leader to followers. State Machine Safety is the property that turns the replicated log into a correct state machine replication service (Module 4). Module 8 proves the analogous fact for Paxos (a chosen value stays chosen), where the new leader instead learns the past from a quorum during Phase 1, and Module 9 does the same per slot. Module 10 shows that both proofs are instances of one lemma about intersecting quorums.

Further reading

  • The Raft paper, §5.3 to §5.4, Figures 3, 7, 8, 9 (1).
  • Ongaro’s thesis, chapter 3 and appendix B (the TLA+ specification and the full proof) (2).
  • Kleppmann’s notes §6.2 for the same algorithm in his notation (3).
  • Howard and Mortier compare Raft’s election restriction with Paxos’s log repair (4).

Homework

The problems for this module are in Homework 7.

1.
Ongaro D, Ousterhout J. In Search of an Understandable Consensus Algorithm. In: Proceedings of the USENIX Annual Technical Conference (ATC). 2014. p. 305–19.
2.
Ongaro D. Consensus: Bridging Theory and Practice [phdthesis]. Stanford University; 2014.
3.
Kleppmann M. Distributed Systems: Lecture Notes [Internet]. University of Cambridge, Computer Science Tripos Part IB; 2021. Available from: https://www.cl.cam.ac.uk/teaching/2122/ConcDisSys/dist-sys-notes.pdf
4.
Howard H, Mortier R. Paxos vs Raft: Have We Reached Consensus on Distributed Consensus? In: Proceedings of the 7th Workshop on Principles and Practice of Consistency for Distributed Data (PaPoC). 2020.