Lesson 02

Leader Election and Consensus

Building block

Most designs put one machine in charge of something: the primary of a partition, the scheduler, the counter service that hands out blocks of IDs. A first design with one such machine, and a person who promotes a replacement by hand when it fails, is a correct design. It is where you start, and you say so.

The probe that changes it is "The primary dies at 3 a.m." Now the other machines must choose a replacement themselves, and never two. That is consensus. In an interview you never build it. You rent it from a small replicated service, and you say why it is safe and what every write through it costs.

A Replicated Log Is the Product

Consensus, as systems use it, means agreeing on the order of one log. Every replica applies the same commands in the same order, and so ends in the same state: a replicated state machine. An election exists only so that the log has one writer at a time.

Paxos, Leslie Lamport's protocol, came first. Raft, published by Diego Ongaro and John Ousterhout in 2014, was designed to be easier to understand, and it is the one outlined here. How a store replicates its data, with one leader, several or none, is Chapter 4's first lesson; this lesson owns how the leader is chosen.

Terms and Votes

Every node is a follower, a candidate or the leader. The leader sends heartbeats. A follower that hears nothing for its election timeout becomes a candidate: it raises the term by one, votes for itself, and asks the others for their votes.

Each node votes at most once per term, so at most one candidate can collect a majority: one leader per term. The election timeouts are randomised, so two followers rarely become candidates at the same moment and split the vote.

The term also works as a logical clock. A message from an older term is refused, which is how a deposed leader learns that it has been deposed. And a node refuses its vote to a candidate whose log is behind its own, so a new leader always holds every entry that was committed.

One election
No heartbeata follower's election timeout runs out
→
It standsraises the term, votes for itself
→
A majority votes yesone vote per node per term
→
It leadsand sends heartbeats

The Log and the Majority

The leader appends a command to its log and sends it to the others. The entry is committed once a majority holds it, and only a committed entry is applied.

The majority sets the cluster's size. Three members have a majority of two, so they survive one failure. Five have a majority of three and survive two, the size the Raft paper calls typical. Four also need three for a majority, so they still survive only one failure: an even size adds a machine and no safety.

A partition splits the members, and only the side with a majority can commit. An old leader on the small side may still believe it leads, but it cannot change the log. What it can still do outside the log, acting on a stale belief, is the problem of the next lesson.

Three members against five
Three membersmajority of 2
Survives: one failure.
A write, zones of one region: waits one round trip of 1-2 ms to the nearest follower.
A write, three regions: about 70 ms, US east to US west.
Five membersmajority of 3
Survives: two failures.
A write, zones of one region: waits for the two nearest followers, still 1-2 ms each.
A write, three regions: about 70 ms, set by the nearest member in another region.

What a Coordination Service Gives a Design

etcd, which runs Raft, and ZooKeeper, a replicated state machine of the same kind, sell consensus as a few primitives. A small key-value store with compare-and-set transactions. Leases, or ephemeral nodes, that vanish when their holder stops renewing them. Watches that push a change to every client that asked. And a counter that only grows: etcd's revision, or ZooKeeper's sequential nodes.

From those, a design gets configuration that every service reads the same way, a membership list, leader election for its own services, and the base of a lock. Some systems embed consensus instead. Spanner replicates each piece of its data through a Paxos group. Kafka moved its metadata from ZooKeeper to its own Raft-based controllers, and Kafka 4.0, released in March 2025, runs without ZooKeeper at all.

On the board, the coordination service sits beside the data path, never on it. Every request goes from the app servers to the store and back. Off that path, each partition's leader holds a leader key, the app servers watch the configuration and hear from it only when it changes, and they ask it for a new block of IDs every few minutes.

A coordination service beside the data path, asked per election, per change and per block
App serversthe data path Data storea leader per partition Coordination3 members, 3 zones every request leader key: per election ID block: every 8 min config changes data pathoff the data path

What It Costs

Every write waits for a majority's durable acknowledgement. Between zones of one region, a round trip is 1 to 2 milliseconds, so a write costs a few milliseconds. Spread the members over US east, US west and western Europe, and a leader in US east waits for its nearest follower, in US west, about 70 milliseconds away, on every write.

