ealeng

Episode 17 of 55 · Replication

Synchronous replication and quorums: how many copies to wait for

One Postgres replica was paused for 8 seconds. With one setting, not a single write finished for 8.1 seconds; with another, the longest gap was 73 ms. What each synchronous_commit level waits for and costs, why one named synchronous replica turns its pause into a write outage, and why waiting for any one of two doesn't. Then a three-copy quorum store: R + W above N was exactly the line between 5–23% stale reads and none, and the choice of R and W decided what survived one node down.

Lab 09 · Replication and Consistency17 min read
Video transcript8:22

0:00Cold open

0:01

I froze one copy of my database for eight seconds. On purpose.

0:07

Nothing crashed, and nothing failed. But for eight seconds, not a single save finished.

0:15

Then I changed one setting, and froze it again. The longest gap between saves: seventy-three milliseconds.

0:23

Same database, same freeze. What changed?

0:29Intro

0:30

Hands-on system design, part seventeen.

0:34

Today: what it costs to wait for copies, and how many copies to wait for.

0:40Meet Lucía

0:41

I’m Lucía, a platform engineer.

0:44

My Postgres database has a primary. It takes every write.

0:50

And two replicas that copy it. The second one runs on a quarter of a CPU.

0:56

By default, the primary says “saved” without waiting for any copy.

1:02

Think of a head office with branch offices. Every new page of the ledger is sent to the branches, and each branch files a copy. The question is: when does the clerk say “saved”?

1:1501 · What you need

1:17

To run it yourself: Python with a few packages, and Docker for the database, its two replicas, and a small three-node store.

1:26

The quick tests run in a couple of seconds, without Docker.

1:31

No cloud, and no API keys.

1:3602 · What “saved” waits for

1:37

Postgres lets you choose what the clerk waits for. I timed each choice, with one writer, then with sixteen at once.

1:46

Wait for nothing, not even its own disk: three hundredths of a millisecond. But a crash can lose the last saves.

1:54

Wait for its own disk: about a quarter of a millisecond. That’s the usual default: its own disk, no copy.

2:03

Wait until a branch has received the copy: about the same. Until the branch has saved it to its own disk: about a third of a millisecond.

2:13

And wait until readers at the branch can see it: about half a millisecond.

2:19

Now sixteen writers at once. Waiting for its own disk: about twenty-two thousand saves a second. Waiting until readers at the branch see it: ten and a half thousand. Roughly half. Waiting for nothing at all: almost fifty-nine thousand.

2:36

On one laptop the branch is very close. Between data centers, each wait for a branch would add a trip across the network.

2:4603 · Freeze one branch

2:47

Now the freeze. Postgres lets you name the branch to wait for. Here, replica one, and wait until it has saved the copy to its disk. Four writers save as fast as they can for sixteen seconds, and in the middle I pause replica one for eight seconds.

3:05

Waiting for no branch: a hundred thirty-one thousand saves. The longest gap between saves: thirty-five milliseconds.

3:14

Now the clerk waits for replica one. What happens during the freeze?

3:22

No save finished for eight point one seconds. Nothing failed. The four saves in progress just waited eight point one seconds, then finished together. With a five-second timeout, they would have failed.

3:36

Last, the clerk waits for any one of the two branches. The longest gap: seventy-three milliseconds. Replica two answered while replica one was frozen.

3:47

Wait for one named branch, and when that branch stops, every save that waits for it stops. Wait for any one of two, and it doesn’t.

3:5804 · A store with three copies

3:59

Some databases go further: no head office at all. I wrote a small store for this test, with three copies: three branches, each with a copy of everything.

4:10

Now whoever sends the save is the clerk. Send the page to all three, and say “saved” once W of them confirm. W is a number we pick.

4:21

R is the same for reads: ask all three, and take the newest of the first R answers. A setting here means a pair: R and W.

4:32

To make it realistic, each branch waits a random zero to twenty milliseconds before it starts showing a save to readers. So the copies are briefly out of step.

4:4505 · Save and read, in code

4:46

Here it is. A save goes to all three branches.

4:51

And it counts as done when the first W confirm. The slower ones catch up later.

4:57

A read also asks all three, and waits for the first R answers.

5:03

Then it keeps the newest version among them.

5:0806 · How often a read was old

5:09

Aiming for a hundred saves a second, four readers, twenty seconds per setting. A read is old, also called stale, if it missed a save that was already confirmed.

5:20

R one, W one: twenty-three percent of reads were old.

5:26

R two, W one: about six percent old. R one, W two: about five.

5:34

Now R two and W two. Or W three, or R three. How many old reads?

5:42

None. Zero, in about forty-seven thousand reads.

