ealeng

Episode 45 of 55 · Failover and recovery

Bulkheads, backups and a restore: one slow dependency took a cart to 19.5%, and a DELETE reached the replica within the second

A shopping cart that never calls recommendations was answered in time for only 19.5% of its requests while recommendations was slow, because recommendations held all 20 workers in a shared pool. A bulkhead of 5 places for recommendations brought the cart back to 100%. Then a DELETE that matched every row removed them on the primary and on the replica alike. A base backup plus the archived write-ahead log restored all 1,968 writes confirmed before the restore target, and none from after it, but 21 confirmed writes from just before the mistake did not come back.

Lab 18 · Failure, Failover and Recovery10 min read
Video transcript8:19

0:00Cold open

0:01

Two ways to lose a service, both on purpose.

0:05

First, one slow dependency. The shopping cart never calls it, and still only one in five cart requests was answered in time.

0:16

With a wall around that dependency: all of them.

0:20

Second, a DELETE that matched far more rows than I meant. My replica, the copy I kept for safety, had lost the same rows too.

0:30

A backup brought them back. Almost all: twenty-one confirmed writes were lost, and the reason was a fraction of a second.

0:40

So what keeps one slow thing from sinking everything? And what actually undoes a mistake?

0:48Intro

0:49

Hands-on system design, part forty-five.

0:53

Today: bulkheads, why a replica is not a backup, and a point-in-time restore.

0:59Meet Lucía

1:01

I’m Lucía, a platform engineer. First, a simulation: one program with twenty workers, each handling one request at a time. The services it depends on are just timed waits.

1:14

The cart gets two hundred requests a second; each asks the inventory service, which answers fast.

1:21

Recommendations gets fifty a second. For fifteen seconds in the middle, its service takes two seconds to answer.

1:29

Clients give up after one second. That one second includes time spent waiting for a free worker.

1:37

Think of a ship. One open hull: a single leak floods everything. A bulkhead is a wall between compartments, so one leak floods only its own.

1:4901 · What you need

1:50

To run it yourself: Python for the simulation, and Docker for the second half, a real Postgres database with a replica.

2:00

The quick tests, including the bulkhead’s, run without Docker in a couple of seconds. Nothing to pay for.

2:0902 · One shared pool

2:10

First, every request takes any free worker: one shared pool. Before the slowdown, everything was answered in time.

2:19

Then recommendations turned slow. The cart, which never calls it, dropped to nineteen and a half percent answered in time. And recommendations itself: none.

2:31

Why the cart? Fifty recommendation requests a second, each holding a worker for two seconds, would need a hundred workers. There are twenty. So the slow calls kept nearly all of them busy.

2:45

So the cart’s requests waited for a free worker, and most of them waited past their client’s one second.

2:5303 · In code

2:55

The fix is a bulkhead: its own small limit for calls to recommendations, five at a time. If all five places are taken…

3:04

…the call is refused at once. It doesn’t wait in line.

3:10

And the recommendations page answers without recommendations. A place is given back when a call finishes.

3:1904 · With a bulkhead

3:20

The same slowdown, with the bulkhead.

3:24

The cart: every request answered in time. Slow recommendations could hold only five of the twenty workers.

3:33

The recommendations page answered seven hundred seventeen of seven hundred fifty-seven in time, every one of them without recommendations.

3:44

The other forty got a place, and waited two seconds: too late. Five places, two seconds each, over fifteen seconds, is about thirty-eight; forty were measured.

3:55

A partial page, quickly, instead of nothing. Back in part forty-two, a circuit breaker stopped calling a slow service altogether. A bulkhead caps how many workers a slow service can hold. They work well together.

4:1105 · A mistake

4:13

Now a real database, Postgres: a primary that takes the writes, and a replica that copies every change. First, a base backup: a full copy of the database, made in under two seconds.

4:27

Then a writer adds a hundred rows a second, for thirty seconds, and records every write the database confirms.

4:35

Twenty seconds in, the mistake: a DELETE meant for a few rows that matched them all. It removed every row the writer had added so far.

