Consensus Algorithms
How Raft and Paxos let a group of nodes agree on an ordered log, what a majority actually buys you, and why leader election is the part that shows up in incidents.
Consensus lets a group of nodes agree on a sequence of operations despite crashes and network delays. It is the mechanism behind CockroachDB and YugabyteDB ranges, MongoDB elections, etcd, ZooKeeper and ClickHouse Keeper.
What Raft does
Raft splits the problem into leader election, log replication and safety.
- One node is leader for a term. All writes go through it.
- The leader appends an entry to its log and sends
AppendEntriesto followers. - When a majority — including the leader — has persisted the entry, it is committed and applied to the state machine.
- Followers that fall behind are caught up by the leader; conflicting entries are overwritten.
The majority requirement is what makes it safe. Any two majorities of the same group overlap in at least one node, so a newly elected leader is guaranteed to have seen every committed entry.
What a majority means for availability
Even-numbered groups are wasteful: they cost an extra node without tolerating an extra failure. Larger groups tolerate more failures but make every commit wait for more nodes, so latency grows.
Leader Election
When followers stop receiving heartbeats within the election timeout, one becomes a candidate, increments the term and requests votes. A node grants its vote only if the candidate's log is at least as up to date as its own, which prevents a node missing committed entries from winning.
Two properties matter operationally:
- Unavailability window. Writes stop between leader failure and new leader election — typically the election timeout plus a round trip. Timeouts are tunable, and tuning them down increases the risk of spurious elections when the network hiccups.
- Election storms. If timeouts are too aggressive or the network is unstable, nodes repeatedly elect and depose leaders, and no work gets done. Randomised timeouts reduce split votes, but persistent instability means the timeout is wrong for the network.
Raft versus Paxos in practice
Multi-Paxos and Raft solve the same problem with the same majority guarantees. Raft constrains the design — a strong leader, an append-only log, no holes — which makes it easier to implement correctly and easier to reason about during an incident. Most systems built after 2014 chose Raft for that reason.
What consensus does not solve
- Throughput. Every write is serialised through one leader per group. Systems scale by having many independent groups (ranges, tablets, shards), not by making one group faster.
- Latency across regions. A commit requires a majority, so a group spanning continents pays the inter-region round trip on every write. Place replicas according to where writes originate. See Geographic Distribution.
- Correctness of your data model. Consensus guarantees agreement on an order of operations, not that the operations were the right ones.