Don't add a read replica until you've read this
incident.io moved nearly all Postgres read traffic to a read replica and had to preserve read-after-write consistency without hand-routing each query. Within a request, a Go context flag is tainted on any write or transaction so later reads go to the primary, a simple SELECT-prefix check classifies queries, and a circuit breaker falls back to the primary if the replica is unreachable. Across async boundaries, 0.1–0.5% of queue messages hit NotFound errors because workers ran faster than replication. The fix stamps each message and each mutating API actor (user or API key) with the primary's WAL LSN and checks pg_last_wal_replay_lsn() before reading from the replica. Lagging messages are nacked with exponential backoff, which doubles as backpressure, and replica lag is best monitored in bytes rather than time.
Related posts
- Read your writes: WAIT FOR in PostgreSQL 19
ClickHouse mediumAdept
PostgreSQL 19 adds a top-level `WAIT FOR LSN` command that blocks a session until a standby has written, flushed or replayed WAL up to a given LSN, giving read-your-writes consistency on asynchronous replicas without the per-commit cost of `synchronous_commit = remote_apply`. The pattern is to capture `pg_current_wal_insert_lsn()` after a write on the primary, then run `WAIT FOR` on the standby before reading. Options include MODE (standby_replay, standby_flush, standby_write, primary_flush), TIMEOUT and NO_THROW. It must run as a snapshot-free utility command outside functions and above READ COMMITTED, because a held snapshot can stall WAL replay and self-deadlock. The feature took about 10 years and three reverts to land. Timeline changes after promotion are not tracked, and connection poolers and proxies are natural places to inject it transparently.
- Introducing Read Replicas
Supabase mediumAdeptSupabase now supports Postgres read replicas in 12 regions, up to two per project, for offloading reads and analytics, serving data closer to users, and redundancy. Replication combines native streaming replication for low lag with file-based WAL shipping as a fallback, reusing their PITR pipeline (WAL-G archiving to S3) so replicas that fall behind past wal_keep_size can still catch up. Each replica gets its own Supavisor connection pool and PostgREST instance, with a round-robin load-balancing endpoint. Pricing covers replica compute and disk plus $0.09/GB WAL transfer, and automatic failover, geo-routing and Supavisor read/write splitting are planned.
- Scaling Nextdoor’s Datastores: Part 2
Nextdoor Engineering mediumAdeptLetting product engineers manually choose replica vs. primary routing in Django broke down as abstractions hid read-after-write dependencies, and the common workaround of wrapping logic in transactions sent everything to the primary, slowly eroding the capacity gained from read replicas. Nextdoor replaced manual routing with ORM-level tracking of which tables a web request has written to, automatically sending subsequent reads of those tables to the primary and everything else to replicas. Because average replication lag (~20ms) was far shorter than request duration, they refined this with a timer that makes tables replica-eligible again after p99.9 replication lag elapses, moving most read queries off the primary.
- More load is faster: Tuning Postgres read replicas
ClickHouse hardMaster
Postgres read replicas fed from archived WAL (rather than streaming replication) show a U-shaped lag curve: idle systems wait up to archive_timeout (60s) for a 16MB segment to fill, while very high write rates make archive upload fall behind. Moderate load gives about 3s lag by default. Setting archive_timeout=5s and wal_retrieve_retry_interval=500ms is a safe starting point. More aggressive options give diminishing returns because each poll costs about 0.8s of WAL-G calls, re-fetching and decompressing the previous segment, and adds cross-AZ network cost with synchronous HA. These options include sub-second pg_switch_wal() loops and a persistent WAL-G daemon client. Streaming replication still wins on lag but brings VACUUM, slot and failover hazards.
- Patching the PostgreSQL JDBC Driver
Zalando Engineering mediumAdeptPostgres logical replication slots for clients watching low-traffic tables never advance, so the server retains all WAL generated by other tables until disk fills and the node goes read-only. Zalando fixed this upstream in PgJDBC: the driver now tracks the last received and last confirmed replication LSNs, and when they match and a KeepAlive message reports a higher server LSN, it confirms that LSN, safely advancing the slot without skipping relevant events. The fix, merged for PgJDBC 42.7.0, removes the need for the common dummy-write workaround and was validated by comparing WAL size graphs before and after a patched Debezium image rollout across hundreds of event streams.