Replication Lab: Lag, Stale Reads and Sync Commits
What a replica sees right after a write, and what synchronous replication costs when replicas are 5 ms away
labs/replication.py measures the three things that matter about
streaming replication: how far behind replicas fall, whether a read on a
replica sees a write you just made, and what it costs to make commits wait
for a replica.
docker compose -f compose_prod.yaml run --rm labs python replication.pyLag during a bulk write
2. Lag during a bulk write (1,000,000 rows in one INSERT)
insert took 1.3s; replicas were at most 46.1 MB and 238 ms behindEven on one host, a single large statement put the replicas 46 MB and a quarter of a second behind. Across runs this ranged from 32 to 46 MB and 198 to 315 ms. Replication is fast, but never instant.
Reading your own write from a replica
The lab inserts a row on the primary and immediately reads it back on a replica, 500 times, over connections already open to each replica:
async: 499 of 500 reads did not see the row just written (stale/reads: pg-1 249/250, pg-3 250/250)499 of 500 missed. With async replication the primary returns from
COMMIT as soon as its own WAL is on disk; the replica receives and
applies it a fraction of a millisecond later, and a read that follows
immediately is faster than that. An earlier version of the lab opened a
new TLS connection for each read, and then only 0 to 14 of 500 missed:
the few milliseconds of handshake were enough for the replica to catch up.
Both numbers are real; what decides staleness is the time between the
write and the read, which is why "read your own writes from a replica"
bugs come and go with load.
Making commits wait for a replica
Patroni's synchronous_mode makes one replica synchronous: the primary's
COMMIT waits for it. synchronous_commit chooses how far:
synchronous_commit | COMMIT waits until the sync replica has |
|---|---|
remote_write | received the WAL and written it (not yet flushed to disk) |
on | flushed it to disk: survives the replica crashing too |
remote_apply | applied it: a read on that replica sees the change |
To make the numbers realistic, the lab uses tc netem to put the replicas
"in another zone", 5 ms each way, delaying only traffic between the
primary and the replicas:
- Async doesn't care where the replicas are: about 1 ms either way.
- On one host, sync costs almost nothing: 0.9 ms to 1.3 ms.
- 5 ms away, every sync commit pays a round trip: 15.5 ms with
remote_write, 18.7 ms withon, 18.5 ms withremote_apply. A client doing one commit at a time goes from about 1,000 commits a second to about 55.
Explaining 18.7 ms
5 ms each way should cost about 10 ms. A probe from the primary to a
replica measured a 12 ms query round trip, and the kernel's own estimate
for the replication connection (ss -ti) was rtt:12.68: netem plus the
VM's virtual network add about 2.7 ms to the 10. On top of that,
remote_write adds the replica's write (15.5 ms total), and on adds its
fsync (another 3 ms). Measured against the delay setting, sync commits
grew by about 2 ms for every 1 ms of one-way delay: 11.9 ms at 2 ms, 18.2
at 5, 27.9 at 10, 49.1 at 20.
The first run showed async commits with a 17 ms p99 once the delay
was on, which made no sense: async commits don't wait for replicas. The
cause was the lab, not Postgres. After switching from sync back to
async, the lab waited for Patroni's member list to stop showing a
sync_standby, but Patroni updates that before Postgres reloads
synchronous_standby_names. The first few "async" commits still waited.
The lab now waits for SHOW synchronous_standby_names itself, and the
async p99 dropped to 1.2 to 3.6 ms across runs.
Sync replication and stale reads
remote_apply should make read-your-writes safe, and it does, but only
on the replica that's synchronous:
read-your-writes with remote_apply, stale/reads per replica:
one sync standby (pg-1): pg-1 0/250, pg-3 100/250
both synchronous: pg-1 0/250, pg-3 0/250With one synchronous standby, reads on pg-1 were never stale, but pg-3 is
still async and missed 100 of 250. A load balancer spreading reads over
both replicas would see stale data on roughly one read in five. Making both
synchronous (synchronous_node_count: 2) fixed it, at the cost of every
commit now waiting for the slower of two replicas, and of writes stopping
if either one is down (unless Patroni drops it from the sync set, which
synchronous_mode does).
Pick per transaction, not per cluster. synchronous_commit can be set
with SET LOCAL inside a transaction, so payments can wait for a replica
while page-view counters don't. And route reads that must see the user's
own writes to the primary; replicas are for reads that tolerate a little
lag.