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.
Recommendations slow (2 s a call) from 10 s to 25 s
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.
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.
One run: 100 writes a second for 30 s
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.
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
- Assume a replica copies the mistake. Keep a backup somewhere the database cannot write to, and test that it restores.
- 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.
- Keep a base backup and the archived log. Together they rebuild any moment since the backup.
- 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.
- Time the restore at full size. A restore of a small database is a fast lower bound, not an estimate.
Quiz
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.Why did the cart, which never calls recommendations, get only 19.5% of its requests answered within one second in the shared pool?
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.