4:46

All along, Postgres writes each change to a log before applying it: the write-ahead log. Every few seconds, the log so far is copied to a separate archive. The backup plus that log can rebuild any moment since the backup.

5:0206 · The replica

5:04

Two thousand nine hundred ninety-seven writes were confirmed. A second after they stopped, the primary held one thousand and seven: only the ones written after the DELETE.

5:16

And the replica? The same one thousand and seven. It copies every change, and a DELETE is a change.

5:24

A replica protects you from a dead machine. Not from a wrong command.

5:3107 · The restore

5:32

The restore, called a point-in-time restore: a fresh, empty database, and the base backup unpacked into it.

5:41

Then replay the archived log, change by change, up to a target time. I aimed fifty milliseconds before I sent the DELETE.

5:51

Stop there, and open the database for writes.

5:5608 · What came back

5:57

The restore itself took one point two seconds. Every write confirmed before the target was there: one thousand nine hundred sixty-eight of one thousand nine hundred sixty-eight.

6:10

And none from after the DELETE: they were never part of that moment.

6:15

One point two seconds, because this database is tiny. A restore takes longer the bigger the backup, and the more log there is to replay. Until you’ve timed one at full size, its duration is a guess.

6:3109 · The gap

6:32

But look at the edge. My timestamp was taken just before I sent the DELETE, through a separate command. It reached the database a moment later.

6:43

How much later? A thousand and twenty-three writes were confirmed after my timestamp, but only a thousand and seven survived. So, by the database’s clock, the DELETE took effect about a sixth of a second after my timestamp: sixteen confirmed writes fell in that gap. The separate command took time, and my laptop’s clock isn’t the database’s.

7:06

Six were confirmed inside my fifty-millisecond margin; one made it in, five didn’t. Twenty-one in all, from before the mistake, never came back.

7:18

A restore lands exactly where you aim. Aim from the mistake’s real time, from the database’s own record of it, not from your clock. Its log shows when each change happened.

7:31Back to Lucía

7:33

So, three lessons.

7:35

Give each dependency its own compartment, so one slow service can only hold its own share of the workers.

7:44

A replica is not a backup. It copies the mistakes too.

7:49

And a restore lands where you aim: find the mistake’s real time, and time your restore at full size.

7:57

Next time: multiple regions, and what changes when the copies are far apart.

8:03

For today: walls between, backups behind.Lucía: Walls between. Backups behind.

8:08Summary

8:09

Wall off each dependency, keep a backup as well as a replica, and aim a restore from the mistake’s real time.

Key takeaways

  • ✓A slow dependency can take down an endpoint that never calls it: while recommendations took 2 s a call, the shopping cart, which only calls inventory, was answered within one second for 19.5% of its 3,095 requests, because recommendations held the shared pool's 20 workers.
  • ✓A bulkhead, which is a separate limit of five concurrent calls to recommendations, brought the cart back to 100% of its requests, and the recommendations endpoint answered 717 of its 757 requests within a second, every one of them without recommendations.
  • ✓A replica is not a backup: after a DELETE removed every row the test had written, the primary and the replica each held the same 1,007 rows, the ones written after the DELETE, because the replica applies every change, the mistake included.
  • ✓A point-in-time restore from a base backup plus the archived write-ahead log brought back all 1,968 of 1,968 writes confirmed before the target, and 0 of the writes from after the mistake, in 1.2 s on a database this small.
  • ✓The restore landed exactly where it was aimed, which was 50 ms before a timestamp taken on the client, and that cost 21 confirmed writes: 16 were confirmed between the timestamp and the moment the DELETE reached the database, and 5 more fell in the 50 ms margin.

A shopping cart that never calls recommendations was answered in time for 19.5% of its requests while recommendations was slow. A bulkhead, a separate limit of five calls for recommendations, brought it back to 100%. Then a DELETE that matched every row removed them from the primary and from the replica alike. A base backup plus the archived write-ahead log brought back every write confirmed before the restore target, but 21 confirmed writes from just before the mistake did not come back. This chapter takes the three pieces apart: the bulkhead, why a replica is not a backup, and what a point-in-time restore can and cannot recover.

