ealeng

Episode 43 of 55 · Failover and recovery

Failover and lost writes: a killed primary lost none, a cut-off one lost 162 to 202 in every asynchronous run

A two-node Postgres primary was killed 20 times, frozen 10 times, and cut off from its lease store and its replica 10 times with asynchronous replication (and 20 more times with synchronous settings), with a write-checker recording every confirmed write. A killed primary failed over in 2.30 seconds with nothing lost. A cut-off primary, still reachable by its clients, lost 162 to 202 confirmed writes in every asynchronous run, and relaxed synchronous replication lost 165 to 220. Strict synchronous replication lost none, but stopped all writes on two nodes. The lease sets the recovery time, and a 2-second pause is enough to fail over a healthy primary when the lease is 2 seconds.

Lab 18 · Failure, Failover and Recovery11 min read
Video transcript9:00

0:00Cold open

0:01

I broke my database’s primary on purpose, while it took two hundred writes a second.

0:08

I killed it twenty times. Writes worked again after about two point three seconds, and not one confirmed write was lost.

0:17

Then I cut it off from etcd and its replica, but its clients could still reach it. It failed over just as fast, and every time it lost between one hundred sixty-two and two hundred two confirmed writes.

0:32

With synchronous replication, in a relaxed mode that gives up on a missing replica, it still lost them, every time.

0:42

So why is a database that’s alive more dangerous than one that’s dead?

0:48Intro

0:50

Hands-on system design, part forty-three.

0:53

Today: failover. How fast a replica takes over, how much data you lose, and how long to wait before deciding the primary is gone.

1:03Meet Lucía

1:05

I’m Lucía, a platform engineer. Our database has a primary, which takes the writes, and a replica, which keeps a copy. If the primary fails, the replica takes over. That’s a failover.

1:17

Two numbers describe one. The recovery time, or RTO: how long until the new primary takes writes.

1:25

And the recovery point, or RPO: how much confirmed data is lost. Here, I count it in writes.

1:33

Who’s the primary? Whoever holds a key in etcd, a small store both nodes can see. The primary renews it every quarter second. If it stops for two seconds, the key disappears and the replica takes it. That timer is called a lease.

1:50

Think of a shop. The manager phones head office every few minutes. If no call comes for a while, the deputy takes the keys. And a manager who can’t get through should lock the till. That last part is called fencing.

2:0601 · What you need

2:07

To run it yourself: Docker, for two Postgres nodes, etcd, and on each node a small failover controller, written from scratch.

2:17

The quick tests check every takeover rule, without Docker, in a couple of seconds. No cloud, nothing to pay for.

2:2702 · The test

2:28

The test: four clients write two hundred rows a second. Each finds the primary through etcd, and every write that got a “yes” is recorded.

2:39

About five seconds in, I break the primary: kill it, freeze it, or cut it off from its replica but not from its clients.

2:48

Afterwards I count: how long until writes worked again, and which writes that got a “yes” are missing afterwards.

2:57

By default the primary says yes first, and sends the copy a split second later. That’s asynchronous replication.

3:0603 · Killed

3:08

First, a kill, twenty times. Writes worked again after two point three seconds, typically.

3:15

Where does that go? Two seconds from its last renewal for the lease to run out, a moment for etcd and the replica to notice, then it takes over.

3:26

And in twenty kills, not one confirmed write was lost. On one machine, the copy most likely never fell more than a moment behind.

3:3704 · Frozen

3:39

Next, I froze the primary, every process paused, for ten seconds. The replica took over after two point four, but writes only came back after three.

3:50

That’s my test clients’ timer: a write to the frozen primary waits two seconds, then the client waits one more before looking again. Nothing was lost.

4:0205 · Cut off

4:04

Then the case that matters. I cut the primary off from etcd and from its replica, for twelve seconds. But the clients could still reach it.

4:15

It didn’t crash. How many confirmed writes do you think we lost?

4:21

The new primary took over as fast as after a kill. And every run lost between one hundred sixty-two and two hundred two confirmed writes.

4:32

It was alive, and its clients kept writing to it. It kept saying “saved”, to writes its replica would never see, until it stopped itself. Just under a second of writes, at two hundred a second: about one hundred ninety.

4:4706 · Fencing, in code

4:49

Here’s what the primary decides, every quarter second. If someone else holds the key, it fences: it stops taking writes.

