0 XP · 0 scenes

Consensus: Raft and Paxos

Distributed Systems Core · lesson 3 of 11 · about 35 min

Consensus: Raft and Paxos

How a group of unreliable machines agrees on one order of events: why a majority is the magic number, a live Raft cluster where you kill leaders and cut networks, how elections and log commits work, what a committed write costs, and why the correct answer to losing the majority is to stop. Tap any for a plain-language card.

0 / 9 sections done
  1. 1Why a majority4 min
  2. 2Leader election, live6 min
  3. 3Log replication, live5 min
  4. 4Safety under a partition5 min
  5. 5What consensus costs4 min
  6. 6Does it need consensus?3 min
  7. 7Who lives by this1 min
  8. 8Interview angle2 min
  9. 9Break It: losing the majority4 min

Words in this lesson12Tap one for its meaning.

The problem

Everybody must agree on one order of events, even while machines die.

Why a majority? Because any two majorities of the same group must share at least one member. That shared member will not vote for two different leaders in one term and remembers what it accepted, so two leaders can never both gather a majority in the same term. The page checks this by brute force for your cluster size. The price: a cluster of N tolerates only ⌊(N−1)/2⌋ failures, and an even size buys nothing over the odd size below it.

Compare 3 and 4 nodes, and 5 and 6: the even size tolerates no more failures than the one below it.

Majority needed3
Failures tolerated2
Smallest overlap of two majorities1
Cluster available99.884%
  • 1 server (tolerates 0)
    95.00%
  • 2 servers (tolerates 0)
    90.25%
  • 3 servers (tolerates 1)
    99.28%
  • 4 servers (tolerates 1)
    98.60%
  • 5 servers (tolerates 2)
    99.88%
  • 6 servers (tolerates 2)
    99.78%
  • 7 servers (tolerates 3)
    99.98%
  • 9 servers (tolerates 4)
    100.00%

5 servers need a majority of 3 and tolerate 2 failures. The two smallest possible majorities share at least 1 server, found by checking every pair of 3-server subsets. With each server down 5% of the time, the cluster is available 99.884% of the time.

Brute-force check over every pair of majority-sized subsets; availability is an exact binomial tail assuming independent failures.

Leader election

Randomness is how a leaderless group picks one without talking first.

The cluster below is live. It is a working simplified Raft: 5 servers, 10-20 ms message delays, heartbeats every 50 ms. Kill the leader and watch the followers time out, one becoming a candidate in a higher term and winning. The experiment underneath repeats the failover 60 times for your timeout range: if the range is too narrow, two followers time out together, split the vote and must try again (a ).

Kill the leader twice in a row. Then set the spread to 5 ms and watch the experiment's split votes rise.

S1followerterm 1
·
commit 1
S2leaderterm 1
·
leader commit 1
S3followerterm 1
·
commit 1
S4followerterm 1
·
commit 1
S5followerterm 1
·
commit 1
Leader nowS2 (term 1)
Elections started1
Time without a leader160 ms
Average failover (60 runs)184 ms
Worst failover390 ms
Split votes8% of runs

Timeouts of 150 to 300 ms: a failure is repaired in about 184 ms on average (worst 390 ms), and 8% of the 60 runs needed an extra term because of a split vote. Shorter timeouts mean faster failover but more false alarms on a slow network; a wider spread makes split votes rarer.

Real simulated Raft (terms, RequestVote, AppendEntries heartbeats, randomised timeouts). One tick is 10 ms. Failover time includes the timeout and one round of voting.

Log replication

A command is real only when a majority has written it down.

Cards show each server's log: solid squares are committed, hatched ones are written but not yet committed (a dot is a leader's no-op). Send commands and watch the commit index advance a round trip later. Then take followers down: with 3 of 5 still alive the cluster keeps committing; with fewer, entries pile up uncommitted.

Send four commands, kill two followers and send more: still committed. Kill a third and they stay hatched.

S1leaderterm 1
·
leader commit 1
S2followerterm 1
·
commit 1
S3followerterm 1
·
commit 1
S4followerterm 1
·
commit 1
S5followerterm 1
·
commit 1
Servers alive5 of 5
Entries in the leader's log1
Committed1
Waiting for a majority0

5 of 5 servers are up, so every command reaches a majority (3) and is committed within about a round trip: 1 committed, 0 still in flight.

Working Raft log replication with the AppendEntries consistency check, majority commit and a leader no-op on election.

Safety

The old leader thinks it still leads. The majority disagrees, and wins.

When the network heals, the old leader hears a message with a higher term and steps down. Its uncommitted entries conflict with the new leader's log, so they are deleted and replaced. Nothing that was committed is ever lost, and a client that was told “committed” can rely on it. A client that sent a command to the old leader gets no commit and must retry: that is the price of safety. Press the buttons in order.

