We use analytics cookies.We use cookies to analyze site traffic and improve your experience.
Loading...
3 system design questions lean on this idea. Each walks through the full answer.
System Design Masterclass · Distributed Systems Core
Free preview: the first 2 parts of this 11-part lesson.
Already bought the course? Sign in to read the whole lesson.
this lesson is part of
System Design Masterclass
Unlock this lesson and the 751 other paid lessons
$25 once, by PayPal
See the System Design courseread the rest of this lesson in
System Design Masterclass
Unlock this lesson and the 751 other paid lessons
$25 once, by PayPal
See the System Design coursein Distributed Systems Core
This lesson is part of the full System Design Masterclass. Unlock it along with 769 other lessons for a one-time payment.
One-time payment by card or PayPal
Get Lifetime AccessOne-time payment: UPI, card or netbanking
Get Lifetime Access18 Foundation lessons are free, no payment needed. Try them now
It is easy and good
“It is really nice and good Please join the course”
Imagine a group of five friends trying to order pizza. No one is "in charge." Everyone shouts their preferences simultaneously. One wants pepperoni, another wants margherita, two want Hawaiian, and the last person is lactose intolerant. Without someone to make a final decision, they'll argue for 45 minutes and end up ordering nothing.
This is exactly the problem distributed systems face. When you have multiple nodes that need to coordinate, deciding which writes to commit, which replica is authoritative, which node handles a particular request, someone has to make the final call. That "someone" is the leader.
But here's the catch: there's no boss. No predefined hierarchy. The nodes are peers. They have to elect a leader from among themselves, using only network messages. And they have to do it correctly, if two nodes both think they're the leader, you get split-brain. If no node thinks it's the leader, the system freezes.
Leader election is a solved problem. Raft, ZooKeeper's ZAB protocol, and etcd all do it reliably. But understanding the mechanics matters because when your cluster stops processing messages or your Kubernetes API server becomes unresponsive, the root cause is often a leader election that failed, took too long, or resulted in the wrong outcome.
There are several approaches to leader election, each with different trade-offs:
The Bully Algorithm, the node with the highest ID wins. When a node detects the leader is down, it sends an election message to all nodes with higher IDs. If none respond, it declares itself the leader. If a higher-ID node responds, that node takes over the election. Simple, but the highest-ID node always wins, even if it's slow or poorly connected.
Ring-based Election, nodes are arranged in a logical ring. When the leader fails, the detecting node passes an election message around the ring. Each node adds its ID to the message. When the message returns to the initiator, the node with the highest ID in the message becomes the leader. More fair than bully, but slow, you have to wait for the message to traverse the entire ring.
Election, nodes have randomized election timeouts. When a follower doesn't hear from the leader within its timeout, it increments its term, votes for itself, and asks others for votes. The first candidate to get a majority of votes wins. Randomized timeouts prevent election storms. This is what most modern systems use.
| Algorithm | Complexity | Fault Tolerance | When to Use |
|---|---|---|---|
| Bully | O(n^2) messages | Tolerates crashes | Small clusters, simple implementations |
| Ring | O(n) messages | Requires ring maintenance | When fairness matters more than speed |
| Raft | O(n) messages | Tolerates minority failures | Production systems, databases, coordination |
| ZAB | O(n) messages | Tolerates minority failures | ZooKeeper, ordered broadcast needed |