Sub topic 13
Split Brain and Fencing
This chapter assumes you know what a primary and a replica are, how health checks and heartbeats work, and what quorum means from the previous chapter. It covers what goes wrong when two nodes both believe they are the primary, and how to stop it.
What split brain is
Split brain is a state where two or more nodes each believe they are the active primary and both accept writes. The cluster has no single authority, so the copies of the data diverge.
The usual cause is a failure detector that cannot tell the difference between a dead node and an unreachable one. A health check only tells you that no answer arrived. The primary may have crashed. It may also be paused by a long garbage collection, starved of CPU, or cut off by a network partition while it keeps serving clients.
Why it is damaging
Both sides acknowledge writes, so clients on each side believe their data is safe. When the partition heals, the histories conflict. One side's writes must be discarded or merged by hand. With asynchronous replication, some of those writes were already confirmed to users, so you lose acknowledged data.
Prevention 1: quorum
Quorum makes promotion require agreement from a majority. A standby may become primary only if it can reach more than half of the voting members. With three voters, the side of a partition holding two votes can elect a leader. The side holding one vote cannot.
This guarantees at most one side can promote. It does not stop the old primary from continuing to act on its own, because it may not yet know it lost leadership. The old primary must step down when it loses contact with a majority. A paused process, however, may wake up later and not realize any time has passed.
Prevention 2: fencing
Fencing blocks a deposed primary from doing damage, even if it still believes it is in charge. Two common forms exist:
- Resource fencing (STONITH): the cluster powers off or isolates the old node, for example through a power controller or by revoking its network access. STONITH stands for "shoot the other node in the head".
- Fencing tokens: each new primary gets a number higher than the last. The storage layer rejects any write that carries an older number.
The example below shows a token check in the storage layer.
class FencedStorage:
def __init__(self):
self.highest_token = 0
self.value = None
def write(self, token, value):
if token < self.highest_token:
raise PermissionError("stale fencing token")
self.highest_token = token
self.value = value
store = FencedStorage()
store.write(1, "from old primary")
store.write(2, "from new primary")
try:
store.write(1, "late write from old primary")
except PermissionError as err:
print(err) # stale fencing tokenThe old primary's late write is refused because its token, 1, is lower than the highest token seen, 2.
Choosing between the approaches
| Approach | Stops | Needs | Weakness |
|---|---|---|---|
| Quorum | Two sides both promoting | Odd number of voters, at least 3 | Does not stop a stale primary on its own |
| Fencing token | Stale writes to storage | Storage that checks tokens | Only protects resources that check |
| STONITH | Old node acting at all | Out-of-band control such as power | Control path can itself fail |
In practice you combine them. Quorum picks one leader, and fencing makes sure the previous leader cannot interfere.
Common errors
- Running two voters. If they are partitioned, neither has a majority, and the cluster stops. Use three or more, in different fault domains.
- Promoting on a short timeout alone. A brief pause then triggers a needless promotion. Lengthen the timeout or require confirmation.
- Issuing tokens that are not strictly increasing, such as tokens that reset after a restart.
- Fencing only some resources. If the old primary can still reach one unprotected disk or queue, it can still corrupt it.
Exercise
A three-node cluster is split by a partition into one node and two nodes. The old primary is on the single-node side. Which side can elect a leader, and what stops the old primary from writing to shared storage?
Show answer
The two-node side holds a majority (2 of 3 votes), so it can elect a leader. The old primary is stopped by fencing: the new leader gets a higher fencing token, and the storage rejects any write that carries the old, lower token. The old primary should also step down because it cannot reach a majority.