Sub topic 12
Quorum and leader election
This chapter assumes you know how primary and replica copies of data work, and that you have seen health checks and timeouts. It covers how a group of nodes agrees on one leader without a single coordinator that could itself fail.
Why a majority
A quorum is the smallest number of nodes that must agree before a decision counts. Most systems define it as a majority: N // 2 + 1 out of N voting nodes.
Two groups that each hold a majority must share at least one node. That node votes only once per decision. So two separate groups cannot both elect a leader, even if the network splits them.
| Voting nodes | Quorum | Failures tolerated |
|---|---|---|
| 3 | 2 | 1 |
| 4 | 3 | 1 |
| 5 | 3 | 2 |
| 7 | 4 | 3 |
Four nodes tolerate no more failures than three, and they need a larger quorum. This is why clusters usually have an odd number of voters.
def quorum(n):
return n // 2 + 1
def tolerated_failures(n):
return n - quorum(n)
def wins_election(votes, n):
return votes >= quorum(n)
for n in (3, 4, 5, 7):
print(n, quorum(n), tolerated_failures(n))
print(wins_election(2, 5)) # False
print(wins_election(3, 5)) # TrueHow a leader is elected
Most consensus protocols, such as Raft and Zab, follow the same pattern:
- The leader sends periodic heartbeats to the followers.
- A follower that hears nothing for longer than its election timeout becomes a candidate.
- The candidate increases the term (also called an epoch), a counter that grows with each election. It votes for itself and asks the others for votes.
- Each node grants at most one vote per term.
- A candidate that gathers a quorum becomes leader for that term.
Each node picks its election timeout at random, for example between 150 and 300 ms. Without that randomness, several followers would time out together, split the votes, and nobody would reach a majority.
If node A comes back, it sees term 2 in B's heartbeat and steps down. A higher term always wins. The term is the number that the next chapter uses as a fencing token.
What quorum costs you
A cluster that loses its majority stops accepting writes and elections, even though some nodes are still running. This is intended. The minority side stays unavailable so the majority side stays consistent.
Writes also need acknowledgment from a quorum. This ties back to replication. A write that reached a majority survives any single leader change, because every new leader must have gathered votes from at least one node that holds that write.
Common errors
- Voters in one fault domain. Five nodes in one rack lose quorum when the rack loses power. Spread voters across at least three zones.
- Even-sized clusters. A 4-node cluster split 2/2 has no majority on either side, and neither side can elect a leader.
- Timeouts too short. A brief network pause triggers elections that interrupt service. This is the same speed against false positives trade-off as health checks.
- Reading from a stale leader. A deposed leader may still believe it leads for a short time. Serving reads or writes from it needs the fencing described next.
- Hand-rolling consensus. Use a proven implementation such as etcd, ZooKeeper, or Consul rather than writing your own election logic.
Exercise
A cluster has 5 voting nodes. A network partition splits them into groups of 3 and 2. Which group can elect a leader, and how many more node failures can the cluster tolerate before losing its quorum in the healthy group?
Show answer
The group of 3 can elect a leader, because the quorum for 5 nodes is 3. Once that group holds a leader, it can tolerate no further failures. If one more node fails, only 2 voters remain and quorum is lost.