Triage and Mitigation of Critical PostgreSQL Replication Lag During Peak Traffic
Scenario Prompt:
“During a high-traffic sales event, read replicas experience a 35-minute replication lag. Customers complain that newly placed orders do not appear on their profile dashboards. The primary database disk is at 91% capacity due to accumulated WAL segments. How do you triage and resolve this?”
Substantive Technical Concepts to Hit:
Root Cause Identification
Check pg_stat_replication on the primary and pg_stat_wal_receiver on replicas. Determine if the lag is network bandwidth bottleneck, long-running read queries holding locks on the replica (max_standby_streaming_delay), or I/O saturation.
Read Routing Workaround
Temporarily route critical read queries (like order confirmation lookups) to the primary database while leaving bulk analytics on replicas to alleviate user-facing inconsistencies.
WAL Segment Disk Pressure
If the primary disk fills up completely, PostgreSQL will crash. Ensure WAL archive storage (e.g. S3 WAL-G/pgBackRest) is healthy so old WAL can be safely pruned from primary storage.
Replica Concurrency Tuning
Increase max_parallel_maintenance_workers and inspect vacuum operations to prevent table bloat from slowing down WAL replaying.
- Is the replication lag caused by long-running analytical queries blocking the replica replay process?
- What is the replication slot status on the primary - are inactive slots holding back WAL segment cleanup?
- Can we temporarily route critical read-your-own-writes traffic to the primary node?