Sub topic 13
Consensus: how strong consistency is implemented
This chapter assumes you know what linearizability is, how replication works, and how quorums overlap. You should also be able to read simple Python.
The problem consensus solves
Quorum overlap alone does not give linearizability. Two clients can propose different values at the same time, and replicas can see them in different orders. Replicas need to agree on one value and one order, even when messages are delayed and nodes crash.
That agreement problem is called consensus. Each node proposes a value, and the protocol makes all non-faulty nodes decide on the same one. Decisions are final.
Replicated state machines
Consensus becomes a database through a replicated state machine:
- Every replica starts in the same state and runs the same deterministic code.
- Clients send commands to the cluster.
- Consensus decides which command goes in each slot of a shared log.
- Each replica applies the log entries in order.
Deterministic code applied to the same ordered log produces the same state on every replica. The log gives every replica the same single order of operations, and that is what a linearizable system needs.
How Raft and Paxos do it
Protocols such as Raft, Multi-Paxos and Zab share the same core ideas:
- Leader: One node at a time orders commands. If it fails, an election picks a new one.
- Terms (epochs): Each leader has a numbered term. Nodes reject messages from older terms, which stops a deposed leader from causing harm.
- Majority commit: An entry is committed only after a majority of N nodes store it. With N = 2f + 1 nodes, the cluster tolerates f crashes.
- Majority election: A new leader needs votes from a majority. Any two majorities overlap, so the new leader always hears about every committed entry.
The majority overlap is the same R + W > N idea from the quorum chapter. Here it is applied to a single ordered log, not to independent key versions.
The sketch below shows the commit rule.
def committed(acks, n):
"""An entry is committed once a majority of n nodes has stored it."""
return acks > n // 2
print(committed(2, 3)) # True
print(committed(2, 5)) # False
print(committed(3, 5)) # TrueReads need care
Writes go through the log. Reads also need care. A deposed leader might not know it has been replaced, so it could return stale data and break linearizability. Systems avoid this in one of three ways:
- Send reads through the log, which is correct but slow.
- Have the leader confirm with a majority that it is still leader before it answers.
- Use time-based leader leases. This is fast but depends on bounded clock drift.
Costs and limits
- Latency: Each write needs at least one round trip to a majority. Spreading nodes across regions raises this cost.
- Availability: Without a majority, the cluster stops accepting writes. This is the CP side of CAP.
- Throughput: One leader orders all commands, so it can become a bottleneck. Large systems split data into shards, each with its own consensus group.
- Limit: Consensus cannot be solved deterministically in a fully asynchronous network if even one node may crash (the FLP result). Practical protocols stay safe always and make progress only when the network is stable enough to elect a leader.
Exercise
A cluster has 5 nodes. Two of them crash. Can it still commit writes? What if three crash?
Show answer
With two crashes, 3 of 5 nodes remain, which is a majority, so writes still commit. With three crashes, only 2 remain, which is not a majority, so the cluster stops accepting writes and stays consistent instead of risking disagreement.
Exercise
Why must a newly elected leader be chosen by a majority, and what does that guarantee about committed entries?
Show answer
Any majority overlaps with the majority that stored each committed entry, so at least one voter has the entry. The protocol only elects a candidate whose log is up to date, so the new leader holds every committed entry.