4:58

And if it hasn’t renewed for one point two seconds, it fences itself. Its last renewal came a little before the cut.

5:07

That’s before the replica can take over, at two seconds. So in every run, the old one stopped before the new one started. But what it said yes to in that second exists only on the old primary.

5:2107 · Synchronous

5:23

One answer is synchronous replication: the primary confirms a write only after the replica has it too. That should lose nothing.

5:32

Cut off, with relaxed sync: every run still lost between one hundred sixty-five and two hundred twenty confirmed writes.

5:42

In the relaxed mode I built, the primary simply stops waiting when the replica disconnects. Tools like Patroni guard this through etcd: a replica it stopped waiting for isn’t allowed to take over.

5:5708 · The setting, in code

5:58

My relaxed kind waits for the replica only while it’s connected. If it disconnects, the primary stops waiting, and says yes to writes the replica never gets.

6:10

The strict kind, plain Postgres sync, never stops waiting. No replica, no “saved”.

6:1809 · Strict

6:19

Strict sync, cut off: nothing lost, in ten runs.

6:24

And no write worked again for the rest of the test. Each was waiting for the other to confirm, so neither could save a write.

6:33

With two nodes, strict sync stops all writes while it’s cut off, and by design until the lost node is rebuilt as its replica. The usual way out is a third node.

6:4610 · How long to wait

6:47

So why not a shorter lease, for a faster failover? I ran five kills each, with leases from one to ten seconds.

6:56

Writes came back a little after the lease: two point three seconds for two, five and a half for five, ten point three for ten.

7:06

And one second behaved just like two: etcd, with its default settings, won’t give a lease under two seconds.

7:1611 · A false alarm

7:17

But a primary can also be slow without being dead: a machine that stalls for a moment, a disk that hangs. So five times per lease, I froze a healthy primary for two seconds, then let it carry on.

7:31

With a two-second lease, how often do you think the replica took over anyway?

7:38

Every time: five of five. A healthy primary was fenced and had to be rebuilt as a replica, for nothing. With a five-second lease: zero of five.

7:49

So a healthy primary’s longest pause must fit well inside the lease, with room to spare, because the primary locks itself at six tenths of the lease: one point two seconds of a two-second lease. A two-second pause is already too long.

8:07Back to Lucía

8:08

So, three lessons.

8:11

Recovery time is the lease, plus a little. A shorter lease fails over faster, and more often for nothing.

8:19

A primary that’s cut off is more dangerous than a dead one: it keeps saying “saved”.

8:26

And sync loses nothing only if, without its replica, it stops writes, or never fails over to it.

8:34

Next time: what if the cut-off primary doesn’t stop itself, and a client keeps writing to it? Two primaries at once.

8:42

For today: dead is easy. Cut off is the danger.Lucía: Dead is easy. Cut off is the danger.

8:47Summary

8:49

Recovery is the lease plus a little, a cut-off primary loses writes, and sync only helps if, without its replica, it stops.

Key takeaways

  • ✓Recovery time (RTO, how long writes stop) is the lease plus a little: with a 2-second lease, a killed primary took writes again after 2.30 seconds at the median, and 5.45 and 10.33 seconds with 5- and 10-second leases.
  • ✓Recovery point (RPO, how much confirmed data is lost) was zero in all 20 kills, but a primary that was cut off, and still took writes from its clients, lost 1,856 acknowledged writes across 10 runs, between 162 and 202 in every run.
  • ✓Asynchronous replication loses a confirmed write when the primary says yes before the replica has the write, so a cut-off primary keeps saying yes to writes that never reach the replica until it fences itself 1.2 seconds after its last renewal, which here is just under a second of writes at 200 a second.
  • ✓Relaxed synchronous replication, where the primary stops waiting for a replica that has disconnected, lost 1,946 writes in 10 runs (165 to 220 each), as many as asynchronous, because it keeps saying yes alone.
  • ✓Strict synchronous replication, which never stops waiting for its replica, lost none in 10 runs, but on two nodes no write succeeded for the rest of the test, so the cost is availability, not data.
  • ✓A 2-second lease failed over a healthy primary on 5 of 5 two-second pauses, which forced a rebuild for nothing, while a 5-second lease failed over on 0 of 5, so the lease must be long enough that the primary's fencing point, 0.6 of the lease, comes after the longest pause a healthy primary takes.