The examples come from Lab 18 (Failure, Failover and Recovery) of the system design labs. The bulkhead is script 04_bulkhead.py, a simulation in one process. The backup and restore is script 05_pitr.py, which runs real Postgres 17 in Docker: a primary and a replica, with a restore script at tools/restore.sh. Both ran on the same laptop setup as the earlier chapters, an Apple M4 Max with Docker Desktop and 16 virtual CPUs. Each configuration is one recorded run. The README puts scripts 01 to 05 at about 70 minutes together; the two here take about a minute and about two minutes.

Why one slow dependency takes the cart down

A service with 20 workers serves two endpoints. Each request holds a worker while it works, and each endpoint calls its own dependency:

  • /cart: 200 requests a second, each calling inventory, which takes 5 ms.
  • /recs: 50 requests a second, each calling recommendations, which takes 10 ms, and 2 s from 10 s to 25 s into the run.

Clients give up after one second, and that second includes the time spent waiting for a free worker. Think of a ship with one open hull: a single leak floods everything. A bulkhead is a wall between compartments, so one leak floods only its own.

With one shared pool, every request takes any free worker. Before the slowdown, both endpoints were answered in time. During it, each recommendation call holds a worker for 2 s. At 50 calls a second, that is 100 workers wanted at once, and there are 20. The slow calls take nearly all of them, and the cart's requests queue behind them and miss their one-second limit. The cart calls nothing slow and still fails. In the recorded run, /cart was answered in time for 19.5% of its 3,095 requests during the slowdown.

The fix is a bulkhead: a small, separate limit on calls to recommendations, five at a time. A sixth call is refused at once instead of waiting. The recommendations page then answers without recommendations, and a place is given back when a call finishes.

Try it · Workers in one serviceLab 18 · 04_bulkhead.py · recorded run

Recommendations slow (2 s a call) from 10 s to 25 s

held by recommendations: 20 of 20free for /cart and /recs

Arithmetic, not a measurement: 50 recommendation calls a second, each holding a worker for 2 s, want 100 workers at once. The shared pool has 20, so the slow calls take them all. With a bulkhead, the calls beyond 5 are refused at once, and the rest of the pool stays free. The grid shows the setup, not a trace.

/cart answered in 1 s
19.5%
/cart requests
3,095
/recs answered in 1 s
0.0%
/recs without recs
0

Recorded: 04_bulkhead.json, recs slow phase. Before the slowdown, /cart and /recs were answered 100% in both setups. Each /recs number is out of 757 requests; the bulkhead answered the other 757 of them within the second with recommendations.

The recorded results, from the recommendations-slow phase:

Setup /cart answered in 1 s /recs answered in 1 s /recs answered without recommendations
One shared pool 19.5% of 3,095 0% of 757 0
Bulkhead of 5 for recommendations 100% of 3,095 94.7% of 757 717

Before the slowdown, both endpoints were answered 100% in both setups. The bulkhead's 40 misses on /recs were requests that got one of the five places and then took the full 2 s, which was too late. Five places, each held for 2 s over the 15 s of slowdown, cover about 38 calls. That is arithmetic. The lab measured 40.

A bulkhead trades completeness for containment: a partial page, quickly, instead of nothing. It does not stop calls to a dependency that is failing, which is a circuit breaker's job. The two are used together.

A replica is not a backup

The test then makes a mistake on purpose. The primary is a Postgres database that takes the writes, and the replica copies every change from it. The writer adds 100 rows a second for 30 seconds, and it records every write the database confirms. At 20 seconds in, a DELETE meant to remove a few rows runs without the narrowing it needed, and removes every row this run has written so far. The writer keeps going.

In the recorded run, 2,997 writes were confirmed. A second after they stopped, the primary held 1,007 rows, the ones written after the DELETE. The replica held the same 1,007. A replica applies every change within moments, and a DELETE is a change. It protects you from a dead machine, not from a wrong command.

