Consensus: Raft and Paxos
Sooner or later every system design needs a single source of truth that doesn't live on a single machine. Which node is the leader for this shard? Who holds this lock? What is the current cluster configuration? Has this transaction committed? If one server answers these questions, that server is a single point of failure. If several servers answer them independently, they can disagree, and disagreement here means two leaders writing over each other, two clients both believing they hold a lock, or money committed on one replica and missing on another.
Consensus is the problem of getting a group of machines to agree on a value (or a sequence of values) in a way that survives some of them crashing, restarting or being cut off by the network. It's the foundation under etcd, ZooKeeper, Consul, Kafka's KRaft controller, Spanner, CockroachDB and most "strongly consistent" distributed databases.
You'll almost never implement Raft in an interview. But you'll constantly lean on systems built with it, and a strong candidate can explain what those systems guarantee, what they cost, and why "just use a primary with a heartbeat" isn't enough. We'll build up from the core problem:
- What consensus promises, and why it's harder than it looks.
- Quorums and the replicated state machine, the two ideas everything else rests on.
- Raft in detail: elections, log replication, and the safety rules.
- Paxos, and how it relates to Raft.
- Practical concerns: reads, leases, fencing tokens, performance and cluster sizing.
The Problem
Formally, a consensus algorithm lets a set of nodes each propose a value and guarantees:
- Agreement: no two nodes decide different values.
- Validity: the decided value was proposed by some node (the algorithm can't just always pick "0").
- Termination: every node that doesn't crash eventually decides.
The first two are safety properties: nothing bad ever happens. The third is a liveness property: something good eventually happens. Real algorithms are designed so that safety holds no matter what (messages delayed for minutes, machines pausing, packets reordered) and liveness holds when the network is behaving reasonably.
That split isn't a design choice, it's forced. The FLP impossibility result shows that in a fully asynchronous network, where messages can be delayed arbitrarily and you can't tell a slow node from a dead one, no deterministic algorithm can guarantee termination if even one node might crash. Raft and Paxos get around this the practical way: they use timeouts to make progress, and if timeouts fire at the wrong moments they might stall for a while, but they never decide two different things.
Why the Obvious Approaches Fail
Consider the simplest design: one primary database, one standby, and a heartbeat between them. If the standby stops hearing heartbeats for five seconds, it promotes itself.
The failure is easy to construct. The network between the two drops, but both machines are fine and clients can still reach both. The standby promotes itself. Now there are two primaries accepting writes: split brain. When the network heals, you have two diverged histories and no principled way to merge them.
Adding a third "witness" node that the standby must ask before promoting helps, and if you follow that thought carefully you end up reinventing majority quorums. The other obvious approach, "always pick the node with the lowest ID as leader," fails because nodes don't agree on who is alive. Every correct answer has the same shape: a decision only counts if a majority took part in it.