Key takeaways
- ✓Split brain needs no bug: when the primary was cut off and one client still pointed at its address, the old primary kept saying yes to that client, 1,212 to 1,231 times per run, after the replica had been promoted and the lock had moved.
- ✓Rebuilding the old primary as a copy of the new one, the only way back to one history, threw away every acknowledged write only it had: 2,814 to 2,916 per run without fencing, from both clients together.
- ✓Self-fencing at 0.6 × the 2-second lease makes the old primary read-only 1.2 seconds after its last renewal, before the lease can run out, and with it on the pinned client got zero yeses after the promotion in all five runs.
- ✓Fencing stops two primaries but not the loss: 195 to 212 acknowledged writes per run were still gone, because they were accepted in the window before the fence, when the replica could no longer receive them.
- ✓Self-fencing trusts the old primary's clock, which this lab did not test; a fencing token, a number that only goes up and that the storage checks on every write, does not depend on clocks, and building one is an exercise in the lab rather than something measured here.
The primary database was cut off from its replica and from etcd for 12 seconds. The replica took over, as designed. But one client still had the old address, and the old primary kept saying yes to it, about 1,200 times per run, after it had been replaced. Fencing, where the old primary stops itself first, stopped the second primary. It did not stop the loss: about 200 writes per run were still gone. This chapter takes apart what each part of that means, and why the fix is partial.
The examples come from Lab 18 (Failure, Failover and Recovery) of the system design labs, script 02_split_brain.py, and the failover controller's rule in election.py. The lab runs two Postgres 17 nodes, node1 and node2, each with a small controller that owns its database. They share an etcd 3.5 cluster, which holds the leader key on a lease. Toxiproxy, a proxy that can cut a network link on command, sits between each node and etcd, and between the two nodes. Everything ran in Docker on one Apple M4 Max laptop. The lab's README puts the whole lab at about 70 minutes; this exercise takes about 5.
The setup: a leader key on a lease
Only one of the two databases may accept writes. That is the primary. It sends each change to the other node, the replica, just after it says yes to the client. The primary holds a key in etcd, named /pg/leader, attached to a lease. A lease is a timer: the key disappears when the lease runs out, unless the holder renews it. The lab's TTL (time to live) is 2 seconds, and the controller renews every quarter-second.
When the primary stops renewing, the lease runs out after 2 seconds, the key is free, and the replica takes it and promotes itself. The controller's rule for this is short. A primary that sees another node holding the key fences if fencing is on. A primary that cannot reach etcd fences itself at 0.6 × the TTL after its last renewal. A replica takes the key when nobody holds it.
The test
The test cuts the primary off from etcd and from its replica for 12 seconds. It is still running, and clients can still reach it. It has only lost its links to the two things that decide who is primary and to its copy. That is a network partition.
Two clients write 100 rows a second each, for 20 seconds:
- Client A finds the primary by asking etcd. Like many clients, it asks again only after one of its writes fails.
- Client B is pinned to the old primary's address, like an application that looked the address up once and cached it.
Each run then checks what survived. The rows each node holds are compared after the partition heals, and before the old primary is rebuilt. The lab ran five runs with fencing off and five with it on.
Nothing stops the cut-off primary. After the replica promotes, the old primary keeps saying yes to the pinned client, and after the heal it is still a primary.
The fence and lease marks come from the controller’s settings; the promotion mark is the recorded median.
| Run | Promoted after | Yes after promotion | Lost on re-clone |
|---|---|---|---|
| 1 | 2.37 s | 1,218 | 2,814 |
| 2 | 2.29 s | 1,230 | 2,841 |
| 3 | 2.65 s | 1,231 | 2,916 |
| 4 | 2.35 s | 1,231 | 2,854 |
| 5 | 2.53 s | 1,212 | 2,865 |
Yes after promotion counts the pinned client only: 1,212 to 1,231 per run. Lost on re-clone counts both clients’ acknowledged writes that only the old primary held.
What the old primary did without fencing
The replica was promoted 2.37 seconds after the cut began (median). The old primary did not know it had been replaced: it could not reach etcd, so it never saw the new leader. So it kept accepting writes, and it kept saying yes to client B, which was still pointed at it.
- Client B was told yes about 1,200 times per run after the promotion: 1,212 to 1,231 in the five runs. That count includes writes after the heal, because the old primary stayed a primary the whole time.
- Client A's writes did not fail, so it never asked etcd again. Both clients stayed on the old primary, and from that moment the two nodes were taking writes independently.
- When the heal came, the old primary was still a primary. The lab's table shows
primaryin every unfenced run.
Rebuilding it throws the history away
Two primaries that both took writes have two histories, and Postgres cannot merge them. To get back to one history, the lab rebuilt the old primary as a copy of the new one, a step the lab calls a re-clone. That threw away every acknowledged write that only the old primary held. Without fencing, that was 2,814 to 2,916 per run, from both clients together.
The count is about what the arithmetic predicts. The writers ran at 200 a second in total for about 14 seconds after the cut began: the 12-second cut, plus the 2 seconds the test kept writing after it ended. That is about 2,800 writes, the same order as the loss.
Fencing: the old primary stops itself
The old primary cannot hear etcd, so it cannot learn it was replaced. But it does know when it last renewed its lease. The rule uses that: once more than 0.6 × the TTL has passed since the last renewal, the primary fences itself. With a 2-second lease, that is 1.2 seconds.
That is well before the lease can run out at 2 seconds, which is when the replica is allowed to take over. The controller checks every quarter-second, so it fences in the gap between the cut and the lease's end.
To fence, the controller makes new writes read-only by default and cuts off every client connection. Client B's next write is refused.
The workflow and the results
Each row is a recorded value from data/results/02_split_brain.json. The promotion times are measured from the moment the cut began.
| Fencing | Replica promoted after (median) | Yes to the pinned client after promotion | Rows only the old primary had, lost on re-clone | Old primary after the heal |
|---|---|---|---|---|
| Off | 2.37 s | 1,212–1,231 per run (6,122 in five runs) | 2,814–2,916 per run (14,290 in five runs) | still primary |
| On | 2.63 s | 0 in all five runs | 195–212 per run (1,008 in five runs) | fenced |
Two readings from the table matter most:
- With fencing on, the pinned client's yeses after the promotion were zero in every run. The fence came at 1.2 seconds, before the promotion at 2.6 seconds (median), so the old primary was no longer taking writes by the time the new one was promoted.
- Fencing reduced the loss by about 14 times, but not to zero. Rows lost fell from 14,290 to 1,008 across the five runs. The 195 to 212 per run that remain were accepted before the fence.
Lab 18's first exercise ran the same kind of partition, with writes at 200 a second and asynchronous replication, and lost 162 to 202 per run. The two numbers are close because they are the same mechanism: writes accepted by a primary the replica cannot reach.
What fencing does not fix
Fencing decides who may accept writes. It does not decide what happened to the writes already accepted. In the lab, the old primary accepted about one second's worth of writes before it fenced itself (195 to 212 per run, at 200 a second). The replica never received them, because its link was cut. Those writes were acknowledged to clients and are gone after the re-clone.
Losing nothing needs a different rule: the primary must not say yes until a copy has the write. Lab 18's first exercise tests synchronous replication, including a strict mode that never drops the copy, and strict mode has a cost. With only two nodes, a strict primary stops taking writes when its only copy is cut off.
The assumptions, and a stronger fix
Self-fencing assumes the old primary's clock runs at about the same rate as everyone else's. If the old primary's clock ran slow, it would fence late, and for a moment there could be two primaries again. The lab did not test a slow clock.
The stronger fix is a fencing token: each new leader gets a number that only goes up, and the storage refuses any write that carries an older number. Storage then decides, not the clock. The lab's controller records the etcd revision as its term, but the storage does not check it, so this is not a fencing token yet. The README's second exercise asks you to build one.
Caveats
- One set of runs, on one laptop. Five runs per setting, a 12-second cut, and two clients at 100 writes a second each. The numbers are recorded results from that setup, not a production measurement.
- Client A's behavior was not recorded separately. The lab counts the pinned client's yeses and the rows each node held. That client A's later writes landed on the new primary is inferred from the counts, not measured.
- The 1,212–1,231 count is not only the cut period. It counts every yes from the pinned client after the promotion, including the time after the heal.
- The fence timing was not measured. The 1.2 seconds is the controller's setting. The cut and the controller's last renewal are not timed against each other in this run.
- The two cuts are not simultaneous. Toxiproxy cuts the etcd link and the replication link in two separate commands, a few hundred milliseconds apart.
- The loss counts assume the script counts correctly. The first run of the script merged the row ids of the two writers and undercounted the loss by about half. The script now counts (writer, id) pairs, and the numbers above are from the rerun.
- Not covered: a slow clock, fencing tokens, three nodes, and strict synchronous replication in this exercise. The lab's README lists fencing tokens as an exercise.
The checklist
- A partition and a client that didn't move is enough. Two primaries need no bug, only a cut link and a cached address.
- Fence before your lease can run out. Stop accepting writes at a fraction of the TTL, measured from your last successful renewal.
- Expect to lose what was accepted before the fence. Fencing stops new writes, not the ones already acknowledged.
- Rebuilding the old primary discards its history. Count what it held alone before you rebuild it, and treat that as the loss.
- Trust the storage, not only the clock, for the final word. A fencing token, checked by the storage, closes the gap that a slow clock leaves.
Quiz
1.With fencing off, the replica has been promoted, and a client still connected to the old primary keeps writing. What does that client get back, and what happens to those writes?
2.Fencing was on, and about 200 acknowledged writes per run were still lost. Why?
3.Why does the primary fence itself at 0.6 × the lease, after its last renewal, rather than waiting for the lease to expire?
Check your understanding
Try each question before opening its answer.
With fencing off, what made the old primary keep saying yes after the replica was promoted, when no part of it was broken?
It was alive and reachable by client B, which still had its address, but it could not reach etcd or its replica. So it never learned that the lease had moved, and it kept accepting writes as a primary. A partition and a client that didn't move were enough. Client A, which only looks up the primary after a failed write, never failed, so it never moved either.
Fencing was on, and the pinned client got zero yeses after the promotion. Why were 195 to 212 writes per run still lost?
Because they were accepted before the fence. The old primary fenced itself 1.2 seconds after its last renewal, and until then it accepted writes that the replica never received. Fencing stops new writes after the fence. It doesn't undo the writes accepted before it.
The primary fences at 1.2 seconds, but the lease doesn't run out until 2 seconds. Why not fence at 2 seconds, to avoid any unnecessary fence?
Because the replica may take the lease the moment it expires. If the old primary waited until then, both nodes could be accepting writes during the handover. Fencing before the lease ends means the old primary has stopped by the time anyone else is allowed to promote.
Why did rebuilding the old primary as a copy throw away 2,814 to 2,916 writes per run?
Because while both nodes were primaries, each had accepted writes the other never received, and Postgres cannot merge two histories. Rebuilding the old primary as a copy of the new one made the new history the only one, so the rows that only the old primary held were discarded.
Next up: Chapter 45, backups and point-in-time restore: the replica that copied a bad
DELETEin milliseconds, and the restore that undid it.