Why Read Replicas Still Stall at p99
Every team eventually tries to buy their way out of database tail latency by provisioning another read replica. For a week, average CPU drops. Then Monday morning hits and p99 still spikes past 800 ms.
The mistake is treating read replicas as stateless caches. A streaming Postgres replica shares the primary's write-ahead log timeline, lock conflicts, and buffer-pool churn. When a long-running analytical query holds back hot-standby feedback or triggers a conflict on a vacuumed page, the replica pauses WAL apply — or cancels your read outright.
1. The pooler sits in front of the wrong queue
Put PgBouncer in transaction-pooling mode in front of three replicas and
it looks balanced until one host hits an I/O checkpoint. Because health
checks only verify that SELECT 1 responds within two
seconds, the pooler keeps routing read transactions to the stalled node.
Your tail isn't bounded by the median replica; it is bounded by the
slowest node still passing health checks.
-- Spot queries holding snapshots that block WAL replay on a standby
SELECT pid, now() - backend_xmin_age AS age, state, left(query, 80)
FROM pg_stat_activity
WHERE backend_xmin IS NOT NULL
ORDER BY age DESC
LIMIT 10;
2. Cache invalidation vs. snapshot freshness
Most endpoints that call a read replica don't actually need read-your-writes consistency for every byte. They need bounded staleness on the hot path (article bodies, config flags, public catalog rows) and strict primary reads only for the mutation confirmation screen. Moving read-mostly lookups to an edge KV or short-TTL cache cuts replica IOPS by an order of magnitude before you ever touch instance sizing.
3. Isolate the workload before scaling horizontally
Before adding a third replica, separate interactive request pools from
batch exports and background workers. Giving reporting jobs their own
dedicated replica with max_standby_streaming_delay = -1
keeps analytical snapshots from poisoning interactive p99.
Predictable latency comes from isolating failure domains, not multiplying identical nodes behind a round-robin DNS record.