Skip to content
DevOps AI ToolKit
Newsletter
All prompts
AI for Postgres Difficulty: Advanced ClaudeChatGPTCursor

Postgres Read-Replica Load Balancing Design Prompt

Design read/write query routing across streaming replicas that respects replication lag and read-your-writes consistency — so you scale reads without serving stale data to the user who just wrote it.

Target user
Platform engineers scaling read traffic across Postgres streaming replicas
Difficulty
Advanced
Tools
Claude, ChatGPT, Cursor

The prompt

You are a senior PostgreSQL architect who has built read-scaling tiers on streaming
replication. You treat replication lag and consistency as first-class design inputs, not
afterthoughts, and you know that "just send reads to the replica" breaks read-your-writes.

I will describe:
- Primary + replica topology and how replicas are provisioned (sync/async): [TOPOLOGY]
- The routing layer in use or under consideration (app-side, PgBouncer, pgpool-II,
  a proxy, HAProxy): [ROUTER]
- Read/write ratio and the queries that dominate read load: [WORKLOAD]
- Consistency requirements — which reads must see the user's own latest write: [CONSISTENCY]
- Tolerated staleness for the rest, and current typical/worst lag: [LAG BUDGET]
- Postgres version: [VERSION]

Work through this in order:

1. **Classify reads by consistency need**: reads that must be strongly consistent
   (read-your-writes, financial totals) vs reads that tolerate staleness (search,
   dashboards, feeds). Only the second class is safe to route to async replicas.

2. **Choose a routing strategy** and be explicit about its trade-offs: app-level routing
   (precise but couples app to topology), a proxy like pgpool-II (transparent but adds a
   hop and its own failure modes), or sticky "route to primary for T seconds after a
   write" for read-your-writes. Explain how to detect a replica during connection setup.

3. **Guard against stale reads**: how to measure lag
   (`SELECT (now() - pg_last_xact_replay_timestamp())` on the replica, and bytes via
   pg_stat_replication on the primary), how to eject a replica from the pool when lag
   exceeds the budget, and the LSN/`pg_wait_for_lsn`-style pattern for causal reads.

4. **Handle the pitfalls**: recovery conflicts and "canceling statement due to conflict
   with recovery" on replicas, connection failover when the primary is promoted, and the
   fact that a replica set to hot_standby_feedback trades bloat on the primary for fewer
   query cancellations.

5. **Give an operational plan**: health checks, how the pool reacts to a promoted primary,
   and metrics/alerts (per-replica lag, cancellation rate, pool balance).

Output: (a) a read-classification table [read type | consistency | route]; (b) the chosen
routing design with trade-offs; (c) the lag-measurement and ejection commands; (d) the
failover behavior; (e) the alerts to add.

Guardrails: never route read-your-writes or transactional-total reads to an async replica
without a lag/LSN guard. Confirm the routing layer fails reads over to the primary (or
errors loudly) when all replicas exceed the lag budget, rather than silently serving stale
data. Test primary-promotion behavior in staging.

Run this prompt with AI

Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.

Why this prompt works

Read scaling with replicas is easy to bolt on and easy to get subtly wrong. The moment a user’s read is served by an async replica that hasn’t yet replayed their write, the app shows stale data and the bug is intermittent and maddening to reproduce. This prompt puts consistency classification first, so reads are routed by what they can tolerate rather than uniformly shipped to whatever replica is least busy.

It also refuses to treat lag as invisible. By requiring concrete lag measurement (pg_last_xact_replay_timestamp, pg_stat_replication), an ejection policy when the budget is exceeded, and an explicit failover story for primary promotion, the design stays safe under the exact conditions — high lag, a failover — where naive routing corrupts the user experience. Folding in replica-specific hazards like recovery conflicts and hot_standby_feedback trade-offs makes it an operational plan, not just a diagram.

Related prompts

More Postgres prompts & error guides

Browse every Postgres prompt and troubleshooting guide in one place.

Free download · 368-page PDF

Reading prompts? Get all 500 in one free PDF

500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.

  • 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
  • Instant PDF download — yours free, forever
  • Plus one practical AI-workflow email a week (no spam)

Single opt-in · unsubscribe anytime · no spam.