Sub topic 2
Crashes, partitions, and majority quorums
This chapter defines the failures Raft is designed to survive and explains why a majority quorum is the tool that makes survival possible. Later chapters (terms, elections, replication) use these ideas without re-deriving them.
I assume you know what nodes, messages, and network partitions are, and what a request and response look like in an RPC system. The Python is short and uses only the standard library.
The fault model
A protocol is correct only relative to the failures it assumes. Raft assumes the following.
- Servers can crash and restart. A crashed server stops sending and receiving messages. When it restarts, it keeps whatever it wrote to stable storage (disk) and loses everything held only in memory. This is called the crash-recovery model.
- The network is unreliable. Messages can be lost, delayed, duplicated, or reordered. Raft RPCs are written so that a duplicate or late message is harmless.
- The network can partition. The servers split into groups. Servers in the same group can talk to each other, and servers in different groups cannot.
- Servers are not malicious. A server can fail by stopping, but it never lies, forges votes, or sends corrupt data on purpose. Tolerating that kind of fault is called Byzantine fault tolerance, and Raft does not provide it.
- Clocks and speeds are not trusted for safety. Servers may run slowly and messages may arrive arbitrarily late. Raft never relies on timing for correctness. It only relies on timing to make progress.
The last point separates two properties you should keep apart for the rest of the tutorial.
| Property | Meaning | Depends on timing? |
|---|---|---|
| Safety | Nothing bad ever happens, for example two servers applying different commands at the same log index | No. It holds under any delays, losses, and crashes in the model |
| Liveness | Something good eventually happens, for example a leader is elected and client commands get committed | Yes. It needs the network and servers to be reasonably timely for long enough |
If the network is badly broken, Raft may stop making progress, but it will not return wrong answers. That trade-off is deliberate.
You cannot tell a crash from slowness
Suppose server A sends a request to server B and hears nothing back. Several different things could have happened:
- B crashed before receiving the request.
- B processed the request, and its reply was lost.
- B is alive but very slow, and the reply will arrive later.
- The network between A and B is partitioned.
From A's point of view these cases look identical. The only observation A has is a timeout, which is a guess. A system that treats every timeout as "B is dead" can wrongly declare a live server dead. Raft is built on the assumption that such wrong guesses will happen. Suspicion is allowed to be wrong, as long as wrong suspicion never breaks safety.
This is why Raft cannot use a rule like "wait until we know who is alive, then decide". Nobody can know that. The protocol needs a rule that gives the same safe answer however the guesses turn out. The majority quorum is that rule.
Majority quorums
A quorum is a group of servers whose agreement is enough to make a decision. In Raft, a quorum is any set containing more than half of the servers in the cluster. For a cluster of n servers:
- Majority size =
floor(n / 2) + 1. - Tolerated failures =
n - majority size. This is how many servers can be unreachable while a quorum can still form.
| Cluster size n | Majority size | Failures tolerated |
|---|---|---|
| 1 | 1 | 0 |
| 2 | 2 | 0 |
| 3 | 2 | 1 |
| 4 | 3 | 1 |
| 5 | 3 | 2 |
| 6 | 4 | 2 |
| 7 | 4 | 3 |
To tolerate f failures you need 2f + 1 servers. Look at the rows for 3 and 4, or 5 and 6. Adding one server to an odd-sized cluster raises the majority size without allowing another failure. It adds cost, and it makes some partitions worse, as the next section shows. Clusters are therefore usually sized 3 or 5.
The crucial fact is not the size but the overlap.
Any two majorities intersect
Take any two majorities of the same cluster. Each has more than n / 2 members, so together they have more than n memberships. There are only n servers, so at least one server must belong to both.
This one shared server is the core of Raft's safety argument. It means that:
- If a leader's entry was stored on a majority, then any later majority that tries to elect a new leader includes at least one server that has the entry. That server can refuse to vote for a candidate whose log is missing it.
- If a candidate wins votes from a majority in a term, no other candidate can also win a majority in that same term. They would need a shared voter to vote twice, and each server votes at most once per term.
You will see both uses in later chapters. Here, what matters is that the guarantee comes from counting alone. It does not depend on knowing which servers are alive.
You can check the overlap property by brute force for small clusters.
from itertools import combinations
def majority(n):
return n // 2 + 1
def max_failures(n):
return n - majority(n)
def every_pair_intersects(n, quorum_size):
"""True if any two groups of quorum_size servers out of n share a member."""
quorums = [set(c) for c in combinations(range(n), quorum_size)]
return all(a & b for a in quorums for b in quorums)
for n in range(1, 8):
print(
f"n={n} majority={majority(n)} tolerates={max_failures(n)} "
f"majorities_intersect={every_pair_intersects(n, majority(n))}"
)
# Half of an even cluster is not enough: two disjoint groups of 2 exist in 4 servers.
print("n=4, size 2 intersects:", every_pair_intersects(4, 2))Every majority row prints True. The last line prints False, because in a 4-server cluster the groups {0, 1} and {2, 3} are disjoint. Exactly half is not a quorum.
What a partition does
A partition tests the quorum rule. Suppose a five-server cluster splits into two groups.
The two sides behave differently:
- The majority side
{A, B, C}has three servers, which is a majority of five. It can elect a leader and commit new entries, because a leader there can gather three acknowledgments. - The minority side
{D, E}has two servers. It can never gather three. A candidate there can collect at most two votes, and a leader there can collect at most two acknowledgments, so it cannot commit anything.
Neither side knows the partition exists. D and E only see timeouts. Without a timeout they cannot tell "A, B, C crashed" from "the link is cut". It does not matter, because the minority side blocks itself whatever the cause.
What happens to an old leader
The more dangerous case is when the old leader ends up in the minority. Say D was the leader when the link broke.
- D keeps accepting client commands and appends them to its log. It sends them to E and to the majority side, but only E replies.
- D counts two copies (itself and E) out of five. That is not a majority, so D never marks those entries committed and never tells clients they succeeded.
- On the majority side, the servers stop hearing from D, time out, and elect a new leader, say A, for a higher term. A commits new entries with B and C.
- When the partition heals, D sees a message carrying a higher term, steps down, and discards its uncommitted entries in favour of A's log.
At no point were there two leaders able to commit. D thought it was the leader, but the quorum rule stopped it from doing any harm. The uncommitted entries it held were never acknowledged to a client, so dropping them breaks no promise.
Why even-sized clusters are a trap
A four-server cluster has majority size 3, so it tolerates one failure, the same as three servers. It also has a worse partition profile. If it splits two and two, neither side has three servers, so neither side can make progress and the whole cluster is unavailable. A three-server cluster that splits one and two always leaves one side with a majority.
Common errors
- Counting only the servers you can reach. The majority is computed from the configured cluster size, not from the live servers. If a five-server cluster has two crashed servers, the majority is still three. Lowering the threshold to "most of the servers I can see" would let two minority sides each act as a majority and diverge.
- Forgetting that the leader counts toward the majority. A leader's own copy of an entry counts as one replica. In a five-server cluster, a leader needs acknowledgments from two other servers, not three.
- Counting a rejection as an acknowledgment. A server that replies with
Falsehas not stored the entry or given its vote. Only successful replies count toward a quorum. - Treating "no reply" as "no". A missing reply means nothing. The server may have stored the entry or granted the vote, and the leader or candidate cannot know. Raft handles this by retrying, which is safe because the RPCs tolerate duplicates.
- Assuming a quorum is a fixed set of servers. Any majority works. Which three of five answer first is irrelevant to correctness.
- Mixing configurations. Overlap is guaranteed only among quorums of the same cluster configuration. If servers are added or removed, majorities of the old and new sets may not intersect. Raft has a dedicated procedure for this, covered in the chapter on membership changes.
Crash recovery and the quorum
Overlap says that a server holding a committed entry exists in every later quorum. That only helps if the server still has the entry after it restarts. A server that acknowledged an entry and then forgot it after a crash would silently break the argument, because the majority that "stored" the entry would no longer be a real majority.
For this reason Raft requires servers to write certain state to disk before replying to an RPC. The chapter on persistence lists exactly which state and why. For now, remember that the quorum argument assumes an acknowledgment is a durable promise.
Exercise
A cluster has 6 servers. (a) What is the majority size and how many failures can it tolerate? (b) Is it better or worse than a 5-server cluster for availability, and why?
Show answer
(a) The majority size is 6 // 2 + 1 = 4, so it tolerates 6 - 4 = 2 failures. (b) It is no better, because the 5-server cluster also tolerates 2 failures. It is slightly worse: it needs 4 acknowledgments per commit instead of 3, and a 3/3 partition leaves neither side with a majority, so the whole cluster stops. The 5-server cluster never splits into two sides that both lack a majority.
Exercise
A 5-server cluster is partitioned into {A, B} and {C, D, E}. The old leader A is on the minority side and receives a client command. It appends the command, and B acknowledges it. Can A tell the client the command succeeded? What happens to the entry after the partition heals?
Show answer
No. A has the entry on 2 of 5 servers, which is not a majority of 3, so the entry is not committed and A must not report success. Meanwhile {C, D, E} can elect a new leader in a higher term and commit other entries. After the partition heals, A sees the higher term and steps down, and its uncommitted entry is overwritten by the new leader's log. No client was told the command succeeded, so no promise is broken.
Exercise
Why does a candidate that wins a majority of votes in term T guarantee that no other candidate also wins a majority in term T?
Show answer
Any two majorities of the same cluster share at least one server. That shared server grants at most one vote per term, so it cannot vote for both candidates. Therefore two candidates cannot both collect a majority in the same term.