Press “Split”, then “Send to the new leader”, then “Heal” and watch the hatched entry disappear.

S1followerterm 1
·
commit 1
S2followerterm 1
·
commit 1
S3leaderterm 1
·
leader commit 1
S4followerterm 1
·
commit 1
S5followerterm 1
·
commit 1
Leaders at once1
Highest committed1
Entries deleted on heal0

The cluster is healthy with one leader. Split it to see what the minority side can and cannot do.

The same simplified Raft as before, with a partition applied to message delivery. “Entries deleted” counts log entries a follower removed because they conflicted with the leader's.

What it costs

Every committed write waits for a majority to write it to disk.

Messages are the other cost: in steady state each entry costs one append and one acknowledgement per follower, 2 × (N − 1) messages, and every added node raises the work the leader does. More nodes buy fault tolerance, not speed. Multi-Paxos with a stable leader has the same steady-state cost; Raft was designed to be easier to understand and implement, not faster.

Set 5 nodes with 2 near (1 ms) and 2 far (80 ms): the write takes about 3 ms. Then set only 1 near.

  • Commit latency p50
    3.1 ms
  • Commit latency p99
    3.7 ms
Followers needed per commit2
Messages per commit8
Leader messages per second40K

With 5 servers, a commit needs the leader plus 2 followers. 2 near followers are enough, so the far ones never hold the write up: about 3.1 ms (p99 3.7 ms). At 5K commits a second the leader exchanges 40K messages a second.

4,000 seeded commits: latency = fsync + the k-th smallest follower round trip (log-normal jitter). Messages: one append plus one acknowledgement per follower.

Does it need consensus?

Six problems, three answers.

Consensus is powerful and expensive. Use it only when the problem truly demands agreement. Each answer is worth 20 XP.

Ask: must exactly one answer win? and is a wrong or duplicate answer a real problem?

Question 1 / 6

Choosing exactly one leader for a database cluster, even when messages are delayed or lost.

Tip: press 1 to 3 to answer, Enter for the next one.

Who lives by this

The quiet machinery under Kubernetes, Kafka and Google.

Four real systems that run consensus, and what they have published.

Designed to be understoodRaft (Ongaro and Ousterhout)

paper 2014 · "In Search of an Understandable Consensus Algorithm"

Diego Ongaro and John Ousterhout designed Raft explicitly to be easier to understand than Paxos, splitting consensus into leader election, log replication and safety. Their USENIX ATC 2014 paper reports a user study in which students answered questions about Raft more accurately than about Paxos, and there are many open-source implementations.

Kubernetes' memoryetcd

kube-apiserver → etcd (Raft) → desired state

etcd is a distributed key-value store using Raft, and Kubernetes keeps all cluster state in it. Its documentation recommends clusters of 3, 5 or 7 members: more members add no speed and tolerate more failures only at odd sizes, and each write needs a majority to be committed.

Paxos in productionGoogle Chubby and Spanner

lock service → Paxos groups

Google's Chubby lock service (2006) uses Paxos to replicate a small, highly consistent store used for leader election and configuration, and the paper "Paxos Made Live" describes the engineering effort of building it. Spanner uses Paxos groups to replicate each shard of data across zones.

Replacing ZooKeeperApache Kafka KRaft

controller quorum → Raft metadata log

Kafka used ZooKeeper to store cluster metadata, then built KRaft, its own Raft-based metadata quorum, so the cluster manages itself. KRaft became production-ready in Kafka 3.3 and ZooKeeper mode was removed in Kafka 4.0: consensus moved from a separate service into the system.

Interview angle

What interviewers actually ask.

Six questions, each worth 20 XP. “Explain Raft” or “why do we use an odd number of nodes?” are common senior-level questions.

Name the three parts (election, replication, safety), say what a majority guarantees, and say what happens when it is lost.

Question 1 / 6

Why does a Raft cluster need a majority to elect a leader or commit an entry?

Tip: press 1 to 3 to answer, Enter for the next one.

Break it

Kill three of five and the cluster refuses to do anything. Good.

Kill servers one by one and try to send a command after each. When the majority returns, the cluster recovers by itself: a leader is elected and everything that was committed is still there. Notice also what cannot happen: the two surviving servers never decide on their own, so there is no split brain and no divergence to repair afterwards.

Kill three servers and press “Send a command”: it is not accepted. Then revive all.

S1followerterm 2
·
commit 1
S2followerterm 2
·
commit 1
S3followerterm 2
·
commit 1
S4followerterm 2
·
commit 1
S5leaderterm 2
·
leader commit 1
Servers alive5 of 5
Leader with a majorityS5
Committed entries1

5 servers up and S5 leads with a majority: the cluster accepts and commits commands (1 committed so far).

The cluster is a working simplified Raft. “Leader with a majority” means a leader that a majority of live servers currently follow.

Where to go next