Raft elections: terms, durable votes, and log eligibility
Explain how a candidate becomes leader while distinguishing one leader per term from the stronger property that future leaders retain committed history.
On this page
The question for today
What you’ll understand
- Trace term advancement and one-vote-per-term persistence.
- Apply Raft's last-log-term and last-log-index comparison.
- Separate election safety from timing assumptions that make elections finish.
Raft makes a leader responsible for proposing a log sequence, but a machine calling itself leader does not make its history authoritative forever. Terms label election generations, votes establish a quorum mandate, and an eligibility rule prevents candidates with insufficient log history from winning. This lesson uses the basic fixed-membership crash-fault protocol. Pre-vote, leadership transfer, leases, and implementation-specific optimizations require their own rules and must not be inferred from the simplified election story.
A term is durable logical state, not a clock reading
A follower that starts an election advances its current term, becomes a candidate, votes for itself, and requests votes. Each server grants at most one candidate its vote in a given term, retaining the decision across crashes. A candidate becomes leader only after obtaining the required majority for that same term. Duplicate replies do not create extra voters, and a delayed grant from an older term cannot be counted toward the current election. Node identity and the configured voter set define what a distinct vote means.
On observing a valid higher term, a server updates its term and follows the protocol's transition to follower. A disconnected old leader can nevertheless continue to believe it is leader until it learns newer information. The invariant is not that only one process anywhere ever has the leader role at a moment. It is that at most one candidate can obtain a valid majority for a particular term, because intersecting majorities cannot share a server that votes twice in that term. Persisting votes is therefore a safety requirement, not an optional recovery optimization.
A vote also compares log history
One leader per term is insufficient if a later leader can omit committed entries. Raft therefore compares the candidate's last log term and index with the voter's pair. A later last-entry term is considered more up-to-date; only when those terms match does the larger last index win. A candidate with a more recent last-entry term can be eligible even with a shorter log. The comparison belongs to the complete log-matching and commitment proof; it is not an arbitrary preference for shorter or longer files.
The term in an election request and the term of the candidate's last log entry are different fields. A candidate starting election term twelve might have last created entries from term nine. Raising the election term does not fabricate a term-twelve log entry or repair missing history. Before granting, a voter checks both its vote availability in the request's term and the log eligibility condition. Skipping the log check can elect a candidate that discards prior committed commands despite an apparently well-formed majority election.
Election timeouts affect progress, not permission to forget
Randomized election delays reduce repeated split votes when message delivery and processing become timely enough. Choose timeouts with realistic network, scheduling, and disk delays in mind; a busy event loop can look like a failed leader. No particular timeout makes a crash inference infallible. Increasing a timeout changes responsiveness and instability tradeoffs, while weakening vote persistence or accepting stale-term messages changes correctness. Separate those changes during diagnosis rather than treating every election failure as a reason to bypass the protocol.
In the reliability lab, Reset and explicitly set n=5, k=2 before predicting and using Play or Step. Three available voters meet the numerical election threshold. For another run Reset and explicitly set n=5, k=3; two cannot. The illustration contains no candidate, term, vote history, log-eligibility check, or message delay. It cannot distinguish three eligible voters from three nodes that already voted for competing candidates. Use a separate per-term vote table to explain whether a particular candidate can actually win.
Work it through
Compare eligibility before counting votes
A voter ends its log at term four, index 100. Candidate A ends at (5,80), B at (4,101), and C at (4,99). All are examples of otherwise valid logs, and consider separate elections in which the voter is free to vote.
Candidate A's last-entry term five exceeds four, so A passes this voter's log comparison despite its smaller last index 80. Its election-request term must independently satisfy the protocol.
B has the same last-entry term four and a longer index 101, so B also passes in its separate election. C has term four but index 99, so C fails the log eligibility test.
For a five-node election, an eligible candidate still needs three distinct grants in one term. The voter cannot grant both A and B in the same term merely because both would be individually eligible; it must persist its chosen vote before acknowledging it.
A and B can pass the specified log test in separate elections; C cannot. Passing eligibility is necessary but is not itself election victory.
The living notebook
Explore the model
Interactive lab · reliability
Votes require more than reachable machines
Compare independent runs with Reset → explicitly set both n and k → predict → Play or Step. Use (5,2), then Reset and (5,3). The majority toy does not run elections or represent terms, durable votes, log eligibility, or conflicting candidates.
- Available replicas
- 4 of 5
- Majority needed
- 3
- Majority available?
- Yes
A replica count and a majority threshold, without a consensus protocol. Playback highlights fixed replica states. Having a majority alone does not establish linearizability, durability or failure independence. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.
Look forCount distinct configured voters, then inspect each voter's actual term, prior vote, and log comparison before claiming an election quorum.
Pause & reason
Check your understanding
Your turn
Try it yourself
In a seven-voter group, a candidate receives two distinct grants plus its own persisted vote. One grant is delivered twice. How many additional distinct grants are needed? Can restarting a voter erase its choice for the same term?
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
Compute the full-group majority and count voter identities rather than messages. Then apply the crash-recovery invariant.
Compare with the solution
The majority is four. Two distinct remote grants plus the candidate's own vote total three, so one more distinct eligible grant is needed; the duplicate adds nothing. A restarting voter must recover its term and vote so it cannot grant a competing candidate in the same term. If restarting could erase that record, two majority elections in one term could share a double-voting node, breaking the argument that made the count meaningful.
Keep the idea
Take it with you
- Terms organize logical election generations; they do not measure elapsed time.
- Durable one-vote-per-term behavior makes majority intersection constrain leadership.
- Log eligibility uses last-entry term first, then last-entry index.
Read further
Original sources and deeper explanations to follow at your own pace.
Optional progress, saved in this browser when available. Revisit any lesson at any time.
Lesson not marked complete.