Lab · Puzzle

Leader election

Five nodes running Raft, and you control the network between them. Crash leaders, cut links and split the cluster in two, trying to get two leaders to commit different writes. Terms and quorums stop you, until the last level switches the quorum check off.

Level 1 of 5

Long live the leader

Five nodes, one leader. Followers expect a heartbeat every few ticks; when one stops hearing it, it waits out a random timeout and asks for votes.

GoalCrash the leader and get a new one elected in a later term.

Hint

Press play until someone leads, select it, and crash it. The random timeouts decide who notices first.

tick 0
Cut link n1–n2Cut link n1–n3Cut link n1–n4Cut link n1–n5Cut link n2–n3Cut link n2–n4Cut link n2–n5Cut link n3–n4Cut link n3–n5Cut link n4–n5n1f t0n2f t0n3f t0n4f t0n5f t0

Tick 0: no leader

Cluster

No leader yet

Select a node to crash it, restart it, send it a client write or move it to the other side of a partition. Tap a link in the diagram to cut it.

RequestVote
vote granted
vote refused
AppendEntries

Replicated logssolid = committed

1234
n1
n2
n3
n4
n5

Cluster log

  1. Quiet. Press play.

What the puzzle models

The nodes run a small but faithful version of Raft. Time moves in ticks, and a message takes two or three ticks to cross a link. A follower that hears nothing from a leader for ten to twenty ticks (the exact number is random, per node, per wait) becomes a candidate. It increments its term, votes for itself and asks everyone else for a vote. A majority makes it leader, and it starts sending heartbeats carrying any log entries a follower is missing.

The rules that matter are the same as in the paper. A node votes at most once per term. Any message with a higher term makes the receiver adopt that term and step down. A follower only accepts entries that line up with the entry just before them, and throws away anything that conflicts. A leader commits an entry from its own term once a majority of nodes hold it. What's left out is the machinery a real implementation needs and a puzzle doesn't: snapshots, membership changes and per-RPC retry timers.

Why randomised timeouts

If every follower waited exactly the same time, they would all become candidates in the same tick, each vote for itself, and nobody would win. They would time out together again and repeat. Randomising the timeout means one node usually wakes up first and collects its votes before the others start. It doesn't make split votes impossible. It makes them unlikely to happen twice in a row.

Terms are a logical clock

A term number goes up with every election, and every message carries one. That is how a deposed leader finds out: the first message it sees from a later term tells it that the cluster moved on without it. Until then it really does believe it is leader. That's why level 2 has two leaders at once. It is allowed, because the stale one can't commit anything.

Majorities overlap

Every safety argument in Raft comes back to one fact: any two majorities of the same cluster share at least one node. An entry is only committed once a majority holds it, and a candidate only wins with votes from a majority. So at least one voter in every election holds every committed entry. That voter refuses any candidate whose log is behind its own, comparing last terms first and then length. A new leader therefore always has every committed write, which is what level 4 shows from the other side.

Level 4 also shows a real weakness. A node that was cut off keeps starting elections nobody hears, so its term grows. When it comes back, that term forces a working leader to step down, even though the returning node can't win. Production implementations such as etcd add a pre-vote round: a candidate first asks whether it could win, and only bumps its term if a majority says yes.

What breaks without a quorum

The last level commits an entry once two of the five nodes hold it. Two groups of two don't have to overlap, so the old leader on the small side can commit a write while the big side elects a new leader and commits a different write at the same index. Both clients were told their write succeeded, and when the network heals one of those "committed" writes is silently overwritten. That is a split brain, and it is what every shortcut around a majority quorum eventually turns into.