Skip to content

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.

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.

When a write is acknowledged

The primary acknowledges a write at once, then ships it to the replicas.

A primary that is cut off from a majority of the nodes stops accepting writes 100 ms after the cut.

1,750 ms
Node 1ReplicaIn syncepoch 2Node 2PrimaryAccepting writesepoch 2Node 3ReplicaIn syncepoch 2
Node 1ReplicaIn syncepoch 2Node 2PrimaryAccepting writesepoch 2Node 3ReplicaIn syncepoch 2
  • 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
The nodes at this moment
NodeRoleStateWrites applied
Node 1ReplicaIn sync1, 2, 3, 6, 7 and 9
Node 2PrimaryAccepting writes1, 2, 3, 6, 7 and 9
Node 3ReplicaIn sync1, 2, 3, 6, 7 and 9
Event log: every event of the run, in order
Every event of the run in time order. Events after the current moment are dimmed.
Time in the runEvent
100 msWrite 1 (Alice) is accepted by Node 1.
100 msWrite 1 (Alice) is acknowledged at once.
200 msNode 2 applies write 1.
200 msWrite 2 (Alice) is accepted by Node 1.
200 msWrite 2 (Alice) is acknowledged at once.
300 msNode 3 applies write 1.
300 msNode 2 applies write 2.
300 msWrite 3 (Alice) is accepted by Node 1.
300 msWrite 3 (Alice) is acknowledged at once.
400 msNode 3 applies write 2.
400 msNode 2 applies write 3.
500 msNode 3 applies write 3.
500 msThe network splits. These groups cannot reach each other: Node 1; Node 2, Node 3.
700 msWrite 4 (Alice) is accepted by Node 1.
700 msWrite 4 (Alice) is acknowledged at once.
800 msNode 2 is promoted to primary (epoch 2). Writes it holds: 3. It never received these acknowledged writes, so they are lost: 4.
900 msWrite 5 (Alice) is accepted by Node 1.
900 msWrite 5 (Alice) is acknowledged at once.
1,000 msWrite 6 (Bob) is accepted by Node 2.
1,000 msWrite 6 (Bob) is acknowledged at once.
1,100 msWrite 7 (Bob) is accepted by Node 2.
1,100 msWrite 7 (Bob) is acknowledged at once.
1,200 msNode 3 applies write 6.
1,200 msWrite 8 (Alice) is accepted by Node 1.
1,200 msWrite 8 (Alice) is acknowledged at once.
1,300 msNode 3 applies write 7.
1,300 msWrite 9 (Bob) is accepted by Node 2.
1,300 msWrite 9 (Bob) is acknowledged at once.
1,400 msRead (Alice): Node 1 answers with write 8. The answer is stale (newer acknowledged writes: 1).
1,400 msRead (Bob): Node 2 answers with write 9.
1,500 msNode 3 applies write 9.
1,600 msThe network is repaired.
1,600 msNode 1 learns that Node 2 is the primary and becomes its replica.
1,600 msNode 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 msNode 1 catches up and applies write 6.
1,750 msNode 1 catches up and applies write 7.
1,750 msNode 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.

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.

Question 1 of 3

A partition cuts the primary off and a replica is promoted 300 ms later. The old primary keeps receiving its clients' writes. You can configure the cluster in one of three ways.

Which way ends with two nodes acting as primary for a while, and yet no write that a client was told succeeded is lost?

Choose one answer.

Question 2 of 3

A cluster has three nodes. A partition separates Node 1 from Nodes 2 and 3. The rule is: a primary that cannot reach a majority of the nodes stops accepting writes.

Why is a majority the test, rather than reaching at least one other node?

Choose one answer.

Question 3 of 3

With fencing on, the cut-off primary stops accepting writes 100 ms after the partition. Its clients keep sending writes for the next 800 ms.

What do those clients get?

Choose one answer.

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.