One leader orders every write, so the service is a control plane: small facts that rarely change, never per-request data. The rule follows from that. Ask it once per many operations, the way the URL shortener's counter service is asked once per block of 100,000 IDs. And without a majority it accepts no writes, so every service that waits on it stops changing too.

Coordination Service vs Built-In Consensus

A coordination service, such as etcd or ZooKeeper, is one small cluster that many systems ask about leaders, membership and configuration. Use it when the data store has no consensus of its own and several services must agree on who does what.

Built-in consensus puts the log inside the system: Spanner replicates its data through Paxos groups, and Kafka now keeps its metadata in a quorum of its own controllers. Use it when the data itself must survive a failover with no acknowledged write lost, or when a second cluster to run costs more than it saves.

How Interviewers Probe This

The first probe is "The primary dies. How does the system pick a new one, and how do you know there are not two?" A good answer puts a leader key with a lease in a coordination service, lets a majority decide, gives the failover time as the election timeout plus one election, and fences the old primary. You might say: "Each partition's leader holds a key under a lease. If it stops renewing, a replica wins the next term within a timeout and an election, and the store refuses the old leader's writes by their token."

The second is "Why five members and not four, or seven?" A good answer does the majority arithmetic in words. Four survive one failure, the same as three. Seven survive a third failure, and every write then waits for one more acknowledgement.

The third is "Your coordination cluster spans three regions. What does a write cost, and is it worth it?" A good answer gives about 70 milliseconds per write, the round trip to the nearest member that completes a majority, and keeps the cluster in one region across zones unless the business must outlive a region.

Misconceptions
  • "Consensus makes a system highly available." It makes it correct. Without a majority it refuses writes, and a design that consults it on every request is down whenever it is.
  • "More members are safer." Five survive two failures and seven survive three, while every write waits for one more acknowledgement; four survive no more failures than three.
  • "Keep the data in etcd, it is consistent." One leader orders every write. It is built for small facts that rarely change, and per-request data makes it the bottleneck of the design.
  • "An election guarantees one leader at any moment." It guarantees one per term. An old leader that has not yet heard of the new term can still act, which is why the next lesson fences it.
  • "Consensus needs synchronised clocks." Raft uses timers only to decide when to hold an election. Its safety never depends on a clock.
Why It Matters
  • Draw the coordination service beside the data path, and say how often the design asks it: per block, per lease, per configuration change, never per request.
  • Run three members across three zones by default, five when two may fail at once, and never an even number.
  • Give the failover time as the election timeout plus one election, and say what the system does during it.
  • Keep the members in one region unless the business must survive losing a region, and say the 70 milliseconds per write that crossing regions costs.
RelatedWhy Agreement Is Hard what a majority buysLeases, Locks and Fencing the old leader that still actsLeaders, Followers and Everyone replication of the dataPostgreSQL Deep Dive failover over a consensus store

Knowledge Check

A coordination cluster must keep working through any two members failing at once. What is the smallest number of members it needs?

  • Four, because a majority of four is two, and two members can still vote
  • Five, because a majority of three is still left after two have failed
  • Three, because the two survivors of any failure still form a majority
  • Six, because an even count lets the cluster split evenly in a partition

Why can a Raft cluster never have two leaders in the same term?

  • Because the leader with the lower node ID steps down when the two meet
  • Because every node checks the wall clock before it accepts a leader
  • Because each node votes at most once per term, and a win takes a majority
  • Because the coordination service assigns each term to exactly one node

A partition leaves the old leader with one follower out of five members. What can that side still do?

  • Commit writes from its own clients, and merge them when the cut heals
  • Elect a new leader of its own, so both sides of the cut keep running
  • Nothing in the log, though the old leader may still act on a stale belief
  • Serve reads of the newest data, because it still holds a recent log

Which of these belongs in a coordination service such as etcd?

  • Which node currently leads each partition of the data store
  • Every new short link, so that no two links are ever stored twice
  • Each user's session, so that any server can read it consistently
  • The click counts per link, which must be accurate on the dashboard

A coordination cluster's members sit in US east, US west and western Europe, with the leader in US east. What does each write cost?

  • About 85 ms, the round trip to the member in western Europe
  • About 155 ms, a round trip to each of the two other regions
  • A few milliseconds, because only the leader's own disk must confirm
  • About 70 ms, the round trip to the follower in US west

You got correct