Read replica
a database copy that can serve read queries
What it is
A read replica is a copy of a database that can serve read queries while another instance receives writes. The replication mechanism delivers changes and the replica applies them. A PostgreSQL hot standby, for example, can accept read-only connections while it replays write-ahead log records. The copy has its own resources and connections. A request reaching it observes the state that has been applied under the database’s consistency rules.
Consider an online shop that writes orders to its primary instance and sends historical reports to a replica. A report can use memory and CPU on the replica while checkout uses the primary. A customer who immediately opens a freshly created order may need to read from the primary, because the record can take time to appear on the replica. A route that tolerates a short delay can normally use the replica if the measured lag stays within its budget.
The topology can serve more than one purpose. A readable replica can provide query capacity, and in certain configurations it can also be promoted after a failure. Promotion changes which instance can accept writes and requires a route switch or client reconnect. It calls for testing recovery objectives, fencing and the effect of incomplete replication. A service needing read capacity can choose a different topology from one needing automatic failover. Decide which promise the replica is expected to support and whether the service can tolerate the resulting trade-offs.
Why it matters
Reporting, exports and searches often compete with writes for database resources. A separate instance can absorb work that already reads a coherent snapshot and can accept a predictable freshness delay. Moving a query can free primary capacity, but the report itself can still need indexes, bounded work and enough memory. Compare the cost of adjusting the query with the cost of running another instance and operating a replication topology.
Consumers need a freshness policy before traffic moves. A dashboard that shows the previous day’s order totals can tolerate some delay. An order receipt and a support screen opened immediately after an update may need a read that reflects the preceding write. Route based on the business requirement. Some services pin a session to the primary for a short period. Others use a replication position and wait for a suitably advanced replica when their storage system exposes a usable mechanism. Include a time budget and a clear response when the condition is not met.
Replication also creates operational signals. Track positions or timestamps at the producer, receiver and replay process as appropriate for the engine. A connection can be healthy while an intensive workload prevents the replica from applying changes promptly. Check disk usage, replay conflicts and query cancellations as well as a single lag number. An idle database can make timestamp-only measures misleading, so consider a verified heartbeat when a service needs continuous evidence of freshness.
What it looks like in practice
In a staging environment, place an example order through the primary and request the order from the replica immediately. Record the first successful request on each route. Repeat while the replica is busy with a large report and while it is temporarily disconnected. Test the important user outcome. A confirmation page might always request the primary, while a chart might show its last refreshed time and retry later. Check what each path displays when the preferred database is unavailable.
Plan admission and recovery together. Limit the number of long reports that can run at once and test what happens when a query competes with replay. PostgreSQL can cancel conflicting standby queries or defer replay within a configured budget. Run representative report sizes, measure their elapsed time and see how the strategy affects freshness under sustained writes. A fallback that sends every delayed read to the primary can bring back the overload that motivated the replica, so include the primary’s available capacity in the test.
Our case study on operating PostgreSQL and SQL Server systems describes database performance monitoring and documented recovery procedures. Those are practices a team can apply when it adds a read replica. The shop example describes tests for read routing and replication lag. A separate promotion exercise is relevant when the replica is also intended for recovery.
Where it goes wrong
A generic read pool can mix requests with very different consistency needs. Map the routes before enabling automatic read balancing. Include requests issued immediately after writes, administrative views that expect new permissions and jobs that depend on a certain data revision. Measure from the application boundary as well as the database metrics. A small average lag can conceal cases that affect a user at the worst moment.
A replica can fall too far behind to catch up from retained records. Follow how the engine manages log history, slots and resets, and define the required disk capacity. A slot that retains logs during an absence can put the primary under pressure. In some systems an interrupted replica must restart from a new copy if the needed history has been discarded. Drill the rebuild, including when to take the replica out of load balancing and how to confirm it is ready again.
A replica follows legitimate writes, including mistaken changes. Keep separate versioned backups and verify restores independently. When the design includes failover, measure which committed changes survive, how the old primary stops accepting writes and where clients reconnect. A read replica can supply part of that design. Its successful response to a read query leaves these failure-handling questions open.
Further reading
- PostgreSQL hot standby describes read-only connections, replay conflicts and cancellations.
- PostgreSQL standby servers covers streaming, recovery and replication options.
- Amazon RDS PostgreSQL read replicas documents how read copies work in this managed service.