Sub topic 15
The alternative: consensus-based replication with Raft or Paxos
This chapter assumes you know how preference lists, quorums (R + W > N) and vector clocks work from earlier chapters. It compares the leaderless design you have built so far with a design built on consensus.
What changes with consensus
In the leaderless design, any replica can accept a write, and conflicts are detected afterward with version vectors. A consensus-based design takes the opposite approach. The replicas of a key range agree on a single ordered log of operations before any of them is applied. Every replica applies the same operations in the same order, so there are no conflicting versions to resolve.
Raft and Multi-Paxos are the two protocols most often used for this. Raft is easier to learn because it splits the problem into leader election, log replication and safety. The two protocols give the same guarantee.
How Raft replicates a write
- The cluster elects one leader for a term. A node becomes leader when a majority of nodes vote for it.
- A client sends a put to the leader.
- The leader appends the operation to its log and sends it to the followers.
- Once a majority of nodes have stored the entry, the leader marks it committed and applies it to its state.
- The leader replies to the client and tells followers to apply the entry.
If the leader fails, the followers time out and hold a new election. Only a node whose log contains every committed entry can win, so committed writes are never lost.
How it fits a key-value store
One Raft group cannot hold a large data set, because every write goes through a single leader. Systems such as etcd keep all data in one group and accept that limit. Systems such as CockroachDB and TiKV split the key space into ranges. Each range is its own Raft group with typically 3 or 5 replicas. This replaces the hash ring with range partitioning, and each range has its own leader.
Comparing the two designs
| Property | Leaderless quorum | Consensus (Raft) |
|---|---|---|
| Write path | Any replica, N parallel writes | Leader, then a majority |
| Consistency | Eventual, tunable by R and W | Linearizable when reads go through the leader or a read index |
| Concurrent writes | Siblings or last-write-wins | One total order, no siblings |
| Minority partition | Can still accept writes with a sloppy quorum | Cannot commit writes |
| Leader failure | No leader to lose | Short pause during election |
| Repair | Read repair, Merkle trees | Followers catch up from the log |
In CAP terms, a Raft group is CP. It gives up availability on the minority side of a partition to keep one history. The leaderless design leans AP. In PACELC terms, Raft pays latency on every write for the round trip to a majority, even when there is no partition.
Gotchas
- A cluster of N nodes tolerates only floor((N - 1) / 2) failures. Three nodes tolerate one failure, and five tolerate two.
- Even cluster sizes add cost without adding fault tolerance. Four nodes still tolerate only one failure.
- Writes are limited by one leader per group. Hot ranges need to be split.
- Cross-region replicas raise commit latency, because the leader waits for a majority that may be far away.
Choosing between them
Choose consensus when you need a single agreed order, such as for locks, metadata or transactions, and you can accept lower availability during partitions. Choose leaderless quorums when writes must succeed during failures, you can handle conflicts in the application, and low write latency matters more than a strict order.
Exercise
A key-value store runs one Raft group on 5 nodes. Two nodes crash. Can it still commit writes? What if three nodes crash?
Show answer
With two crashes, 3 of 5 nodes remain, which is a majority, so writes still commit. With three crashes, only 2 nodes remain, which is less than a majority, so no new writes can be committed until a node returns.
Exercise
Why does a Raft-based store have no need for vector clocks or siblings?
Show answer
Every write goes into one ordered log agreed by a majority, so all replicas apply writes in the same order. Writes are never concurrent from the store's point of view, so there are no conflicting versions to detect or merge.