Leader Election Patterns Lab (Interactive)
Bully versus ZooKeeper-style elections, herd versus watch, counted message by message. Crash nodes in an eight-member cluster and compare message cost per election across Bully, ephemeral-sequential ZAB-style election, and the thundering-herd wake-up.
Bully vs ZooKeeper Election Arena
Kill leaders in an 8-node cluster and compare who pays for re-election: every node with O(N) chatter, or one lucky watcher.
Crash the 👑 node to trigger a re-election and watch the message accounting.
Kafka elects its Controller, Kubernetes controllers lease via etcd, and Patroni guards PostgreSQL primaries with exactly these patterns: unique ID bullying is simple but O(N) per election and wake-up hungry; ephemeral sequential nodes turn failover into one delete plus one watch event — which is why 10,000 standby workers can wait behind 1 leader without stampeding.
How It Works Under the Hood
Every consensus group needs a leader, and election styles trade chatter against coordination. The Bully algorithm messages every higher-id node and the highest survivor claims victory — simple, O(N) per election, and useless without leases to fence zombie leaders. ZooKeeper instead has every contender create an ephemeral sequential znode and watch its immediate predecessor; lowest sequence wins, and after a crash only one successor wakes, making failover cheap and the ephemeral session itself the health proof. This lab counts messages and wake-ups per election under each scheme so the design difference stops being folklore.
Core Architectural Principles
- Bully fans out to every higher node and broadcasts the result — message cost per election counted.
- Ephemeral-sequential predecessor watch wakes one waiter instead of the whole herd.
- Lease duration control frames how long a dead leader’s title survives before renegotiation.
When proposing a coordinator, say which election you delegate and why: “we rely on ZK/etcd ephemeral-sequential watches because hand-rolled Bully needs leases and heartbeat fencing.” Mention split-vote risk with N-of-M pings and prefer delegated elections for anything already storing state there.
Bully is trivial to implement but chatty; watch-based election scales and self-heals but binds you to a coordination service.