Search DistillSys

Find a concept

Type at least two characters to search lessons, designs, papers, and interview prep.

Consensusintermediate6 min read

Leader Election

Select one current coordinator and prevent stale leaders from continuing to act.

2–5 minute refresher
Mental model

Elect authority → attach an epoch → fence every side effect

30second
refresher
Leader election lets a group choose one node to coordinate writes, scheduling, or ownership. A safe design also proves freshness with a term, epoch, or fencing token so an isolated former leader cannot keep acting.

What problem does it solve?

Some operations need a single authority to serialize decisions or avoid duplicate work. Static ownership fails when the owner crashes, while informal failover risks two active leaders.

How it works

  1. Followers monitor heartbeats or a lease deadline.
  2. A candidate advances the term and asks a quorum for votes.
  3. Voters grant at most one vote per term under the protocol rules.
  4. The winner publishes its term with every command.
  5. Resources accept only commands with a term at least as new as the last one observed.
Decision guide

Key trade-offs

ChoiceWhat you gainWhat it costs
Short timeoutFast failoverFalse elections during delay
Long timeoutStable leadershipLonger recovery
Lease leaderFast local decisionsClock and pause assumptions
What happens if?

The old leader is paused, not dead

After a long process pause it resumes with stale authority. Without fencing it can overwrite the new leader's work. A monotonically increasing token lets storage reject that command.

Where it appears

  • Raft
  • ZooKeeper recipes
  • etcd elections
  • Kubernetes controllers
Senior interview modeWhy is a distributed lock without a fencing token unsafe?Show answer
The holder may pause past expiry and later resume. Another client can acquire the lock meanwhile, so both can act unless the protected resource rejects the stale holder's lower token.
#leadership#leases#epochs#fencing