Base backups and the write-ahead log

A point-in-time restore needs two things kept somewhere separate from the database. The first is a base backup: a full copy of the database at one moment, taken here with pg_basebackup, Postgres's own backup tool. In the run it took 1.8 s, and this database holds only the test rows.

The second is the write-ahead log, or WAL: Postgres writes each change to a log before it applies it, and the log is a complete, ordered record of every change since the backup. The lab archives it, copying each finished WAL segment (a fixed-size file of the log) to a separate archive. A setting called archive_timeout closes the current segment every 5 s so that recent changes are archived without waiting for a full one. The base backup plus the archived log can rebuild the database as it was at any moment since the backup was taken.

The restore, and what it can and cannot recover

The restore builds a fresh, empty database, unpacks the base backup into it, and replays the archived log change by change, stopping at a target time. Then it opens for writes. Here the target was 50 ms before the timestamp taken just before the DELETE. The restore script, tools/restore.sh, ran in a separate container, the same way a restore drill would.

The restore took 1.2 s. Every write confirmed before the target was there: 1,968 of 1,968. None of the writes from after the mistake were: 0. The restored total was 1,969, because one of the 6 writes confirmed in the 50 ms margin made it in.

The gap is where the restore cannot help. 21 writes that were confirmed before the mistake did not come back, for two reasons:

  • Five were in the 50 ms margin and came after the target. They were lost because the target was set before them.
  • Sixteen were confirmed after the timestamp, but before the DELETE reached the database. The timestamp was taken on the Mac, and the DELETE ran through a separate command into a container, so it landed about 0.16 s after the timestamp by the database's clock. Those 16 writes were confirmed in that gap, then deleted, and the restore target was earlier than them.

A restore lands exactly where you aim. The lesson is to aim from the mistake's real time, taken from the database's own log, where each change is recorded when it happened, and not from a client's clock.

Try it · Restore to just before the mistakeLab 18 · 05_pitr.py · recorded run

One run: 100 writes a second for 30 s

0 s30 sDELETE at 20 starget: 50 ms beforewrites confirmed, 2,997 in all
Rows on the primary
1,007
Rows on the replica
1,007
Confirmed before target
1,968
Restored in total
1,969

All 1,968 writes confirmed before the target came back (the restored total is 1,969: those, plus one of the 6 confirmed in the last 50 ms before it). Missing from before the target: 0. From after the mistake: 0. Restore took 1.2 s; backup took 1.8 s.

confirmed after the client's timestamp: 1,023 − survived on the primary 1,007 = 16 deleted by the DELETE after the stamp
confirmed in the 50 ms margin: 6 − restored from it 1 = 5 lost
5 + 16 = 21 confirmed writes from before the mistake did not come back

Recorded: 05_pitr.json. The 16 are an inference from the recorded counts: the DELETE reached the database after the client's timestamp, so those writes were confirmed before it and then removed. Restore time is for this small database only.

The workflow and the results

