Sub topic 2
Crashes, partitions, and majority quorums
This chapter assumes you know what nodes, messages, and network partitions are, and that you have read the previous chapter on replicated state machines. Here you learn what kinds of failures Raft tolerates and why a majority of servers is the unit that makes decisions.
The fault model
Raft assumes a specific set of failures. Knowing the limits tells you what the protocol promises.
- Crash-recovery. A server can stop at any moment and restart later. It keeps whatever it wrote to disk and loses everything in memory.
- Message loss, delay, duplication, and reordering. The network is unreliable. Raft's RPCs must be safe to retry and to receive out of order.
- Partitions. The network can split servers into groups that cannot talk to each other. Each group may still contain running servers.
- No Byzantine faults. Servers do not lie, forge messages, or act maliciously. A server that answers either follows the protocol or is silent.
Raft also does not rely on synchronized clocks for safety. Timing affects only liveness, meaning whether the cluster makes progress. If clocks drift or messages are slow, the cluster may stall or elect leaders more often, but it must never return wrong results.
Why a majority
Suppose a cluster has five servers. Any decision, such as "this server is the leader" or "this log entry is stored", needs acknowledgement from at least three of them. Three is a majority: more than half of the cluster.
The key property is that any two majorities overlap in at least one server. With five servers, two groups of three must share at least one member, because 3 + 3 = 6 is larger than 5.
That shared server is what carries information from one decision to the next. If a majority stored an entry, any later majority that elects a leader includes at least one server that has that entry. The new leader's voters can therefore check that the new leader is not missing committed data. Later chapters build the election and commit rules on this overlap.
How many failures can you tolerate
A cluster of N servers needs floor(N / 2) + 1 servers for a majority. It can lose the rest and keep working.
def majority(n):
return n // 2 + 1
def tolerated_failures(n):
return n - majority(n)
for n in range(1, 8):
print(n, majority(n), tolerated_failures(n))Running this prints:
| Cluster size | Majority | Failures tolerated |
|---|---|---|
| 1 | 1 | 0 |
| 2 | 2 | 0 |
| 3 | 2 | 1 |
| 4 | 3 | 1 |
| 5 | 3 | 2 |
| 6 | 4 | 2 |
| 7 | 4 | 3 |
Notice that 4 servers tolerate no more failures than 3, and 6 no more than 5. An even-sized cluster adds cost and a larger majority without adding fault tolerance. Production clusters therefore usually have 3 or 5 servers.
What happens in a partition
Say a five-server cluster splits into a group of three (A, B, C) and a group of two (D, E).
- The group of three holds a majority. It can elect a leader and commit new entries.
- The group of two cannot reach a majority. It cannot elect a leader or commit anything. It may keep trying, but it makes no progress.
If the old leader was in the minority group, it keeps believing it is leader for a while. It can accept client requests, but it can never get a majority to acknowledge them, so those entries never commit. Meanwhile the majority group elects a new leader. When the partition heals, the old leader sees a higher term, steps down, and discards its uncommitted entries. Terms are the next chapter's topic.
This is the trade-off Raft makes under the CAP theorem: during a partition, the minority side gives up availability to keep consistency.
Crashes and recovery
A crashed server that restarts rejoins as a follower. What it wrote to disk before the crash is still there. If it had acknowledged a log entry before crashing, that acknowledgement counted toward a majority, so the entry must still be on disk after the restart. Otherwise the overlap argument above would break. The persistence chapter covers exactly which state has to be written before replying.
Exercise
A cluster has 6 servers. How many acknowledgements make a majority, how many failures can it tolerate, and would you gain anything by removing one server?
Show answer
A majority is 6 // 2 + 1 = 4, so the cluster tolerates 2 failures. Removing one server gives 5 servers, which also tolerate 2 failures while needing only 3 acknowledgements. You lose no fault tolerance and decisions get cheaper.
Exercise
A five-server cluster splits into groups of 2 and 3. Which group can commit new entries, and why can't both groups commit conflicting entries?
Show answer
The group of 3 can, because it contains a majority of the 5 configured servers. The group of 2 cannot reach 3 acknowledgements. Any two majorities share at least one server, so two disjoint groups can never both be majorities.