Tier 2 · Distributed Core

Consensus & Raft

How a cluster agrees on one ordered log, and why you should use as little of it as possible

⏱ 21 min distributedconsensusraft 📕 Ch 6 — Design a Key-Value Store

What consensus has to guarantee

In practice you rarely want a single value; you want a replicated log — an ordered sequence of commands that every node agrees on. Feed that identical log into identical deterministic state machines and every replica ends up in the same state. This is state machine replication, and it's what turns consensus into a usable database.

FLP, and why it doesn't stop us

The FLP impossibility result: in a fully asynchronous system where even one node may fail, no deterministic algorithm can guarantee consensus.

The reason connects directly to failure models: you cannot distinguish a crashed node from a slow one, so an algorithm can be kept indefinitely undecided by sufficiently unlucky timing.

Real systems sidestep it — they don't refute it:

So Raft guarantees safety always (never two leaders in one term, never a committed entry lost) and liveness only when the network cooperates. That's the right split: it may stall, but it will not corrupt.

Raft

Raft was explicitly designed to be understandable, decomposing consensus into three pieces.

Leader election

Every node is Follower, Candidate, or Leader. Time is divided into terms, each with at most one leader.

  1. Followers expect heartbeats. If none arrives before a randomised election timeout (say 150–300 ms), the follower becomes a Candidate.
  2. It increments the term, votes for itself, and requests votes.
  3. A node grants its vote if it hasn't voted this term and the candidate's log is at least as up to date as its own.
  4. A majority of votes makes it Leader, and it starts sending heartbeats.

The randomised timeout is the whole trick for liveness: if two candidates split the vote, they'll time out at different times and one will win the next round. With fixed timeouts they could deadlock indefinitely.

Log replication

  1. Clients send commands to the leader only.
  2. The leader appends to its log and sends AppendEntries to followers.
  3. Once a majority has persisted the entry, the leader marks it committed and applies it to its state machine.
  4. Followers learn of the commit from subsequent messages and apply it too.

An entry that's committed is committed forever. This is the safety property everything else protects.

Safety

The subtle part. Raft must guarantee a new leader never erases a committed entry, which it gets from the election restriction: a node only votes for a candidate whose log is at least as up to date as its own.

Since a committed entry is on a majority, and any winning candidate needs a majority, the two majorities must overlap — so at least one voter has the entry and will refuse to vote for a candidate lacking it. Any node that can win an election necessarily already has every committed entry.

That's the same pigeonhole argument as quorums, doing much heavier lifting.

Why odd numbers

A cluster of 2f + 1 nodes tolerates f failures, because a majority must remain reachable.

Nodes Majority Failures tolerated
3 2 1
4 3 1
5 3 2
6 4 2
7 4 3

Even sizes are strictly worse than the odd number below them: 4 nodes tolerate the same one failure as 3, while needing a larger majority (more latency) and adding a machine's worth of failure probability. Always odd.

Three is the common default. Five is for when you want to survive two failures — or one failure during a maintenance window, which is the real reason. Seven is rare; every write must reach four nodes, and the coordination cost outgrows the benefit.

What it costs

Every committed write requires a round trip from the leader to a majority. That means:

Paxos, Multi-Paxos, ZAB, and friends

For interviews, know Raft properly and mention that Paxos solves the same problem. Nobody wants a Paxos derivation; they want to see you understand leader election, majority commit, and the safety argument.

When you actually need it

Yes: leader election, cluster membership, configuration that must not diverge, distributed locks, uniqueness constraints, anything where two simultaneous answers is a correctness bug.

No: high-volume application data, anything tolerating eventual consistency, single-region data a primary/replica setup already handles, and — critically — anything you can design so coordination isn't required.

That last point is the real lesson:

What to take away

Check yourself

  1. Why does Raft use randomised election timeouts rather than a fixed value?

  2. How does Raft guarantee a newly elected leader has every committed entry?

  3. Why is a 4-node Raft cluster a worse choice than a 3-node one?

  4. Your team proposes storing all application records in etcd because it is strongly consistent. What is the main objection?