Consensus and Raft

Consensus is the problem of getting a group of unreliable machines to agree on a single value despite crashes, delays, and lost messages. It sounds narrow, but it's the hidden foundation under leader election, distributed locks, configuration, and every "exactly one node is in charge" guarantee. Raft is the algorithm that finally made it understandable.

Almost every hard problem in the earlier posts reduced to the same underlying need: the nodes must agree. Who is the leader? In what order do operations apply? Is this partition still allowed to serve writes? Consensus is that problem stated precisely — getting multiple nodes to agree on a value such that they all decide the same thing and never un-decide it — and it must work despite the partial failure that defines the whole field. This post explains what consensus guarantees and how Raft achieves it.

What consensus must guarantee

A consensus algorithm lets a set of nodes agree on a value (or a sequence of values) with these properties, even when some nodes crash and messages are lost or delayed:

The reason consensus is hard is the reason the whole series is hard: a node can’t tell a crashed peer from a slow one, so it can never simply “wait for everyone.” Consensus algorithms get around this with majorities — decisions require agreement from more than half the nodes — which is the single idea that makes everything else work.

The majority (quorum) principle

The foundational trick of modern consensus is that any decision requires a majority (a quorum) of the nodes. With a cluster of N nodes, a majority is ⌊N/2⌋ + 1 — 3 of 5, 2 of 3. This one rule delivers two critical properties:

The majority rule is also why consensus clusters use odd numbers of nodes: 5 nodes tolerate 2 failures, and so do 6 — the extra node adds cost without adding fault tolerance, because a majority of 6 is 4 (tolerating 2) just like 5.

Crucially, majorities are what make consensus safe under partition. If the network splits the cluster, at most one side can hold a majority, so at most one side can make decisions — the minority side stops rather than diverging. That is consensus enforcing the CP side of CAP: it sacrifices availability on the minority partition to never violate agreement.

Raft: consensus you can understand

Consensus algorithms were long dominated by Paxos, which is famously hard to understand and implement correctly. Raft was designed explicitly for understandability and decomposes consensus into three digestible pieces: leader election, log replication, and safety. Every node is in one of three states — follower, candidate, or leader — and time is divided into numbered terms, each with at most one leader.

Leader election

Raft has exactly one leader at a time, and all client requests go through it. Followers expect regular heartbeats from the leader. If a follower hears nothing for an election timeout, it assumes the leader is dead, becomes a candidate, increments the term, votes for itself, and asks the others for their votes.

The majority requirement guarantees at most one leader per term (two leaders would need two overlapping majorities, impossible), and it means a partitioned minority can never elect a leader — so it can’t accept writes.

Log replication

Once elected, the leader serves client operations by appending them to its log and replicating entries to followers:

Client → Leader: append entry
Leader: append to own log, send to followers
Followers: append, acknowledge
Leader: once a MAJORITY have the entry → "committed" → apply it, reply to client

An entry is committed only once a majority of nodes have stored it. Committed entries are then applied to each node’s state machine in the same order, which is what gives every node an identical, consistent view — this is the “single order of operations everyone agrees on” that consistency models needed. Because commitment requires a majority, a committed entry survives the failure of any minority of nodes.

Safety

Raft’s safety rules ensure agreement is never violated even across leader changes: a candidate can only win if its log is at least as current as the majority that elects it, which guarantees a new leader already has every committed entry — so committed operations are never lost or reordered when leadership changes. This is the property that makes Raft correct, not just live.

Where consensus actually lives

You rarely implement Raft yourself; you use it through the systems built on it. Consensus is the engine inside:

The practical guidance: don’t build consensus from scratch — it’s subtle, and the safety edge cases are exactly where hand-rolled implementations fail. Delegate agreement to a proven system (etcd/ZooKeeper) or a database that embeds a proven algorithm. Understand consensus deeply so you know what it costs (a majority must be reachable, so it’s a CP building block that trades availability for correctness) and when you need it (anywhere “exactly one” or “single agreed order” must hold) — then reach for a battle-tested implementation.

Key takeaways

Further reading

Sources & References