5:48

In every setting with no old reads, R plus W is more than three: more than N, the number of copies.

5:5707 · Why more than three

5:59

Why? With one and one, the save can be confirmed by branch one, and the read’s first answer can come from branch three. They never meet.

6:09

With two and two, any two that confirm a save and any two you read share at least one branch. So the read always sees the newest save.

6:19

That’s the rule: R plus W more than the number of copies.

6:24

The price is on the saving side. A save waits for the last of the W branches it needs: typically about eight milliseconds with W one, thirteen with W two, nineteen with W three.

6:3908 · One branch switched off

6:41

Last test: I switched branch three off, and ran each setting for eight seconds.

6:47

R one, W one kept working, but fourteen percent of reads were old.

6:53

R two, W two kept saving and reading, with no old reads. Slower, though: five hundred nineteen saves, where R one, W one managed eight hundred, because every save now waits for both remaining branches.

7:09

W three: not one of seven hundred twenty-five saves was confirmed. Yet branches one and two still stored them, so readers saw saves that had officially failed. R three: not one of four thousand two hundred reads was answered.

7:25

Waiting for all three copies buys certainty, until one of them is gone.

7:32Back to Lucía

7:34

So, three lessons.

7:36

Wait for one named branch, and when it stops, every save that waits for it stops. Instead, wait for any one of several.

7:46

R plus W more than the number of copies: every read meets the newest save.

7:52

And choose R and W for the failure you have to survive. With three copies, two and two kept working with one gone.

8:01

Next time: when one database is too big, and the data has to be split.

8:07

For today: wait for enough copies, not for one.Lucía: Wait for enough copies, not one.

8:12Summary

8:13

Don’t depend on one copy, keep R plus W above N, and plan for one copy gone.

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.

Try it · When does the clerk say “saved”?Lab 09 · 02_sync_replication.py · Postgres 18, one laptop

What the head office (the primary) waits for · the setting is synchronous_commit

synchronous_commit = on · synchronous_standby_names = 'replica1'

Head officethe primary · takes every saveBranch officereplica 1 · files a copy of every pagecopy1on its own disk2received3on its disk“saved” here4readers see itthe clerk waits for every filled step · the analogy, drawn from the five settings

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.

1 writer, typical save
0.36 ms
1 writer, p99
1.20 ms
16 writers, saves/s
11,053
vs. own disk only
0.49×

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.

Try it · Freeze one replicaLab 09 · 02_sync_replication.py · 4 writers, 16 s, replica 1 paused 8 s

synchronous_standby_names = 'replica1' · synchronous_commit = on

replica 1 paused, about 4–12 s0 s4 s8 s12 s16 sCommits4 writersReplica 1commits wait for itReplica 2¼ of a CPUno commit finished for 8.1 spaused (docker pause)
Commits in 16 s
33,589
Longest gap
8.10 s
Slowest commit
8,100 ms
Failed
0

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.

Try it · Three branches, no head officeLab 09 · 05_quorum.py · N = 3 copies, 0–20 ms injected delay
W (writes)
R (reads)
Writeconfirmed by 1Readfirst 1 answerBranch 1has the newest writeconfirmedBranch 2not yetBranch 3not yetansweredworst case: the read's first answers come from the other end

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

Stale reads
22.9%
Of reads
14,600
Typical write
7.7 ms
Typical read
2.0 ms

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.

Confirmed the write Answered the read Both: the read sees the write

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

  1. Know what your commit waits for. Write down synchronous_commit and synchronous_standby_names for each database, and what can be lost at that level if the primary dies.
  2. Price the waits on your network. Every wait for a replica adds a round trip; measure it between the machines you actually use.
  3. Never depend on one named synchronous replica. Name a quorum (ANY 1 of at least two), alert when a synchronous replica stops confirming, and set client timeouts so a hang shows up as an error.
  4. Mix levels per transaction if some writes matter more: remote_apply for one the next page must show, off for one you can lose.
  5. 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.
  6. 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.
  7. 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.

Try it · Practice in PythonRuns in your browser · ⌘/Ctrl + Enter to run
quorum_practice.py80 lines

Quiz

Try it · Check your understanding0 of 6 answered
  1. 1.What does synchronous_commit = remote_apply wait for that synchronous_commit = on doesn't?

  2. 2.Postgres names one synchronous replica, and that replica is paused for 8 s. What happens to writes?

  3. 3.With synchronous_standby_names = 'ANY 1 (replica1, replica2)', replica1 is paused for 8 s. What did the lab measure?

  4. 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. 5.In the lab's store, what did raising W from 1 to 3 cost?

  6. 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.

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.