Bulkhead, 04_bulkhead.py. One process, using asyncio (Python's built-in concurrency library), with 20 workers and the two endpoints above. The dependencies are simulated with sleeps, so the numbers describe the model, not a network. The script runs the shared pool, then the bulkhead, and reports each endpoint's share answered within 1 s, before and during the slowdown.

Backup and restore, 05_pitr.py. Run against real Postgres 17 in Docker: take the base backup with pg_basebackup; write 100 rows a second for 30 s; at 20 s run the DELETE; check the replica; force the current WAL segment closed with pg_switch_wal so it is archived; then restore with tools/restore.sh to the target, and check the restored rows against the writer's record.

Measured Value
Writes confirmed 2,997
Rows left on the primary, and on the replica 1,007 and 1,007
Confirmed before the target 1,968
Restored, of which before the target 1,969, all 1,968 before it
Missing from before the target 0
From after the mistake, present after restore 0
Backup time, restore time 1.8 s, 1.2 s

The lab's README asks for the restore to be timed at production size. This one was 1.2 s on a database that held only the test rows, so it says little about a real restore, which grows with the base backup and the WAL to replay since it.

Caveats

  • One run per setting, and both runs are small. The bulkhead was measured once per setup. The restore was measured once, on a database of a few thousand rows.
  • The bulkhead is a model. The dependencies are sleeps in one process. The lab does not test a bulkhead across processes or machines, or the cost of its refusals under load.
  • The archive was on the same host. The archive lived in a Docker volume shared with the database. A real archive should be somewhere the database's own credentials cannot delete, such as another account or object storage with a retention lock. The lab did not test that.
  • The unarchived tail was not tested. The script forced a WAL switch before the restore, so every change was archived. A crash can lose the segment still being written, and that loss is not measured here. The archive timeout of 5 s bounds it, but the lab did not test it.
  • Restores across failovers were not tested. Restoring to a point before a promotion, where the timeline changes, is listed as an exercise in the README and was not run.
  • The timestamp gap was measured by counts, not by the database's log. The 16 and 5 come from the recorded counts and the margin. The lab did not read the database's own log of when the DELETE ran. The "about 0.16 s" is inferred from those counts, not recorded.
  • Replica lag was not measured. The replica showed the same 1,007 rows as the primary after the run. The lab did not record how far behind it was while the writes were arriving.
  • No encryption, no restore drill. The lab ran one restore by hand. Scheduling restores, verifying them, and encrypting the backups were not tested.

The checklist

  1. Assume a replica copies the mistake. Keep a backup somewhere the database cannot write to, and test that it restores.
  2. Put a wall around each dependency. A limit per dependency, a share of the workers only its calls may use, keeps a slow one from taking the endpoints that never call it.
  3. Keep a base backup and the archived log. Together they rebuild any moment since the backup.
  4. Aim from the database's own record of the mistake. Your clock is not the database's, and a few tenths of a second separate them in this test.
  5. Time the restore at full size. A restore of a small database is a fast lower bound, not an estimate.

Quiz

Try it · Check your understanding0 of 3 answered
  1. 1.A replica is kept in sync with the primary. A developer runs a DELETE without a WHERE clause on the primary. What happens to the replica?

  2. 2.Why did the cart, which never calls recommendations, get only 19.5% of its requests answered within one second in the shared pool?

  3. 3.A restore target is set 50 ms before a DELETE that the client timed on its own laptop. Writes confirmed in that gap are restored. What else can be lost, and what should the restore be aimed from?

Check your understanding

Try each question before opening its answer.

With a shared pool, why was the shopping cart answered within one second for only 19.5% of its requests, when it never calls recommendations?

Because the recommendation calls held the pool's 20 workers. At 50 calls a second, each holding a worker for 2 s, they want 100 workers, so the cart's requests waited for a free worker and most missed their one-second limit. A bulkhead of 5 for recommendations left 15 workers free for the cart, which then answered 100% in time.

The bulkhead refused the sixth recommendation call at once. What did /recs do instead, and what did the lab measure?

It answered without recommendations. Of its 757 requests during the slowdown, 717 were answered in time without recommendations, and 40 got a place but took the full 2 s, which was too late. The overall answered share for /recs was 94.7%.

After the DELETE, the replica also had only 1,007 rows. Why can't the replica serve as the backup?

A replica applies every change within moments, and a DELETE is a change, so the replica deletes the same rows. Only a backup taken earlier, with the log replayed up to a time before the mistake, brings back rows removed by a wrong command.

The restore recovered 1,968 of 1,968 writes confirmed before the target, but 21 writes from just before the mistake were still missing. How do you account for the 21?

Five were confirmed in the 50 ms margin between the target and the timestamp, so the target was set before them. Sixteen were confirmed after the timestamp but before the DELETE reached the database, which by the database's clock was about 0.16 s later. Those writes were deleted and sit after the target. Aiming from the database's own record of the change time would avoid the 16.

Next up: Chapter 46, multiple regions: this chapter's copies sat on one laptop. The next one puts them far apart, and asks what a round trip across a continent does to every write.

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.