A primary database was killed 20 times, frozen 10 times, and cut off from its lease store and replica in 10 asynchronous runs and 20 synchronous ones, with a write-checker recording every write the database confirmed. A killed primary failed over in 2.30 seconds and lost none of 62,773 confirmed writes. A primary that was cut off, but still reachable by its clients, lost 162 to 202 confirmed writes in every asynchronous run. This chapter covers the two numbers that describe a failover, the lease that decides who may be primary, and what each replication setting buys and costs.

The examples come from Lab 18 (Failure, Failover and Recovery) of the system design labs. This chapter uses the results of its script 01_failover.py, which breaks the primary with the test running, and of 03_detection.py, which sweeps the lease length. Both ran in Docker on one Apple M4 Max laptop: two Postgres 17 nodes, an etcd 3.5 store, and a small failover controller written for the lab. The lab's write-checker, tools/writer.py, sends 200 writes a second from four clients and records every write that got a yes. Each scenario is one set of recorded runs, not a long benchmark.

RTO and RPO

A failover has two numbers, and they measure different things.

  • RTO (recovery time objective) is how long the service is down: from the fault to the first write the new primary confirms. The lab measures it from the clients' side, because that is what users see. A write to the frozen primary waited out the client's 2-second timeout, so writes came back at 3.03 seconds even though the replica was promoted at 2.43 seconds.
  • RPO (recovery point objective) is how much data the failover loses. Here it is counted in writes: the writes the client was told succeeded that the new primary does not have.

The two trade against each other. Asynchronous replication is fast and can lose the newest writes. Synchronous replication can lose nothing, but a stalled replica then stalls the primary.

Why asynchronous replication loses confirmed writes

By default, a Postgres primary says yes as soon as the write is on its own disk, and sends the change to its replica a moment later. That is asynchronous replication. The replica usually has the write within a split second, so a primary that dies loses nothing that was confirmed. That is what the 20 kills show: 0 of 62,773 writes lost.

A primary that is cut off is a different case. Here the lab cuts it off from etcd, the small store that holds the lease, and from its replica, but not from its clients. The primary is alive, its clients still reach it, and it keeps saying yes. Every one of those yeses is a write the replica will never receive. The primary cannot know it has been replaced until it fences itself, which is when it stops accepting writes. In the lab, that took 1.2 seconds after its last successful renewal of the lease, and the writes it accepted in that second, about 190 at 200 a second, exist only on the old primary. Across 10 cut-off runs that came to 1,856 writes, between 162 and 202 in each run.

Fencing, here, means the primary stops accepting writes. It happens before the replica can take over, so in every run the old primary had stopped confirming writes before the new one started. The loss is the price of the window between the cut and the fence.

Synchronous and strict synchronous replication

Synchronous replication closes that window. The primary confirms a write only after the replica has it too, so a cut-off primary cannot say yes to a write the replica lacks. In theory that loses nothing. The lab tested two ways of doing it, and they behave differently.

  • Relaxed synchronous (the controller's own mode). The primary waits for the replica only while it is connected. When the controller sees the replica disconnect, it removes the replica from the list of synchronous standbys, named by the Postgres setting synchronous_standby_names, and the primary commits alone. In 10 cut-off runs it lost 1,946 writes, 165 to 220 each, as many as asynchronous. Production failover tools such as Patroni guard this with etcd, recording which standby is synchronous and not letting a replica that was dropped take over. The lab did not test Patroni.
  • Strict synchronous (plain Postgres). The primary never stops waiting. No replica, no yes. In 10 cut-off runs it lost none. But on two nodes the only standby is the replica that was cut off, so nothing could commit. No write succeeded for the rest of the 18-second test. With two nodes, strict synchronous replication trades all availability for zero loss while a node is missing, until the lost node is rebuilt. The usual way out is a third node, so the primary still has a standby to wait for after one failure. The lab did not build that.

So synchronous replication helps only when the primary, without its replica, stops writing or never fails over to it. The relaxed mode does neither, and it loses as much as asynchronous.

The lease: who is allowed to be primary

The primary is whoever holds the key /pg/leader in etcd. The key is attached to a lease with a time to live (TTL), the number of seconds it lives unless renewed. The primary renews the lease every tick, which is TTL divided by 4 and at most a quarter of a second, so with a 2-second lease it renews four times a second. If it stops, the lease expires after TTL seconds, the key disappears, and the replica takes it, promotes itself and becomes primary. The replica can take it only once, because only one node can create the key.

The controller's rule, in election.py, has one more step. The primary fences itself once it has not renewed for 0.6 of the TTL (the lab's FENCE_AT), which is 1.2 seconds with a 2-second lease. That is before the lease can expire, so the old primary stops first. The fencing rule is why the lease is the upper bound on RTO, and why the cut-off primary's loss is bounded by the same 1.2 seconds.

