Course map

Topic 133 / Advanced

Leader election

Chapter 8.3 · Consensus and Coordination

What and why

Consensus (→ 132) is the general mechanism; leader election is its most common application — picking exactly one node to hold a role (primary database, lock holder, job coordinator) so the rest of the system has one place to send a given request. The hard part isn't choosing a leader; it's making sure a demoted or partitioned old leader can't keep acting as leader after a new one is elected — a zombie leader. Builds on: → 132 (consensus: Raft and Paxos).

Diagram

Fencing token closes the zombie-leader gap Leader A holds fencing token 5, then pauses. The election times out and Leader B is elected with token 6. When A wakes and writes with token 5, storage rejects it because 5 is lower than the highest token already seen. When B writes with token 6, storage accepts it. Leader A Storage Leader B t0 holds fencing token 5 pauses — GC pause / partition t1 Election times out → Leader B elected, token 6 t2 write (token=5) reject — 5 < last seen 6 t3 write (token=6) accepted
Fencing token: each election issues a monotonically increasing token that the leader must present with every write; storage rejects any write carrying a token lower than the highest one it has already seen — so a zombie leader's stale writes are refused even though it doesn't know it has been replaced.
Text version of this diagram
t0: Leader A holds fencing token 5, then pauses (GC pause / partition)
t1: Election times out → Leader B elected, token 6
t2: A wakes up, writes with token 5 → storage rejects: "5 < last seen 6"
t3: B writes with token 6 → accepted

How it works

  1. Elect a leader via consensus (→ 132): a majority of nodes agree on one candidate for the current term/epoch.
  2. Every election increases a term or epoch number; any message carrying a lower term is recognized as stale and ignored by the rest of the cluster.
  3. The ambiguity from failure detection (→ 127) is the real danger: a leader that is merely slow (a GC pause, a brief partition) looks dead to the cluster, which elects a replacement — but the old leader may wake up or reconnect still believing it's in charge.
  4. A fencing token closes this gap: each election issues a monotonically increasing number that the leader must present with every write to shared storage; storage rejects any write carrying a token lower than the highest it has already seen, so a zombie leader's stale writes are refused even though the zombie itself doesn't know it's been replaced.
  5. Election alone, without fencing, only reduces the chance of two active leaders; fencing is what actually prevents one from corrupting shared state.

Practical example

(illustrative). Leader A holds fencing token 5 and pauses for a long GC cycle. The cluster elects leader B with token 6. A resumes and sends a write still carrying token 5; because 5 is lower than the highest token (6) already seen, storage rejects it — even though A still believes it is the leader.

When to use it

  • Use when: exactly one active writer or coordinator is required and a crash must be survivable without manual intervention (Project 11's job scheduler).
  • Avoid when: a brief, manually-handled failover is acceptable — fencing-token discipline adds real complexity that's wasted if nothing downstream enforces tokens.
  • Failure scenarios: a GC-paused old leader and a newly elected leader both writing to the same resource in the window before the old leader notices it's been replaced; fencing tokens generated but never actually checked by the protected resource.
  • Simpler alternative: a single, manually promoted standby for low-frequency failover needs, where automated election isn't justified.

Practice

(Mechanism kind — predict-then-verify; 20 min, no lab services). Using the scenario above (leader A, token 5, pauses; leader B elected, token 6), predict what happens to A's stale write if storage does not check fencing tokens, versus if it does. Write both outcomes, then state the one specific way the non-fencing design loses or corrupts data that the fenced design doesn't. Done when your two outcomes differ in exactly the way the token check predicts.

Key takeaway

Leader election picks one leader; fencing tokens are what stop an old leader that doesn't know it's been replaced from still acting like one. Done when you can: explain why election without fencing is not enough, using a GC pause as the concrete trigger. Used next in → 136, 137; Project 11.

References

Ongaro and Ousterhout, "In Search of an Understandable Consensus Algorithm (Extended Version)" — see references.md. Kleppmann and Riccomini, Designing Data-Intensive Applications — see references.md.

Source directory

Search the guide