SD System Design In Depth
0%
lesson system-designcoordination

Leader election

Module 3.2: Distributed Coordination, Consensus & Leases Updated Sep 17, 2026
📄 Cheat Sheet
Distributed Systems 6.2: Raft
Martin Kleppmann Distributed Systems 6.2: Raft Watch on YouTube ↗

Kleppmann's Raft lecture steps through terms, randomised election timeouts, vote requests and the up-to-date-log check, the precise mechanics this lesson relies on.

0:00 / --:--

Concrete scenario

A scheduler leader pauses for 20 seconds after its lease expires while a replacement begins running jobs.

Mechanics

Raft elects a leader by term and majority vote, with an up-to-date-log restriction that preserves committed entries. Election timeout ranges are deployment parameters, not universal constants. Leadership authorizes log coordination; external side effects still need idempotency or fencing accepted by the target.

Failure mode

Randomized timeouts reduce split votes but do not prevent every split-brain effect. A paused or partitioned old leader can continue local work, and clock-based leases can expire unnoticed by that process.

Trade-off

A leader simplifies ordering and ownership but creates failover pauses and a hotspot. Leaderless designs spread coordination differently but move conflict and quorum logic to each operation.

Worked example: a Raft election timeline

Five nodes, heartbeat 100 ms, election timeout randomised between 1000 and 2000 ms. The leader L (term 4) crashes at t = 0.

  • t = 1,230 ms: follower B's timer fires first. B increments its term to 5, votes for itself, and sends RequestVote(term 5, lastLogIndex 812, lastLogTerm 4).
  • C, D and E have last index 812 or lower and have not voted in term 5, so they grant their votes. B has 4 of 5 votes, a majority, and becomes leader by about t = 1,235 ms.
  • B sends heartbeats immediately, which resets the others' timers before they fire. Total unavailability is about 1.2 seconds.

Now suppose L was not dead but paused for 3 seconds. It wakes up still thinking it leads term 4 and sends AppendEntries. Every follower replies with "term 5", so L steps down. Any client write L accepted after waking cannot commit, because it can never reach a majority in term 4.

Election approaches

ApproachSafety basisFailover timeUsed by
Raft or Zab internal electionMajority vote per termAbout the election timeoutetcd, Consul, ZooKeeper
Lease in a consensus storeStore's linearizability plus lease TTLLease TTL, often 10 to 15 sKubernetes controllers (Lease objects), Patroni
ZooKeeper sequential ephemeral nodesLowest sequence number wins. Session expiry releases itSession timeoutHBase, older Kafka controllers
Bully / ring algorithmsHighest ID wins. Assumes reliable failure detectionVariesTextbooks. Unsafe under partitions
Database row lock or advisory lockThe database's lockingConnection timeoutSimple cron leaders

Failure modes

  • Flapping leadership when timeouts sit near p99 network or disk latency. Raft's PreVote extension stops a partitioned node from disrupting the cluster with inflated terms when it rejoins.
  • Leader in the wrong place: if the leader sits in a far region, every write pays the long round trip. Some systems support leader preferences or transfers.
  • Clock-based leases on the leader (for fast local reads) are only safe with bounded clock drift. etcd and CockroachDB document this assumption.
Check yourself Question 1 of 3
Up-to-date log rule recall
From: Mechanics

Why does a Raft follower refuse to vote for a candidate whose log is less up to date than its own?

Raft Consensus (3 Nodes)

Key Takeaways

Election timeouts are deployment parameters, not universal constants.
A leader still needs fencing or idempotency for external effects.
Randomization reduces split votes but does not alone prevent stale-owner work.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue