Key takeaways
- ✓A commit can wait for more or less before the database says it's saved. On one laptop, the typical commit took 0.03 ms waiting for nothing, 0.23 ms for the primary's own disk, 0.36 ms for a replica's disk and 0.46 ms until a replica showed it to readers. Sixteen writers got 22,492 commits a second waiting for the primary's disk, and 10,480 waiting for the replica to show it: about half.
- ✓Every wait for a replica adds a trip to it. Here the replica was a container on the same laptop; between data centers, each of those waits includes a network round trip of milliseconds.
- ✓Naming one synchronous replica makes it part of every write. Paused for 8 s, it stopped every commit for 8.1 s. Nothing failed, because Postgres has no timeout for that wait: the four commits in flight just hung, then finished together.
- ✓Waiting for any one of two replicas (ANY 1) kept writing through the same pause, with a longest gap of 73 ms: the other replica confirmed instead, and every confirmed commit was still on two machines.
- ✓In a quorum store, a write is confirmed by W of the N copies and a read keeps the newest of R answers. With N = 3, the settings with R + W ≤ 3 gave 4.7–22.9% stale reads; the three with R + W > 3 gave none in 46,972 reads. When R + W > N, every read set shares a copy with every write set.
- ✓W sets the write cost: a write waits for the W-th fastest copy, 7.7 ms at the median with W = 1, 13.2 ms with W = 2 and 18.9 ms with W = 3, with up to 20 ms of delay injected on purpose.
- ✓With one of three copies switched off, R = 2, W = 2 kept reading and writing with no stale reads. W = 3 confirmed none of 725 writes (but the live copies kept them anyway), and R = 3 answered none of 4,220 reads. Pick R and W for the failure you must survive.
One copy of a database was paused for eight seconds, on purpose. Nothing crashed and nothing failed, but for 8.1 seconds not a single write finished. With one setting changed and the same pause, the longest gap between writes was 73 milliseconds. This chapter measures what a database waits for before it says a write is saved, what each wait costs, what happens when the copy it waits for stops, and then the same question in a store with no leader at all: how many copies to wait for, for writes and for reads.
The examples come from Lab 09 (Replication and Consistency) of the system design labs, scripts 02_sync_replication.py (what a commit costs at each synchronous_commit level, and what a paused replica does to writes) and 05_quorum.py (a three-node quorum store written from scratch for the lab: stale reads for each read and write setting, then one node switched off), with the lab's modules quorum.py (the quorum coordinator), service/kv_node.py (one node of the store), staleness.py and consistency.py (which reads were stale, and a confidence interval for the rate). Chapter 16 covers the same lab's other scripts: how far behind a replica is (01_lag.py), reading your own writes (03_read_your_writes.py) and monotonic reads (04_monotonic_reads.py). The setup is the earlier chapters' laptop (an Apple M4 Max, Docker Desktop with 16 virtual CPUs): Postgres 18.6 with one primary and two replicas, and the store's three nodes, all in Docker. The lab's 367 unit tests (the quorum rule, the coordinator's waits, a simulated quorum store, the interval and staleness code) run in about 2 seconds without Docker; they all pass. Other work kept the laptop busy (a load average, the number of processes wanting a core, of 8–12 during the Postgres runs and 20–29 during the quorum runs), and each row below is one run, so read the shapes, not the last digit.
A head office and its branches
Replication keeps copies of a database on more than one machine. In Postgres, one database, the primary, takes every write. It records each change in its write-ahead log (WAL), the file of changes it writes before anything else (see chapter 7). Replicas receive that log over the network and replay it, applying the same changes, so they hold the same data a little later and can serve reads. Here there are two: replica1 with a full CPU, and replica2 with a quarter of a CPU, a smaller machine.
A commit is the moment the database says a write is saved. The question is what it waits for first. By default, the primary waits for its own disk and nothing else: the replicas catch up later. That's asynchronous replication. If the primary is lost before they catch up, the last commits it confirmed are gone. Synchronous replication makes a commit also wait for a replica.
Think of a head office with branch offices. The head office keeps the ledger; every new page is sent to the branches, and each branch files a copy. The clerk at the head office tells the customer "saved". The question is when.
Postgres has two settings for it:
synchronous_standby_names: which replicas a commit waits for. Empty means none (asynchronous).'replica1'means that one.'ANY 1 (replica1, replica2)'means any one of the two.synchronous_commit: how far a commit waits. With a replica named:
| Level | The commit waits until… | In the office |
|---|---|---|
off |
nothing, not even the primary's disk | "Saved" at once |
local |
the primary has it on its disk, no replica | Filed at the head office |
remote_write |
the replica has received it (in memory) | The branch received the copy |
on |
the replica has it on its disk | The branch filed it |
remote_apply |
the replica has replayed it, so a query there sees it | The branch shows it to its readers |
off is the setting chapter 7 crashed Postgres with: the WAL is flushed a moment later, usually within 200 ms, so a crash can lose commits that were already confirmed.
What "saved" costs
The lab times single-row inserts at each level, with replica1 as the synchronous replica: 2,000 commits from one connection (one writer, each commit waiting for the last), then 8,000 over 16 connections at once. The first row names no replica.
| What the commit waits for | 1 writer: p50 | 1 writer: p99 | 16 writers: p50 | 16 writers: commits a second |
|---|---|---|---|---|
| primary's disk, no replica named (async) | 0.21 ms | 0.74 ms | 0.66 ms | 22,230 |
off: nothing |
0.03 ms | 0.42 ms | 0.18 ms | 58,967 |
local: primary's disk |
0.23 ms | 0.45 ms | 0.65 ms | 22,492 |
remote_write: replica received it |
0.23 ms | 0.79 ms | 0.82 ms | 17,511 |
on: replica's disk |
0.36 ms | 1.20 ms | 1.27 ms | 11,053 |
remote_apply: replica replayed it |
0.46 ms | 2.88 ms | 1.31 ms | 10,480 |
(p50 is the median, the time half the commits beat; p99, the time 99 in 100 beat.)
Each level waits for one more thing, and each costs a little more. For one writer, from about a quarter of a millisecond for the primary's own disk to about half a millisecond for the replica to replay it. With sixteen writers, waiting for the replica to replay cut throughput to 10,480 commits a second from 22,492: about half.
off was about 9× quicker than the primary's own disk for one writer (0.025 against 0.231 ms before rounding) and 2.6× the throughput with sixteen. It's a real option for data you can afford to lose, and nothing else.
The replica was next door. On one laptop, a trip to the replica takes a fraction of a millisecond. Between data centers, every wait that involves a replica includes a network round trip, typically milliseconds: that's the cost to plan for, not these numbers.
Only remote_apply lets a user read their own write from the replica. The other levels mean the replica has the change, not that a query there would see it. Chapter 16 measures how often a read from a replica misses a write without it.
What the head office (the primary) waits for · the setting is synchronous_commit
synchronous_commit = on · synchronous_standby_names = 'replica1'
The branch has the copy on its own disk too. This is what “on” means when a branch is named, and what the freeze test below uses.
One writer: typical save (p50), ms
Single-row INSERTs on the primary: 2,000 on one connection, then 8,000 over 16 connections at once. One run per level on one laptop, where the branch is a container next door: between data centers every wait for a branch adds a network round trip. With no branch named at all (plain async replication, the setting on) the same test gave 0.21 ms and 22,230 saves a second.
Freeze one branch
Now the failure. Four connections commit as fast as they can for 16 seconds (each waits for its commit before sending the next), at synchronous_commit = on. About 4 seconds in, the lab pauses replica1 for 8 seconds with docker pause, which freezes every process in its container. Three ways to name the replica:
synchronous_standby_names |
Commits in 16 s | Longest stretch with no commit | Slowest commit | Failed |
|---|---|---|---|---|
'' (none: async) |
131,180 | 35 ms | 92 ms | 0 |
'replica1' |
33,589 | 8.10 s | 8,100 ms | 0 |
'ANY 1 (replica1, replica2)' |
81,672 | 73 ms | 74 ms | 0 |
With one named synchronous replica, writes stopped for the whole pause. Every commit waited for replica1. The four commits in flight when it froze waited 8.1 s and finished together when it came back. Nothing failed: Postgres doesn't time out a commit waiting for a replica, and the lab's client allowed each commit 30 s. With a 5-second client timeout, those four would have failed instead. Either way, the replica you named is now part of every write's availability.
Waiting for any one of two didn't notice. replica2 confirmed the commits while replica1 was frozen, even on a quarter of a CPU: the longest gap was 73 ms. Every confirmed commit was still on two machines, the primary and one replica.
Asynchronous replication didn't notice either, and got the most commits. The price is the one it always has: a commit confirmed during the freeze was on the primary only.
The commit counts also differ outside the freeze: each commit in the ANY 1 row still waited for a replica's disk, which costs a trip even on one laptop.
synchronous_standby_names = 'replica1' · synchronous_commit = on
Every commit waited for replica 1. The 4 commits in progress when it froze waited 8.1 s, then finished together when it came back. Nothing failed: Postgres has no timeout for this wait, and the lab's client allowed 30 s. With a 5 s client timeout, they would have failed instead. 33,589 commits in all, a quarter of the async run.
Schematic: the lab records the length of the longest gap, not when it started, and the pause's position in the run is approximate (the host's docker pause and the probe's clock aren't lined up). Counts and times are recorded, one run per setting. Throughput differs between rows outside the freeze too, because each wait for a replica is a round trip, even on one laptop.
Three branches, no head office
Some databases go further and drop the leader. Dynamo-style stores (Amazon's Dynamo, Cassandra, Riak) keep N copies of every key on different nodes. Whoever coordinates a request (one of the nodes, or in the lab the client itself) sends a write to all N copies and calls it done once W of them confirm. A read asks all N and keeps the newest version among the first R answers. R and W are numbers you pick, often per request. A setting here means a pair: R and W.
The lab wrote a small store for this (service/kv_node.py, one node; quorum.py, the coordinator) with N = 3: three branches, each with a full copy, and no head office. In the analogy, whoever sends the page is the clerk. The coordinator, shortened:
async def write(self, key, version, value, w):
tasks = [spawn(self._call("PUT", f"{n}/kv/{key}?version={version}&value={value}")) for n in self.nodes]
return await first_k(tasks, w) # done when W nodes confirm; the rest keep going
async def read(self, key, r):
tasks = [spawn(self._call("GET", f"{n}/kv/{key}")) for n in self.nodes]
answers = await first_k(tasks, r) # the first R answers
return newest(answers) # the highest version among them
spawn starts each request in the background, all three at once. first_k waits until k of them have succeeded and returns their answers; if too many fail for k to be reached, it raises an error. A write that isn't needed for the confirmation keeps travelling to the slower nodes, which is how they catch up.
To make the copies briefly out of step, as real replicas are, each node waits a random 0–20 ms before it applies a write and answers (a delay injected on purpose). Reads have no delay. Each node keeps a write only if its version is newer than what it holds.
How often a read was old
One writer writes versions 1, 2, 3… of one key, aiming for 100 writes a second (it waits for each write, so with a larger W it managed fewer: 1,055 to 2,000 in 20 s). Four readers read it in a loop. 20 seconds per setting. A read is stale (old) if it got a version older than the newest write already confirmed when the read was sent.
| R, W | R + W > 3? | Stale reads | Rate (95% interval) | Write p50 | Write p99 | Read p50 |
|---|---|---|---|---|---|---|
| R = 1, W = 1 | no | 3,338 of 14,600 | 22.9% (22.2–23.6%) | 7.7 ms | 26.0 ms | 2.0 ms |
| R = 1, W = 2 | no | 689 of 14,579 | 4.7% (4.4–5.1%) | 13.2 ms | 27.2 ms | 1.7 ms |
| R = 2, W = 1 | no | 682 of 11,489 | 5.9% (5.5–6.4%) | 8.7 ms | 25.3 ms | 3.4 ms |
| R = 2, W = 2 | yes | 0 of 16,136 | 0% (0–0.024%) | 12.8 ms | 23.0 ms | 1.8 ms |
| R = 1, W = 3 | yes | 0 of 14,906 | 0% (0–0.026%) | 18.9 ms | 35.6 ms | 2.1 ms |
| R = 3, W = 1 | yes | 0 of 15,930 | 0% (0–0.024%) | 7.3 ms | 20.7 ms | 1.9 ms |
The 95% interval is the range the true rate probably lies in, given this many reads (the lab uses the Wilson interval, which stays sensible near 0%). Zero stale reads in 16,136 isn't "never": it says the rate is probably below 0.024%.
R + W > N was exactly the line. Below it, 4.7–22.9% of reads were stale. Above it, none in 46,972 reads.
Why. With R = 1 and W = 1, a write can be confirmed by branch 1 while the read's first answer comes from branch 3, which hasn't applied it yet. They never meet. With R = 2 and W = 2, any two branches that confirmed a write and any two that answered a read share at least one branch: there are only three. So the read always includes a copy with the newest confirmed write, and keeping the newest answer picks it. In general: if R + W > N, every set of R copies overlaps every set of W copies. The lab's tests check this against every possible read and write set for N = 5.
The price is on the writing side. A write waits for the W-th fastest copy: 7.7 ms at the median with W = 1, 13.2 ms with W = 2, 18.9 ms with W = 3, which waits for the slowest copy every time. Reads cost little here because reads have no injected delay; R = 2, W = 1's higher read time (3.4 ms) is the one exception, and the lab doesn't explain it.
R + W = 1 + 1 = 2 ≤ 3: the branches that confirmed the write and the branches that answered the read can be different ones. In the worst case, drawn here, the read misses the write.
The recorded run · 4 readers, one writer aiming for 100 writes a second, 20 s
3,338 of 14,600 reads missed a write that was already confirmed (95% interval 22.2%–23.6%). The fastest setting, and the one with the most stale reads. 1,957 writes in 20 s: the writer waits for each one, so it managed 98 a second.
The picture is the worst case the rule is about, not a recorded trace. A store written for the lab: three nodes in Docker, each waiting a random 0–20 ms (injected) before applying a write. A read is stale if it got a version older than the newest write confirmed before it was sent. Counts are recorded, one run per setting.
One branch switched off
Last, the lab stops the third node (kv3, the lab's name for it) and runs four settings for 8 seconds each:
| R, W | Writes confirmed | Reads answered | Stale reads |
|---|---|---|---|
| R = 1, W = 1 | 800 of 800 | 7,344 of 7,344 | 14.0% |
| R = 2, W = 2 | 519 of 519 | 7,756 of 7,756 | 0% |
| R = 1, W = 3 | 0 of 725 | 6,024 of 6,024 | (no confirmed write to compare with) |
| R = 3, W = 1 | 799 of 799 | 0 of 4,220 | (no read answered) |
R = 2, W = 2 kept both reads and writes working, with no stale reads. With two copies left, a write can still get two confirmations and a read two answers, and 2 + 2 > 3 still guarantees the overlap. That's why it's the usual default for N = 3. It got slower: every write now waited for both remaining copies, slow one included (median 15.9 ms, against 12.8 ms with all three up), so the single writer managed 519 writes in 8 s where R = 1, W = 1 managed 800.
R = 1, W = 1 kept working, with 14.0% of reads stale.
W = 3 couldn't confirm a single write, and the writes happened anyway. The coordinator sends every write to all three nodes and never takes one back when it can't confirm it. So nodes 1 and 2 stored all 725 writes, and readers saw versions that had officially failed. The lab's script records 0% stale for this row, but only because there was no confirmed write to compare against: that zero means nothing. A real system would need a way to clean those writes up, or the client would retry a write that was in fact stored (chapter 10 is about making that safe).
R = 3 couldn't answer a single read. Writes were fine.
Waiting for every copy buys certainty until one of them is gone. Choose R and W for the failure you have to survive: with three copies, R = 2, W = 2 survived one gone.
Caveats
- One run per row, on one busy laptop. The replica and the store's nodes were containers on the same machine, so every network trip was a fraction of a millisecond. Between machines and data centers, the cost of waiting for a copy grows with the round trip; the ranking should hold, not the numbers.
- The quorum store's 0–20 ms delay is injected, to make copies fall out of step the way a slow network or disk would. The stale rates depend on it and on how fast the readers read; the zero above the line doesn't.
- The freeze was a
docker pause, which stops a replica cleanly. A dead disk or a network partition looks the same to the primary: a replica that doesn't answer. - The freeze chart is a schematic. The lab records the longest stretch with no commit, not when it started, and the pause's position in the run is approximate.
- R + W > N is not the whole story. Two clients writing the same key at once need a way to order versions (here one writer numbers them). Real Dynamo-style stores add read repair (writing the newest version back to a stale copy a read found), hinted handoff (another node holds writes for a dead one) and sloppy quorums (counting any N nodes, not the key's own), which trade the overlap guarantee for availability. None of these is measured here.
- Not covered: the lab's setup check (
00_setup.py) and its scripts on lag, read-your-writes and monotonic reads, which are chapter 16.
The checklist
- Know what your commit waits for. Write down
synchronous_commitandsynchronous_standby_namesfor each database, and what can be lost at that level if the primary dies. - Price the waits on your network. Every wait for a replica adds a round trip; measure it between the machines you actually use.
- Never depend on one named synchronous replica. Name a quorum (
ANY 1of at least two), alert when a synchronous replica stops confirming, and set client timeouts so a hang shows up as an error. - Mix levels per transaction if some writes matter more:
remote_applyfor one the next page must show,offfor one you can lose. - In a quorum store, keep R + W > N for anything that must not read old data, and measure the stale rate for any setting below it.
- Choose R and W for the failure you must survive. With N = 3, R = 2, W = 2 survives one copy gone for both reads and writes; W = 3 or R = 3 doesn't.
- Treat a write that wasn't confirmed as maybe stored. Make retries safe to repeat.
Practice: the quorum rule, the coordinator and the freeze
Write the lab's logic: the quorum rule (and a brute-force check of it, as the lab's tests do for N = 5), the coordinator's "first k answers" without asyncio, the newest answer, which reads were stale (returning nothing, rather than 0, when no write was confirmed), the Wilson interval, which settings survive a node down, and the longest stretch with no commit. The tests use the recorded counts: the intervals for 3,338 of 14,600 and 0 of 16,136, and the one-node-down outcomes.
Quiz
1.What does synchronous_commit = remote_apply wait for that synchronous_commit = on doesn't?
2.Postgres names one synchronous replica, and that replica is paused for 8 s. What happens to writes?
3.With synchronous_standby_names = 'ANY 1 (replica1, replica2)', replica1 is paused for 8 s. What did the lab measure?
4.A store keeps N = 3 copies of each key. Why does R = 2, W = 2 never return a stale read, while R = 1, W = 1 did on 22.9% of reads?
5.In the lab's store, what did raising W from 1 to 3 cost?
6.One of three copies is switched off. Which setting kept both reads and writes working with no stale reads?
Check your understanding
Try each question before opening its answer.
What does each synchronous_commit level wait for, and what did the extra waits cost on one laptop?
off waits for nothing; local for the primary's own disk; remote_write until the replica has received the change; on until the replica has it on disk; remote_apply until the replica has replayed it, so a read there sees it. One writer's typical commit went from 0.03 ms (off) to 0.23, 0.23, 0.36 and 0.46 ms; sixteen writers got 22,492 commits a second at local and 10,480 at remote_apply. Between data centers, each wait for a replica would add a network round trip.
Your only synchronous replica's disk dies at 3 a.m. What do users see, and what should you have configured?
Every write hangs: Postgres has no timeout for the wait, so commits don't fail, they just don't finish. In the lab, a paused replica stopped every commit for 8.1 s. Name a quorum instead, ANY 1 of at least two replicas: the same pause then left at most 73 ms without a commit. Alert when a synchronous replica stops confirming, and set client timeouts so a hang becomes an error you see.
Why is asynchronous replication not simply better, since it didn't notice the freeze and had the most commits?
Because a commit confirmed while the replica was frozen existed only on the primary. If the primary had died then, those confirmed writes would have been lost. ANY 1 of two kept writing through the same freeze and still had every confirmed commit on two machines.
In a store with three copies, why does R + W > 3 stop stale reads? Explain with R = 1, W = 1 and R = 2, W = 2.
With R = 1, W = 1, a write can be confirmed by one copy and the read answered by another that hasn't applied it yet: 22.9% of reads were stale in the lab. With R = 2, W = 2, any two copies that confirmed and any two that answered share at least one, because there are only three; the read keeps the newest answer, which includes the newest confirmed write. All three settings with R + W > 3 had no stale read in 46,972.
If R = 1, W = 3 and R = 3, W = 1 are both consistent, why is R = 2, W = 2 the usual choice for three copies?
Because of failures. With one copy switched off, W = 3 confirmed none of 725 writes and R = 3 answered none of 4,220 reads. R = 2, W = 2 kept both working with no stale reads. It cost speed: each write waited for both remaining copies, 519 writes in 8 s instead of 800.
With one copy down and W = 3, no write was confirmed. Were the writes lost?
No, and that's the trap. The coordinator sent each write to all three copies and never took one back, so the two live copies stored every write and readers saw versions that had officially failed. The lab's script reported 0% stale reads there, but only because there was no confirmed write to compare against. A client that retries a failed write must expect it may already be stored.
Next up: Chapter 18, sharding and consistent hashing. Replicas copy all the data to every machine, so they help reads but not writes or storage. When one database is too big, the data has to be split.