Full answer
Why one leader
Some jobs must run on exactly one instance: a scheduler that assigns work, a primary database, a process that sends invoices. Running two at once (split brain) causes duplicate or conflicting actions.
Recipe with etcd
- Each candidate creates a lease with a TTL (for example 10 seconds) and tries a transaction: "create key
/election/billingwith my ID and this lease, only if the key does not exist". - The one that succeeds is the leader; it renews the lease every few seconds.
- Others watch the key. When it disappears (lease expired or leader resigned), they try again.
- The key's creation revision is a monotonically increasing number that can serve as a fencing token.
ZooKeeper's recipe: every candidate creates an ephemeral sequential node under /election; the lowest number is leader, and each candidate watches only the node just before it, which avoids a thundering herd when the leader goes.
The coordination service itself runs Raft (etcd) or Zab (ZooKeeper), so its view is consistent across its own replicas.
The dangerous case
The leader pauses for 15 seconds (a long garbage collection pause or a network partition). Its lease expires and a new leader is elected. The old leader wakes up still believing it is leader and continues writing.
Defences:
- Leases with margin. The leader stops acting before its lease would expire by its own clock, minus a safety margin for clock drift. This helps but cannot cover every pause.
- Fencing tokens. Every leader term has a number that increases. The leader attaches it to each write; the storage system remembers the highest token seen and rejects lower ones. The old leader's writes then fail even if it never notices.
- Checking leadership on each action through the coordination service adds latency but narrows the window.
Other approaches
- Raft and Paxos build election into the data system itself (for example a database's replicas elect their own primary), so no external service is needed.
- The bully algorithm (highest ID wins) is simple but unsafe under partitions without a quorum.
Trade-offs
Short leases give fast failover but risk spurious elections during brief network blips; long leases mean longer outages when a leader truly dies. 5 to 15 seconds is common.
By level
Fresher: Explain that servers race to write one shared key and the winner becomes leader until it stops renewing. A fresher should explain why two leaders would be harmful.
Mid: Describe etcd and ZooKeeper recipes, watches, lease renewal and the pause scenario. Introduce fencing tokens.
Senior: Discuss safety margins and clock drift, fencing with real storage systems, quorum loss in the coordination service, and embedded Raft as an alternative. Expect precise failure timelines.
Key points
- Candidates race to create one key or the lowest sequential ephemeral node
- The leader renews a lease; others watch and retry
- An old leader may not know it lost leadership
- Fencing tokens let storage reject stale leaders' writes
- Lease length trades failover speed against false elections
Common mistakes
- Trusting the old leader to notice it lost the lease
- Electing a leader without a quorum-based service
- Every candidate watching the leader node and stampeding on change
- Very short leases that flap during network blips
Follow-ups
- How would you run leader election without an external coordination service?
- What happens if the coordination service loses quorum?
- How do fencing tokens work with a storage system that cannot check them?
Tips
- Walk through the GC pause scenario
- Show the conditional create step
- Explain fencing in one sentence
Evaluation rubric
- Correct election recipe
- Explains leases and renewal
- Handles split brain with fencing
- Discusses lease trade-offs and alternatives