Lesson 5 of 9 · 25m
Streaming Replication Operations
How physical streaming replication works, what replication slots protect, how to measure lag honestly, and what synchronous replication does and does not guarantee.
Replication is easy to start and hard to operate. I have seen teams celebrate a green replica status while the replica was twelve hours behind because nobody distinguished sent WAL from replayed WAL. Replication is not a backup, and it is not automatically high availability. It is a continuous copy whose usefulness depends on lag, durability, and promotion discipline.
Physical replication ships WAL, not queries
A standby connects to the primary, receives write-ahead log records, and replays them into its own copy of the data files. Physical replication is exact and fast. Build a standby with pg_basebackup, configure primary_conninfo, create standby.signal, and start it.
Primary-side settings and access:
wal_level = replica
max_wal_senders = 10
hot_standby = on
# pg_hba.conf: host replication replicator 10.20.0.0/16 scram-sha-256CREATE ROLE replicator WITH REPLICATION LOGIN PASSWORD 'use-a-secret-manager';Slots retain WAL whether you are careful or not
A replication slot prevents the primary from removing WAL that a consumer has not received. That protects a temporarily disconnected replica — and can fill the primary's disk when a decommissioned consumer never returns. Ask how much WAL each slot forces you to retain, not merely whether it is active.
SELECT slot_name, active, restart_lsn, pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)) AS retained_wal
FROM pg_replication_slots;Measure lag in bytes and time
SELECT client_addr, state, sync_state, write_lag, flush_lag, replay_lag,
pg_wal_lsn_diff(pg_current_wal_lsn(), sent_lsn) AS send_lag_bytes,
pg_wal_lsn_diff(sent_lsn, replay_lsn) AS replay_gap_bytes
FROM pg_stat_replication;Sent lag is network/transfer distance. Flush lag is standby durability. Replay lag is how far the standby is from making changes visible. A replica can receive WAL promptly and still serve stale reads because replay is I/O-bound or blocked by a conflict.
Synchronous replication moves the pain
synchronous_commit = remote_apply
synchronous_standby_names = 'FIRST 1 (standby1, standby2)'Synchronous commit reduces data loss during failover but couples primary write latency to standby health. remote_apply waits for replay, not just receipt. Enable it because your RPO justifies the coupling, and test what happens when the standby dies.
Hot standby conflicts are normal
Standby queries can conflict with replay. max_standby_streaming_delay controls how long recovery waits before canceling them; hot_standby_feedback reduces cancellations by telling the primary about standby snapshots, but can increase primary-side bloat.
Field notes
- Replication copies corruption and deletions too; it is no substitute for backups or PITR.
- Monitor inactive replication slots aggressively.
- Measure replay visibility lag when the business reads from replicas.
pg_stat_replicationis on the primary; usepg_stat_wal_receiverand replay functions on the standby.- Failover tooling can promote a replica, but it cannot invent missing WAL.
Was this lesson helpful?