Recovery time follows the lease. In the lease sweep, with five kills each, RTO was about the TTL plus 0.3 to 0.45 seconds. The next section gives the numbers.

The workflow and the results

Failover (01_failover.py). The write-checker runs for 18 seconds at 200 writes a second, with the fault about 5 seconds in. It repeats the fault for each setting: kill (SIGKILL, the process dies at once), pause (every process frozen for 10 seconds, then resumed), and cut off (Toxiproxy, a network proxy the lab puts between each node and etcd and between the nodes, cut for 12 seconds). Afterwards it counts the writes that were confirmed and are missing on the new primary. The lab's failover_metrics.py computes RTO and the loss from the write-checker's log.

Try it · Same primary, same load: what each fault costrecorded run, 01_failover.py

The primary is alive and its clients still reach it, but it cannot reach etcd or its replica for 12 s.

Acknowledged writes lost
1,856
Runs with loss
10 of 10
RTO p50
2.30 s
Replica promoted after
2.38 s
Lost, all runs

Lost 162–202 per run across 10 runs of 33,895 acknowledged writes in total. The bar is scaled to 2,000.

Writes are 200 a second for 18 s, the fault at about 5 s, lease 2 s. RTO is the time from the fault to the first write the new primary acknowledges. "No writes" means the cluster stopped taking writes for the rest of the test.

Fault Replication Runs RTO p50 Replica promoted after (p50) Acknowledged writes lost Runs with loss
Killed asynchronous 20 2.30 s 2.38 s 0 of 62,773 0
Frozen 10 s, then alive asynchronous 10 3.03 s 2.43 s 0 of 29,960 0
Cut off (etcd and replica) asynchronous 10 2.30 s 2.38 s 1,856 (162–202 per run) 10
Cut off synchronous, drops the standby 10 2.48 s 2.56 s 1,946 (165–220 per run) 10
Cut off strict synchronous 10 no writes 2.62 s 0 0

Lease (03_detection.py). Five kills and five 2-second pauses at each lease length. The pause is the case where the primary is slow, not dead: a stall, a disk hang, a GC pause.

Try it · Choosing the lease: failover speed against false failoversrecorded run, 03_detection.py
Kill, RTO p50
2.31 s
Kill, RTO max
2.40 s
2 s pause: failed over
5 of 5
2 s pause: write gap p50
2.08 s

A failed-over pause means the healthy primary was replaced and had to be rebuilt as a replica, for nothing. Five kills and five 2 s pauses per lease.

Lease asked for Kill: RTO p50 Kill: RTO max 2 s pause: failed over Write gap during the pause (p50)
1 s (etcd granted 2 s) 2.29 s 2.75 s 5 of 5 2.06 s
2 s 2.31 s 2.40 s 5 of 5 2.08 s
5 s 5.45 s 5.69 s 0 of 5 2.06 s
10 s 10.33 s 10.65 s 0 of 5 2.04 s

The shorter the lease, the faster the failover and the more often a healthy primary is replaced for nothing. A 2-second lease is faster, but it cannot tolerate a 2-second pause, so a lease must sit above the longest pause a healthy primary can take, with room to spare.

Scope of this chapter. Lab 18 has five scripts. This chapter uses 01_failover.py and 03_detection.py. The split-brain script 02_split_brain.py is the next episode, and the bulkhead and point-in-time restore scripts (04_bulkhead.py, 05_pitr.py) are in a later one.

