ClickhouseColton ShepardExplainer
More load is faster: Tuning Postgres read replicas
Summary
A ClickHouse Managed Postgres user reported unexpectedly high read replica delay, prompting an investigation into Postgres replication lag tuning. The author found that with default settings, replica lag follows a U-shaped curve: low and high throughput cause large spikes, while moderate load yields only a few seconds of delay. Tuning archive_timeout to 5s and wal_retrieve_retry_interval to 500ms provides a safe starting point for reducing lag. More aggressive tuning, such as forcing WAL switches with pg_switch_wal() or using a WAL-G daemon client, can further reduce lag but introduces tradeoffs like increased network costs and CPU overhead. Streaming replication still achieves lower lag than archive-based replication, but the author notes that a few seconds of lag is sufficient for most use cases.
Context
A user reported unexpectedly high read replica delay on ClickHouse Managed Postgres, prompting investigation into Postgres configuration options for replication lag. The author found that conventional wisdom about tuning was outdated and that low write traffic causes long delays due to the 60-second WAL segment switch timeout.
Approach / What changed
The author tested Postgres read replica lag by running pgbench on ClickHouse Managed Postgres instances (i8g.large, 16GB RAM, 2 vCPUs, PG 18.6) with a heartbeat tool writing every 200ms. They tuned archive_timeout and wal_retrieve_retry_interval, and experimented with pg_switch_wal() and a WAL-G daemon client to reduce fetch overhead.
Takeaways
Replica lag with default Postgres tuning is U-shaped: very low and very high write throughput both cause large spikes, while moderate load (e.g., 12MB/s WAL) yields ~3 seconds of delay.
Setting archive_timeout=5s and wal_retrieve_retry_interval=500ms is a safe starting point for reducing replica lag without major tradeoffs.
Forcing WAL switches with pg_switch_wal() at sub-second intervals can reduce lag but increases network overhead and primary CPU load by ~15%.
FAQ
How does archive_timeout affect replica lag?
archive_timeout controls how long Postgres waits before shipping a WAL segment even if it isn't full. Lowering it (e.g., to 5s) forces more frequent shipping, reducing replica lag at low write throughput. However, it increases network overhead, especially with synchronous HA standbys.
Can WAL segment size be tuned after database creation?
Yes, but it requires a maintenance window using initdb --wal-segsize or pg_resetwal --wal-segsize. It is not practical for tuning an existing production environment.
What tradeoff does the WAL-G daemon client introduce?
Using a WAL-G daemon client shaved about 55ms on a hit and 85ms on a miss per fetch, significantly reducing overhead for very short switch intervals (e.g., 250ms). However, it adds external dependencies to the replay path, which is risky for production.
Source and freshness
- Source
- clickhouse.com
- Published
- Added to Yomu
Summaries are generated from the original by a language model. Check the original before relying on it.
Keep reading
Related reading
The Good Stuff
Paid picks we use ourselves. Links carry a referral code — a subscription through one earns us a commission at no extra cost to you.
The best engineering reads, once a week
One useful email. Unsubscribe anytime.