RAFT
Leader election by majority vote
Three nodes. The leader N1 crashes. N2 and N3 use the same fixed 150 ms election timeout, so both time out at the same instant and start an election for term 2 (terms are numbered election rounds; a node gets one vote per term). What happens?
Raft keeps one ordered log on every server. One leader appends client writes and copies them to followers; an entry is committed once a majority holds it. Time is split into numbered terms, each with at most one leader.
When followers stop hearing heartbeats, one times out, increments the term, votes for itself and asks the others. A voter grants at most one vote per term, and only to a candidate whose log is at least as up to date as its own. A majority of votes makes a leader. Randomized timeouts make ties rare. A follower whose log disagrees rejects AppendEntries until the leader backs up to the point where they match.
RequestVote(term, candidateId, lastLogIndex, lastLogTerm)
if term < currentTerm: reject
if term > currentTerm: currentTerm = term; votedFor = null; become follower
if votedFor not in (null, candidateId): reject // one vote per term
if candidate log older than mine: reject // compare last term, then length
votedFor = candidateId; reset election timer; grant
Election
timer fires (random 150-300 ms) -> currentTerm++, vote for self, ask everyone
votes from a majority -> LEADER; set nextIndex[i] = lastLogIndex + 1
hears a higher term -> FOLLOWER
timer fires again -> new term, new election (split vote)
AppendEntries(term, prevLogIndex, prevLogTerm, entries, leaderCommit)
if term < currentTerm: reject (leader steps down)
if log[prevLogIndex].term != prevLogTerm: reject; leader decrements nextIndex
for each entry: conflict -> delete it and all after; then append
commitIndex = min(leaderCommit, index of last new entry)
Commit: a leader marks entry N committed when a majority hold it
AND log[N].term == currentTerm (older-term entries commit only with it).
Safety: two majorities overlap, so one vote per term means one leader per term;
the overlap also holds the newest committed entry, so the log check means
every new leader already has every committed entry. Watch the two invariants and the “leaders by term” row. Every Raft scenario keeps both green, even STALE LEADER REJOINS, where two nodes show LEADER at once in different terms. The two BUG scenarios break the voting rule. VOTE TWICE drops the one-vote-per-term rule (this sim’s version also skips the log check, which changes nothing here because the logs are equal) and elects two leaders in term 2. NO LOG CHECK keeps one vote per term but never compares logs, so it elects N1 with a stale log, and N1 overwrites the committed 2:c.
In LOG DIVERGENCE, N1’s term-3 RequestVote makes the healthy term-2 leader N3 step down, even though N1 cannot win. That is correct Raft, not a sim bug: a node that rejoins with a higher term disrupts the cluster. The PreVote extension (etcd and others use it) stops this by having a candidate first check that it could win before it raises its term.
N1 is going to crash. Inject one more crash or partition so that the survivors can never elect a new leader. Then try the same fault with NODES at 4 and at 5.
GOAL: no leader is elected after term 1
Make onRequestVote safe. Grant at most one vote per term, and only to a candidate whose log is at least as up to date as yours (compare the last entry's term first, then the length). A higher term always updates currentTerm and clears votedFor, even if you then refuse.
etcd (and therefore Kubernetes), Consul, CockroachDB, TiKV, Kafka's KRaft controller and most new replicated databases run Raft. You meet it as leader-election storms, a 3- versus 5-node sizing decision, or a cluster that goes read-only when it loses its majority.
“How does Raft elect a leader, and why can there not be two?” Walk the term, the one persisted vote, the majority, and the randomized timeout. The follow-up is “what stops a node with a stale log from winning?”: the up-to-date check on lastLogTerm then lastLogIndex. Mention that 4 nodes tolerate no more failures than 3.
USED IN
Chapters that build on this lesson.
- Failover, split brain and fencingDatabases