Split brain and fencing
The primary is not dead, it is out of reach
Section titled “The primary is not dead, it is out of reach”In the last lesson the primary crashed, and everyone agreed that it was gone. A network partition is harder. The primary is running and its clients may still reach it, but the other nodes cannot. From where the replicas stand, a crashed primary and an unreachable one look the same: it stops answering. The detector cannot tell them apart, so it promotes a replica. Now there are two nodes that believe they are the primary.
That is a split brain. Each node accepts writes, and neither sees the other’s. The PostgreSQL documentation names the cure and the stakes: the old primary has to be told it is no longer the primary, which it calls STONITH (“shoot the other node in the head”), so that both systems do not think they are primary, because that causes data loss.
Mental model. The head clerk is locked in a room with a phone line that has gone dead. Next door, the staff decide the clerk has gone and appoint another. The clerk inside, who knows nothing of this, keeps taking orders from the customers who can still reach the door. When the line is mended there are two ledgers, and only one can be kept.
Cut the primary off
Section titled “Cut the primary off”In this lab Alice’s side can still reach Node 1, and Bob’s side can only reach the other two nodes. At 500 ms a partition cuts Node 1 off. At 800 ms Node 2 is promoted, and 800 ms later, at 1,600 ms, the network is repaired. The replication is asynchronous and fencing is off.
A network partition cuts the primary off at 500 ms. A replica is promoted at 800 ms, but the old primary is alive and keeps accepting its clients’ writes. When the network is repaired at 1,600 ms, one of the two histories has to be thrown away.
A primary that is cut off from a majority of the nodes stops accepting writes 100 ms after the cut.
- A write on its way to a replica
- A write sent to bring a node level
- A write that never arrives
- A link cut by a network partition
- A write the node has just accepted
- A write acknowledged to its client
- A write the node refused
- A read, with the number of the write it returned
Each square is a write, filled once the node has applied it.
Node 2 is the primary now. It never received these acknowledged writes, so they are lost: 4, 5 and 8.
What the run came to
- Writes acknowledged
- 9 of 9
- Writes refused
- 0
- Time with two primaries
- 800 ms
- Acknowledged writes lost
- 3Writes: 4, 5 and 8
- Writes never acknowledged
- 0
| Node | Role | State | Writes applied |
|---|---|---|---|
| Node 1 | Replica | In sync | 1, 2, 3, 6, 7 and 9 |
| Node 2 | Primary | Accepting writes | 1, 2, 3, 6, 7 and 9 |
| Node 3 | Replica | In sync | 1, 2, 3, 6, 7 and 9 |
Event log: every event of the run, in order
| Time in the run | Event |
|---|---|
| 100 ms | Write 1 (Alice) is accepted by Node 1. |
| 100 ms | Write 1 (Alice) is acknowledged at once. |
| 200 ms | Node 2 applies write 1. |
| 200 ms | Write 2 (Alice) is accepted by Node 1. |
| 200 ms | Write 2 (Alice) is acknowledged at once. |
| 300 ms | Node 3 applies write 1. |
| 300 ms | Node 2 applies write 2. |
| 300 ms | Write 3 (Alice) is accepted by Node 1. |
| 300 ms | Write 3 (Alice) is acknowledged at once. |
| 400 ms | Node 3 applies write 2. |
| 400 ms | Node 2 applies write 3. |
| 500 ms | Node 3 applies write 3. |
| 500 ms | The network splits. These groups cannot reach each other: Node 1; Node 2, Node 3. |
| 700 ms | Write 4 (Alice) is accepted by Node 1. |
| 700 ms | Write 4 (Alice) is acknowledged at once. |
| 800 ms | Node 2 is promoted to primary (epoch 2). Writes it holds: 3. It never received these acknowledged writes, so they are lost: 4. |
| 900 ms | Write 5 (Alice) is accepted by Node 1. |
| 900 ms | Write 5 (Alice) is acknowledged at once. |
| 1,000 ms | Write 6 (Bob) is accepted by Node 2. |
| 1,000 ms | Write 6 (Bob) is acknowledged at once. |
| 1,100 ms | Write 7 (Bob) is accepted by Node 2. |
| 1,100 ms | Write 7 (Bob) is acknowledged at once. |
| 1,200 ms | Node 3 applies write 6. |
| 1,200 ms | Write 8 (Alice) is accepted by Node 1. |
| 1,200 ms | Write 8 (Alice) is acknowledged at once. |
| 1,300 ms | Node 3 applies write 7. |
| 1,300 ms | Write 9 (Bob) is accepted by Node 2. |
| 1,300 ms | Write 9 (Bob) is acknowledged at once. |
| 1,400 ms | Read (Alice): Node 1 answers with write 8. The answer is stale (newer acknowledged writes: 1). |
| 1,400 ms | Read (Bob): Node 2 answers with write 9. |
| 1,500 ms | Node 3 applies write 9. |
| 1,600 ms | The network is repaired. |
| 1,600 ms | Node 1 learns that Node 2 is the primary and becomes its replica. |
| 1,600 ms | Node 1 discards the writes the primary does not have: 4, 5 and 8. Clients had been told these succeeded: 4, 5 and 8. |
| 1,750 ms | Node 1 catches up and applies write 6. |
| 1,750 ms | Node 1 catches up and applies write 7. |
| 1,750 ms | Node 1 catches up and applies write 9. |
Illustrative values, not benchmarks of any real product.
Model notes
Assumptions
- There is one record. Each write gets the next number, in the order clients send them, and a node’s version is the number of the last write it has applied.
- A replica applies a write a fixed time after the primary accepts it: the lag you set. Confirmations travel back at no cost, so an acknowledgement waits only for replicas.
- A primary acknowledges a write once the number of replicas the mode names have applied it. If it cannot reach enough of them it keeps waiting, because it cannot tell slow from gone.
- A write that is still travelling when its sender crashes, or when a partition cuts the link, never arrives.
- Failover is a step of the script: a chosen replica is promoted at a fixed time. The other replicas follow it and are made to match its history.
- A node that comes back, or rejoins after a partition, steps down and discards whatever the new primary does not have. The history of the newest epoch is the one that survives.
Simplifications
- Three nodes, one record and fixed lags. A real replication stream has delays that vary, batches, backlogs and many records.
- The failure detector is a fixed delay. Real ones trade a quicker promotion against false alarms.
- Fencing takes effect 100 ms after the partition. A write acknowledged on the cut-off primary inside that window would still be lost; the script sends none.
Parameter values are illustrative. They are not benchmarks of any real product.
For 800 ms there are two primaries, from the promotion until the repair: the lab shows it as Time with two primaries. Alice writes three times to Node 1 after the cut, and every write is acknowledged. Bob writes three times to Node 2. When the network is repaired, one history has to go. The newest epoch, the one that began with the promotion, wins, so Node 1 steps down and throws away what Node 2 does not have: writes 4, 5 and 8, the three that Alice made after the cut. Clients were told they had succeeded.
Two reads at 1,400 ms show the divergence while it lasts. Alice reads from Node 1 and gets write 8. Bob reads from Node 2 and gets write 9. Each is told something different, and neither is wrong from where it stands.
Stopping the divergence
Section titled “Stopping the divergence”There are two ways to stop the cut-off primary from making promises it cannot keep, and the lab shows both.
Fencing. A node that cannot reach a majority of the nodes stops accepting writes. Turn Fencing on: Node 1 stops accepting writes 100 ms after the cut, at 600 ms, and refuses writes 4, 5 and 8 instead of acknowledging them. Nothing acknowledged is lost, and there are no two primaries at all. The three writes fail, and the clients know it. The reason a majority is the test is that two disjoint groups cannot both have one. In the lab, the fence takes effect 100 ms after the cut, and a write acknowledged in that window would still be lost, although the script sends none.
Waiting for replicas. With synchronous or semi-synchronous replication, the cut-off primary cannot acknowledge writes, because it cannot reach the replica it needs. Writes 4, 5 and 8 are never acknowledged, and nothing acknowledged is lost. Notice what does not change: there are still two primaries for 800 ms. The cut-off one has only stopped promising. The writes it took are discarded on the repair, and nobody had been told they succeeded.
| Approach | Writes 4, 5 and 8 | Acknowledged writes lost | Time with two primaries |
|---|---|---|---|
| Asynchronous, no fencing | Acknowledged | 3 | 800 ms |
| Fencing | Refused | 0 | none |
| Semi-synchronous or synchronous | Never acknowledged | 0 | 800 ms |
Fencing turns lost writes into failed writes, which is what you want when the choice is between an error and a lie. It has a price: the cut-off primary is unavailable for writes even though it is healthy, and its clients get errors until the partition ends. Which of the two to prefer depends on the application, and it is a decision to make before the partition.
Check your reading of the table.
Check your understanding: split brain
This quiz needs JavaScript to check your answers.
What happens if nobody had to decide?
Section titled “What happens if nobody had to decide?”The cluster would still need an answer to “who is the primary now?” The lab uses two ideas for it: an agreed number that grows with each promotion (it calls it an epoch, and the node with the newest one wins), and a majority test, so that two groups that cannot reach each other cannot both decide. The course returns to agreement between nodes in later modules. For now, the lesson is the cost of getting it wrong: a promotion without fencing is a way of losing writes that clients were told were safe.
- A partition makes an unreachable primary look like a dead one. If a replica is promoted while the old primary is alive, there are two primaries.
- In the lab, two nodes believe they are primary for 800 ms, and after the repair the writes that the old primary acknowledged, 4, 5 and 8, are lost.
- Fencing refuses those three writes instead, so nothing acknowledged is lost and there are never two primaries. Synchronous and semi-synchronous replication never acknowledge them, but two primaries still exist.
- The PostgreSQL documentation says the old primary must be told it is no longer primary, which it calls STONITH, to avoid data loss.
That is the end of the module’s lessons. The next page gathers them: a recap, a checklist for designing a replicated database, and a quiz that covers all five.
Your progress
Built by GhaziAlibi.
© ShardCraft. All rights reserved.