What it is
Consensus is the problem of getting a group of nodes to agree on a single value (or, repeated, on a sequence of values such as a log of commands) even though messages can be delayed, reordered or lost and nodes can crash. A correct protocol guarantees:
- Agreement: no two nodes decide different values.
- Validity: the decided value was proposed by someone (a protocol that always decides
0is useless). - Termination: every non-faulty node eventually decides.
The first two are safety properties, the third is a liveness property. Repeated consensus over a log is called state machine replication: if every replica applies the same commands in the same order, every replica ends in the same state.
Why it exists
Replicating data is easy while nothing goes wrong. The trouble starts when two clients write at once, when a node crashes in the middle of an update, or when the network splits the cluster. Without a protocol, replicas apply writes in whatever order they arrive and diverge; a naive failover can produce two primaries that both accept writes (split brain). Consensus is the building block that makes "one leader", "one committed order of writes" and "one owner of this lock" well-defined in a system where any part can fail.
How it works
Nodes cannot tell slow from dead. In an asynchronous network there is no bound on message delay. A node waiting for a reply cannot know whether the peer crashed, is paused, or whether the message or its reply is stuck. Any timeout is a guess, and a protocol must stay correct when the guess is wrong.
Quorums. Instead of waiting for every node (which stops progress when one is down), a decision needs acknowledgements from a majority, ⌊n/2⌋ + 1 nodes. With n = 2f + 1 nodes, the cluster keeps deciding with f nodes down: 3 nodes tolerate 1 failure, 5 tolerate 2, 7 tolerate 3. Even sizes add no tolerance: 4 nodes need 3 acknowledgements and still tolerate only 1 failure.
Overlap is the key. Any two majorities of the same group share at least one node. A value committed by one majority is therefore known to at least one member of every later majority. Protocols use that: before a new leader proposes anything it collects state from a majority and must respect what was already accepted (Paxos's prepare phase, Raft's rule that only a candidate with an up-to-date log can win).
Rounds. Proposals carry a monotonically increasing number: a ballot in Paxos, a term in Raft, an epoch in ZAB. A node that has seen round 6 rejects messages from round 5. That stops an old leader, after a pause or a partition, from overriding a newer one.
Leaders. Two nodes proposing at the same time can keep pre-empting each other with ever-higher rounds. Practical protocols (Multi-Paxos, Raft, ZAB) elect a single stable leader that sequences all proposals; elections only happen when the leader seems to have failed, and randomised timeouts make repeated collisions unlikely.
Safety, liveness and FLP. Fischer, Lynch and Paterson proved in 1985 that in a fully asynchronous system where even one process may crash, no deterministic algorithm can guarantee consensus terminates. Real protocols therefore guarantee safety unconditionally and liveness only under partial synchrony: once messages are delivered within some (unknown) bound for long enough, they decide. During a partition, the majority side keeps working and the minority side stops; nobody ever decides two different things.
When to use it
- Choosing a single leader, primary or owner (database failover, scheduler leader, shard ownership).
- Storing small, critical metadata that everything else depends on: cluster membership, configuration, which node owns which partition.
- Distributed locks and leases where correctness matters, combined with fencing tokens.
- Replicating a database's write log when confirmed writes must survive any minority of failures (CockroachDB, TiKV, Spanner, etcd).
When not to
- For bulk data paths with very high write rates where losing the most recent writes on failover is acceptable: asynchronous replication is cheaper.
- When conflicts can be merged after the fact (shopping carts, counters, collaborative editing): leaderless replication with CRDTs or version vectors stays available during partitions.
- Inside a single machine, or when one database's transactions already give you the guarantee.
- Do not implement a consensus protocol yourself for production use; use a proven implementation or service.
Trade-offs
| Choice | You gain | You pay |
|---|---|---|
| Majority quorum (consensus) | Survives f of 2f + 1 failures with no lost confirmed writes and no split brain | One round trip to a majority per decision; the minority side of a partition is unavailable |
| Wait for all replicas | Every copy always has every confirmed write | Any single failure stops writes |
| Asynchronous replication | Lowest write latency; writes continue with replicas down | Confirmed writes can be lost on failover; failover needs an external decision |
| Larger cluster (5 or 7) | Tolerates more failures | Slower commits (larger majority, often across more zones) and more replication traffic |
| Cross-region cluster | Survives a region outage | Every write pays a cross-region round trip |
Alternatives
- Primary–backup with an external arbiter: a single primary with async or semi-sync replicas, and failover decided by a consensus-backed service (for example a Kubernetes lease or an etcd key). The data path avoids consensus; only the leadership decision uses it.
- Leaderless quorums (Dynamo-style, with R + W > N): reads and writes contact overlapping quorums, but without consensus concurrent writes can conflict and need resolution.
- CRDTs: data types whose concurrent updates always merge deterministically; no agreement needed, but only for operations that commute.
- Byzantine fault-tolerant protocols (PBFT and successors): tolerate nodes that lie, at the cost of 3f + 1 nodes and more messages. Rarely needed inside one organisation.
Failure modes
- Losing a majority: with 3 of 5 nodes down (or unreachable) the cluster cannot commit anything. Spread nodes across failure domains so one zone outage cannot take a majority.
- Slow disks or networks: every commit waits for the median-fastest majority; one slow node is masked, two slow nodes in a five-node cluster slow everything.
- Election storms: timeouts set too close to normal latency make followers start elections during GC pauses or load spikes, and every election pauses writes.
- Unsafe membership changes: adding or removing several nodes at once can create two disjoint majorities (old and new configuration). Protocols change membership one node at a time or with joint consensus.
- Stale reads from an old leader: a leader cut off from the majority may not know it was replaced; reads served from it without checking (ReadIndex, leases) can be stale.
In production
Run three nodes for most clusters and five when you need to survive two failures or one zone plus maintenance. Put them in different zones within a region unless you need regional fault tolerance and can afford the latency. Monitor leader changes, proposal commit latency (which includes the disk sync on a majority) and follower lag. etcd and ZooKeeper are sized for metadata (etcd's default storage quota is 2 GiB), not for application data. Remember that the client side matters too: a client that retries a write after a timeout may apply it twice unless requests are idempotent or deduplicated.
Common misconceptions
- "Consensus means all nodes agree before anything is committed." A majority is enough; the rest catch up later.
- "FLP says consensus is impossible." It says termination cannot be guaranteed in a fully asynchronous model. Safety can be, and practical systems terminate whenever the network is reasonably timely.
- "Four nodes are safer than three." They tolerate the same single failure and add one more machine that can fail.
- "A partition makes both sides keep working, then they reconcile." That is what leaderless or async systems do. A consensus system stops the minority side instead.
- "Consensus makes my application exactly-once." It orders the commands it receives; duplicate client retries are still duplicates unless the application deduplicates them.
Interview angle
When a design needs a single leader, unique ownership or a strongly consistent store, say that you would rely on a consensus-backed component (etcd, ZooKeeper, a Raft-replicated database) rather than invent one. Explain the majority arithmetic (2f + 1), what happens during a partition (the minority side is unavailable), and the cost (a majority round trip per write, so keep the cluster in one region unless you need regional survival). Mentioning safety versus liveness and the role of timeouts (FLP) shows you understand why these systems behave as they do.
Model assumptions
Most of this lesson is a scripted illustration: the packets are placed by hand to show who sends what to whom and in which order, and node state appears as annotations. The simulator has no voting or ballot model. Two steps are simulated with real writes (40 per second) through the engine's synchronous replication: in "Wait for all" the primary answers a write only after every replica has applied it, so a crashed replica fails every write; in "Majority" it answers after one of its two peers has applied it (itself plus one peer is 2 of 3), so writes keep succeeding with one node down and fail once a second node crashes. The leader is fixed in both; there is no election. Latencies are illustrative (5 ms between app and node, 10 ms between nodes, about 1 ms of processing; a few links in the duel are faster or slower to make the race visible); real intra-region round trips are usually well under a millisecond plus a disk sync. The duel uses Paxos-style prepare and accept phases to show pre-emption; Raft avoids most duels with randomised election timeouts.