System design fundamentals learning path Level 3 This level · 1 of 13
Hard system-design fundamentals

How does leader election work, and how do you prevent split-brain?

The 30 second answer

Leader election picks one node to do a job, like accepting writes or running a scheduler. The safe ways are consensus, like Raft, where a leader needs votes from a majority, or a lease in a strongly consistent store that the leader must keep renewing. The danger is an old leader that was paused and still thinks it leads, so every write carries a fencing token, an increasing number, and storage rejects writes with an older token.

Could you say this in an interview?

Raft, the consensus answer. In Raft, time is divided into terms. A follower that hears no heartbeat within its election timeout becomes a candidate, increments the term and asks for votes; whoever gets votes from a majority becomes leader for that term. Raft chooses election timeouts randomly from a fixed interval, for example 150 to 300 ms in the paper, so split votes are rare and resolved quickly. Because each server votes at most once per term and two majorities always overlap, two leaders cannot both win in the same term.

Leases, the practical answer. The AWS Builders' Library says leases are the most widely used leader election mechanism at Amazon: one strongly consistent database stores the current leader, and the leader must heartbeat to keep the lease; if it stops, others can take over. They point out leases depend only on local elapsed time, not synchronized wall clocks. Kubernetes uses Lease objects for component leader election, so only one instance of, for example, the controller manager is active.

Split-brain and fencing tokens. A lease does not stop an old leader from acting. Martin Kleppmann's example: a client holding a lock is paused, for example by a long garbage collection pause, its lease expires, another client takes over, and then the first one wakes up and writes. The fix is a fencing token: a number that increases every time the lock or leadership is granted, sent with every write, with the storage service rejecting tokens older than the newest it has seen. ZooKeeper's zxid or znode version can serve as the token.

Ask whether you need a leader at all. The same AWS article notes that idempotent APIs, optimistic locking or a workflow service can often make a single leader unnecessary, and avoid its failure modes.

Common mistakes interviewers listen for.

  • Building election on a Redis key without fencing; Kleppmann argues Redlock cannot produce fencing tokens.
  • Assuming clocks are synchronized.
  • Running an even number of consensus nodes; a majority of 4 is 3, so 4 nodes tolerate no more failures than 3.
Sources (5)
  1. Raft consensus algorithmraft.github.io
  2. Ongaro and Ousterhout: In Search of an Understandable Consensus Algorithm (Raft paper)raft.github.io
  3. AWS Builders' Library: Leader election in distributed systemsaws.amazon.com
  4. Kubernetes: Leaseskubernetes.io
  5. Martin Kleppmann: How to do distributed lockingmartin.kleppmann.com
Previous
Next