← Back to System Design, page 2

system-design-0033 · Hard senior

How does leader election and consensus work in a system like Raft, and why do clusters use an odd number of nodes?

Raft keeps a replicated log consistent across nodes. One leader is elected for a numbered term by majority vote; it appends client commands to its log and replicates them, and an entry is committed once a majority stores it. A majority is needed for both election and commit, so two leaders cannot both make progress. Odd sizes are used because 4 nodes tolerate no more failures than 3.

Full answer

Why consensus

Some decisions must be agreed by all nodes even with crashes and network delays: who is the leader, which configuration is current, who holds a lock. Consensus protocols such as Raft and Paxos solve this. etcd (behind Kubernetes) and Consul use Raft; ZooKeeper uses a similar protocol (ZAB).

Raft roles and terms

Each node is a follower, candidate or leader. Time is split into terms numbered 1, 2, 3... At most one leader per term.

Leader election

  1. The leader sends regular heartbeats.
  2. If a follower hears nothing for a randomised election timeout (for example 150 to 300 ms), it becomes a candidate, increments the term, votes for itself and asks others for votes.
  3. Each node votes for at most one candidate per term, and only for a candidate whose log is at least as up to date as its own.
  4. A candidate with votes from a majority becomes leader.

Randomised timeouts make it unlikely that two candidates split the vote repeatedly.

Log replication

  1. A client sends a command to the leader.
  2. The leader appends it to its log and sends it to followers.
  3. Once a majority (including the leader) has stored it, the entry is committed; the leader applies it and replies.
  4. Followers apply committed entries in the same order, so every node's state machine ends up identical.

If a follower's log differs, the leader overwrites the follower's uncommitted conflicting entries with its own.

Why majorities prevent split brain

Any two majorities of the same cluster share at least one node. If a network partition splits 5 nodes into 3 and 2, only the side with 3 can elect a leader or commit. The 2-node side cannot, even if it contains the old leader; that old leader cannot get acknowledgements and stops making progress. Clients writing to it will time out.

Why odd numbers

A cluster of N tolerates floor((N - 1) / 2) failures:

NodesMajorityFailures tolerated
321
431
532
642
743

A 4th node adds cost and replication work without extra tolerance. Most clusters use 3 or 5; larger clusters slow down commits because more acknowledgements travel per write.

Practical points

  • Every write goes through the leader and needs a majority round trip, so consensus stores are for small, critical data (configuration, membership, locks), not bulk data.
  • Linearizable reads need the leader to confirm it is still leader (a read index round or a time-based lease).
  • Spread nodes across failure zones: 3 nodes in 3 availability zones survive one zone failure.

By level

Fresher: Explain that a cluster picks one leader by majority vote and that a write is safe once most nodes have it. Explaining why a cluster of three survives one failure is enough.

Mid: Walk through election, terms, log replication and commit, and explain why 3 or 5 nodes are typical. Expect the commit rule and the role of terms.

Senior: Discuss partitions and stale leaders, linearizable reads with read index or leases, membership changes, zone placement, write latency across regions and when to use an existing consensus service instead of building one. It should explain what an old leader in a minority partition can and cannot do.

Key points

  • Raft elects one leader per term by majority vote with randomised timeouts
  • Entries commit once stored on a majority
  • Overlapping majorities prevent two leaders from both committing
  • N nodes tolerate floor((N-1)/2) failures, so odd sizes are efficient
  • Consensus stores hold small critical data, not bulk data

Common mistakes

  • Thinking the minority side keeps accepting writes
  • Saying 4 nodes tolerate 2 failures
  • Using a consensus store as a general high-throughput database
  • Forgetting that reads also need care to be linearizable

Follow-ups

  • What happens to an old leader stuck in a minority partition?
  • How would you place a 5-node cluster across 3 zones?
  • How does etcd serve linearizable reads?

Tips

  • Separate election from replication in your explanation
  • Draw the 3-versus-2 partition
  • Show the failures tolerated table for 3, 4 and 5 nodes

Evaluation rubric

  • Correct election mechanics including terms and vote rules
  • Correct commit rule
  • Explains split-brain prevention with overlapping majorities
  • Correct odd-number reasoning