Why elect a leader
Many systems need one node to coordinate, sequence writes, or assign work. Leader election chooses exactly one such node from a group and lets the group agree on the choice, even after the old leader fails.
Classic algorithms
- Bully algorithm the node with the highest id wins; a node that notices the leader is gone challenges all higher ids, and if none respond it declares itself leader
- Ring algorithm nodes pass an election message around a logical ring, accumulating ids, and the highest id becomes leader
These assume reliable detection of failure, which is itself hard.
Modern coordination services
In practice many systems delegate election to a coordination service:
- ZooKeeper uses ephemeral sequential nodes; the lowest sequence number is leader, and its node vanishes if it disconnects
- etcd uses leases and compare and swap on a key
- Raft elects a leader as part of its consensus protocol
What makes it correct
- Safety at most one leader is recognized at a time
- Liveness a new leader is eventually elected after a failure
- Fencing ensures a deposed leader cannot keep acting
Key idea
Leader election picks one coordinator the group agrees on; classic schemes like bully and ring use node ids, while modern systems lean on ZooKeeper etcd or Raft with fencing to stay safe.