Learn Labs
Replication & Scaling

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

Lag 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 behind

Even 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_commitCOMMIT waits until the sync replica has
remote_writereceived the WAL and written it (not yet flushed to disk)
onflushed it to disk: survives the replica crashing too
remote_applyapplied 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:

on the primary: delay packets to the replicas onlytc qdisc add dev eth0 root handle 1: priotc qdisc add dev eth0 parent 1:3 handle 30: netem delay 5mstc filter add dev eth0 protocol ip parent 1:0 prio 3 u32 match ip dst <replica-ip>/32 flowid 1:3and the same on each replica, for packets to the primary
replicas on the same hostasync0.93 mssync, remote_write1.03 mssync, on (replica flushed)1.31 mssync, remote_apply1.35 msreplicas 5 ms away (tc netem, 12.7 ms TCP round trip)async0.98 mssync, remote_write15.5 mssync, on (replica flushed)18.7 mssync, remote_apply18.5 ms
  • 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 with on, 18.5 ms with remote_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.

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/250

With 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).

On this page