Caveats

  • Few runs, one machine. Each scenario is 10 or 20 runs on one laptop with both nodes in Docker. Network delay between real machines, and the lag it adds, is not in these numbers.
  • The loss in the cut-off runs is a lower bound. The lab cuts etcd and the replication link with two separate commands, and the fault time is stamped before both, so the replication link is cut a moment after etcd. An instantaneous cut would lose at least as much.
  • RTO is approximate. The fault time is taken on the Mac just before the command, and the acknowledgement times come from the containers. The replica's promotion is logged after the promotion, so the first new write often lands a few hundredths of a second before the logged time.
  • Two nodes only for strict sync. The strict result, stopping all writes, is a two-node result. Three nodes with one required standby were not tested.
  • The slow-link kill did not test what it meant to. The lab's README says a kill with 200 ms of delay on the replication link lost nothing, but in-flight data reached the replica after the primary died, so it does not measure the case it was built for. It is not reported here.
  • Not covered: fencing tokens (passing a term number with each write so storage rejects old ones), a third node, a replica that is too far behind to promote, disk failure, and multi-region failover. The lab lists fencing tokens and a third node as exercises.
  • Writes are counted, not bytes. The lost figure counts confirmed writes, one per row, and each run's writes are unique by (run, id), so a repeated id from another run is not counted twice.

The checklist

  1. Measure RTO and RPO from the client. The clients' view includes their timeouts and reconnects, which is what users see.
  2. Set the lease above the longest pause a healthy primary can take. Then fence the primary before the lease can expire, as the lab does at 0.6 of the TTL.
  3. Treat a cut-off primary as the dangerous case. It is still reachable, so it keeps saying yes until it fences itself.
  4. Check that synchronous replication stops writes when the replica is gone, or that the failover refuses to promote a replica that was dropped. Relaxed mode does neither, and it loses as much as asynchronous.
  5. Price the zero-loss setting. Strict synchronous replication lost none, but with two nodes it stopped all writes while a node was missing. Plan a third node if writes must keep flowing.

Quiz

Try it · Check your understanding0 of 3 answered
  1. 1.A primary is killed, and separately a primary is cut off from its lease store and its replica but its clients can still reach it. Both fail over in about 2.3 seconds. Which one loses confirmed writes?

  2. 2.Relaxed synchronous replication is meant to keep the primary writing when its replica disconnects. What does that cost when the replica is cut off?

  3. 3.With a 2-second lease, a healthy primary freezes for 2 seconds. What happens?

Check your understanding

Try each question before opening its answer.

A killed primary lost none of 62,773 writes, but a cut-off primary lost 1,856. Both failed over in about 2.3 seconds. What is different?

The cut-off primary is still alive and reachable by its clients, so it keeps confirming writes. Its replica never gets them, and it cannot know it has been replaced until it fences itself, 1.2 seconds after its last renewal. A killed primary takes no more writes, and every write it had confirmed had already reached the replica.

Relaxed synchronous replication was supposed to keep writes flowing when the replica disconnects. Why did it lose 1,946 writes?

Because when the replica disconnects, the controller stops waiting for it, so the primary commits writes alone and confirms them. Those writes never reach the new primary after the failover. That is the same mechanism as asynchronous replication, so the loss is the same size.

Why did strict synchronous replication lose none, and what did it cost on two nodes?

It never stops waiting for its replica, so no write is confirmed without one. With two nodes, the only replica is cut off, so no write could be confirmed for the rest of the 18-second test. The cost is availability: zero loss was bought with no writes until the lost node was rebuilt.

Why did a 5-second lease fail over on 0 of 5 pauses, while a 2-second lease failed over on 5 of 5?

A 2-second pause lasts longer than the primary's fencing point of 1.2 seconds with a 2-second lease, so the healthy primary fences itself and the replica then takes the lease. With a 5-second lease, the fencing point is 3 seconds, and the 2-second pause ends before it is reached, so the primary resumes and keeps the lease.

A 2-second lease gives a 2.31-second RTO, and a 10-second lease gives 10.33 seconds. What does that tell you about choosing a lease?

RTO is roughly the lease plus 0.3 to 0.5 seconds, so the lease is the setting that decides how long writes stop after a real failure. Choosing a shorter lease shortens the outage, but it also fails over healthy primaries that pause for longer than the fencing point. Pick the shortest lease that is still above the longest healthy pause you measure.

Next up: Split brain and fencing, the next episode: the cut-off primary does not always stop itself, and a client that keeps writing to it can leave two primaries at once.

Learning something useful?

New labs, articles, and videos on evaluating and testing AI systems come out regularly. Follow along so you don't miss the next one.