Scaling Reads with DigitalOcean Managed PostgreSQL Read Replicas and Automatic Failover
Learn how to add a read replica to a DigitalOcean Managed PostgreSQL cluster, monitor replication lag, and rely on automatic failover for read scaling and high availability.
05 Jul 2025, 03:02 UTC

Problem: Read‑heavy workloads overload a single PostgreSQL node
When a DigitalOcean Managed PostgreSQL cluster serves mostly read traffic, the primary node can become a bottleneck. Adding more CPU or RAM helps only up to a point, and scaling writes remains unchanged.
Thesis: Using built‑in read replicas and automatic failover lets you offload reads, keep the same connection string, and regain availability after a primary failure without manual replication setup.
How read replicas work in DigitalOcean Managed PostgreSQL
- You can create up to five read replicas per primary via the control panel,
doctl, or the API. - Each replica inherits the primary’s configuration (size, storage, version) and receives asynchronous streaming replication.
- Replicas expose their own connection endpoints, so you can direct read‑only queries to them while writes stay on the primary.
- If the primary becomes unhealthy, DigitalOcean automatically promotes a replica to primary, usually within 30 seconds, and the managed database URL remains valid for clients after a brief reconnection pause.
Worked example: adding a replica and checking lag
Assume you have a managed cluster named my-app-db in the nyc1 region.
- Create a replica (run where you have
doctlinstalled with a token that can manage databases):doctl databases create-replica my-app-db --name my-app-db-replica --region nyc1 - Wait for the replica to appear in the control panel or via
doctl databases list. The replica will show a separate endpoint, e.g.,my-app-db-replica.do-user-12345.db.ondigitalocean.com. - Verify replication lag from a psql session connected to the primary:
The# Connect to primary psql "postgres://doadmin:[contact removed]:25060/defaultdb?sslmode=require" # Check lag in bytes SELECT pid, usename, client_addr, state, pg_wal_lsn_diff(pg_current_wal_lsn(), replay_lsn) AS lag_bytes FROM pg_stat_replication;lag_bytescolumn shows how many WAL bytes the replica is behind. A value of zero (or a few kilobytes) indicates the replica is keeping up. - Optional: read‑only query on the replica
This query runs on the replica, leaving the primary free for writes.psql "postgres://doadmin:[contact removed]:25060/defaultdb?sslmode=require" SELECT count(*) FROM large_table;
Automatic failover in action
To see failover work without affecting production, you can use the “Force failover” action in the control panel or via the API:
doctl databases failover my-app-db
After a short pause (typically <20 seconds), the former replica becomes the new primary. The connection string you used for writes remains valid; clients that reconnect after the pause will be talking to the new primary. You can confirm the role swap in the activity log or by querying pg_is_in_recovery() on each node.
Trade‑offs and limitations
- Eventual consistency: Replicas are asynchronous. Applications that require the latest data for a read must either route those reads to the primary or tolerate a small staleness window.
- Cost: Each read replica incurs the same hourly price as a primary node of the same size. Adding replicas scales your bill linearly.
- Write‑path overhead**: The primary must stream WAL to every replica. With five replicas, network and disk I/O on the primary increase, which can affect write throughput under heavy load.
Practical way to check the result
After adding a replica, monitor two metrics in the DigitalOcean monitoring dashboard:
- Replication lag (available under the database’s metrics). Set an alert if lag exceeds, say, 10 MB.
- Primary CPU utilization. If it rises significantly after adding replicas, consider scaling the primary or reducing replica count.
You can also run the lag query shown above periodically from a cron job or monitoring tool to get a granular view.
Actionable closing
If your workload is read‑heavy and you can tolerate mildly stale reads, start by adding a single read replica and measure lag and primary CPU. Use the built‑in alerts to keep lag within an acceptable range. Only add more replicas if lag consistently stays low and you need further read throughput. Remember to review the cost impact and test failover in a staging environment before relying on it for production availability.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.