Consensus, Quorums & Raft
Deriving majority quorums from first principles, how Raft uses monotonic terms and log matching to prevent split-brain, and why clusters run 3 or 5 nodes — never 2 or 4.
A single database server is easy to reason about, but it is a SPOF. The obvious first instinct is to add a second server (primary + backup) so that if one dies, the other takes over.
Now cut the network cable between Server A and Server B. Both servers are still alive, and neither can tell whether the other crashed or is merely unreachable:
- If both promote themselves to leader, clients write conflicting updates to each side — classic split-brain.
- If neither promotes itself to avoid split-brain, then losing either server halts the entire system — worse availability than a single server.
With 2 nodes, safe automatic failover is mathematically impossible.
First principles: the majority quorum
To break the tie safely, require every leader election and every committed write
to win approval from a strict majority — a quorum of
floor(N / 2) + 1 nodes.
Why does a strict majority prevent split-brain? By the pigeonhole principle, any two majorities of N nodes must intersect in at least one node. That overlapping node acts as a witness: it cannot vote for two different leaders in the same election, and it always carries the latest committed log entries into the next leader’s term.
Test this live in the 5-node Raft cluster below. Kill the leader to watch a
term + 1 election, or trigger a 3 | 2 partition and submit writes to see
why the minority side refuses commits.
interactive · kill the leader or split the network 3 | 2, then submit writes to watch quorum safety
term 1 · leader N1 · no split-braincommitted x=2 on majority (3/5) · minority refused write
How Raft actually works in four rules
Raft decomposes distributed consensus into four clean invariants:
- Monotonic terms: time is sliced into numbered elections (
Term 1,Term 2, …). Every RPC carries the sender’s term. If any node sees a higher term, it immediately updates its term and steps down to follower — making stale leaders self-healing. - Randomized election timeouts: followers expect periodic heartbeats from
the leader. If a follower hears nothing for a random window (e.g.
150–300ms), it increments its term, becomes a candidate, votes for itself, and asks peers for votes. Randomization ensures one candidate almost always wins before another times out. - Replicated write-ahead log: the leader appends every client command to
its local WAL and sends
AppendEntriesRPCs to followers. Once a majority of nodes persist the entry to disk, the leader marks it committed and replies to the client. - Log matching (election restriction): a candidate can only win a node’s vote if the candidate’s log is at least as up-to-date as the voter’s. Combined with majority overlap, this guarantees every newly elected leader already has every previously committed entry.
floor(N/2) + 1
majority quorum required to commit
1 leader / term
election safety invariant
2F + 1 nodes
required to survive F crash failures
Why clusters use 3 or 5 nodes (never 2 or 4)
Because a cluster of size N can only survive floor((N - 1) / 2) node
failures before losing its majority quorum, even-sized clusters pay extra
hardware and replication latency for zero extra fault tolerance:
| Cluster size (N) | Quorum needed | Failures tolerated | Verdict |
|---|---|---|---|
| 1 | 1 | 0 | No fault tolerance (SPOF) |
| 2 | 2 | 0 | Worse than 1 — losing either node stops writes |
| 3 | 2 | 1 | Standard small production cluster (survives 1 crash or rolling restart) |
| 4 | 3 | 1 | Strictly worse than 3 — same 1-failure tolerance, larger quorum |
| 5 | 3 | 2 | Standard HA cluster (survives 1 planned maintenance + 1 unexpected crash) |
| 7 | 4 | 3 | Rare; higher write latency and election chatter for diminishing returns |
note
Consensus is not free: every write must cross the network and flush to disk on a majority of nodes before returning. Production systems do not run one giant Raft group for terabytes of user data — instead, they shard data into thousands of small Raft groups (Multi-Raft, as in CockroachDB and TiKV) or keep Raft strictly for small control-plane metadata (etcd, ZooKeeper, KRaft).
Where this shows up in system design
- etcd / Kubernetes control planeStores all cluster state, pod schedules, and leader leases across a 3- or 5-node Raft cluster with monotonic revisions as fencing tokens.
- CockroachDB / TiKV / SpannerSplits key ranges into 64 MB shards, each backed by its own 3- or 5-replica Multi-Raft (or Paxos) consensus group.
- ZooKeeper / Kafka KRaftElects partition leaders, tracks in-sync replicas, and stores topic metadata without split-brain.
- Distributed config & metadata storesConsul and Chubby use consensus to serve strongly consistent service discovery, feature flags, and shard routing tables.
Check yourself: system design scenarios
interactive · test your consensus and quorum intuition
1. Sizing an etcd cluster for Kubernetes
survive 2 concurrent outages · minimize write latency · Raft quorum
Your team wants to survive a planned node upgrade AND an unexpected hardware failure at the same time without losing control-plane writes.
Select an option to check the trade-off.
2. Network partition isolates the old leader
3 | 2 partition · strong consistency · no split-brain
In a 5-node Raft cluster, a switch fails and isolates the current leader N1 with N2 (2 nodes) away from N3, N4, and N5 (3 nodes). A client sends a write to N1.
Select an option to check the trade-off.
3. Scaling a globally distributed SQL database to 50 TB
50 TB data · 100k writes/s · strong consistency
You are designing a strongly consistent transactional database storing 50 TB of user rows at 100k writes/sec across 30 machines.
Select an option to check the trade-off.
SPOFreliability
Single Point of Failure: any component whose failure stops the whole system because there is no independent fallback.
split-brainconsensus
A failure where a network partition tricks two nodes into believing they are both the active leader and accepting conflicting writes.
quorumreplication
Minimum replicas that must acknowledge a write (W) or read (R); W + R > N guarantees overlap with the latest write.
WALstorage
Write-Ahead Log: an append-only disk log written before mutating in-memory structures so crashes never lose committed writes.