Skip to content

Splitting reads and writes in a managed Postgres setup

Read replicas only help when the application sends reads to them. Here is how we route read and write traffic on managed Postgres without breaking transactions.

You provisioned a primary and a read replica on managed Postgres. The replica is healthy, replication lag is sub-second, and the primary’s CPU is still pinned at 85% during business hours. The replica is doing nothing because the application is sending every query to the primary. Splitting reads and writes is a code change as much as it is an infrastructure one, and the wrong split breaks transactions in ways that take days to diagnose.

What’s happening

A managed Postgres cluster exposes two endpoints — a writer endpoint that always resolves to the current primary, and a reader endpoint that load-balances across replicas. The application is almost certainly using one connection string that points at the writer. Every SELECT, every dashboard query, every report is going to the primary. The replica is sitting idle, billed but not earning. The fix is to give the application two connections, route by intent, and accept that some reads must stay on the primary.

How to verify

On the primary, see who is doing what right now:

SELECT state, query_start, left(query, 80) AS q
FROM pg_stat_activity
WHERE datname = current_database()
ORDER BY query_start DESC
LIMIT 20;

If the result is dominated by SELECT statements you would happily serve stale by 200ms, the split is worth doing. On the replica, confirm it is actually accepting traffic:

SELECT count(*) FROM pg_stat_activity WHERE state = 'active';

A near-zero number on a “warm” replica is the smoking gun.

The fix

  1. In the application, add a second database connection pointed at the reader endpoint. Most ORMs (ActiveRecord, Eloquent, Django, SQLAlchemy) support a primary/replica configuration natively — turn it on.
  2. Route by intent, not by SQL parsing. Annotate read-only request paths (list endpoints, search, reports, public read API) to use the reader. Anything inside a transaction, anything that issues a write, and anything that reads back its own write must stay on the primary. Read-your-own-write across replicas is a footgun even with sub-second lag.
  3. Move long, expensive analytical reads to the replica explicitly. A 40-second SELECT against the primary is a backup window risk; the same query against the replica is just a slow query.
  4. Watch pg_stat_replication.replay_lag and alert if it exceeds the threshold your application tolerates. If lag spikes, the application should fall back to the primary or surface a “data slightly delayed” notice — never silently serve stale data on a path users assume is fresh.

Once split correctly, the primary’s CPU drops, the replica earns its keep, and the next vertical resize gets pushed out by quarters.

If your application is on managed Postgres and the replica is idle, our Cloud PaaS team can do the split as part of the operating model — book a session.