Read Replicas
Secondary database instances that asynchronously replicate data from the primary, offloading read queries to scale read throughput.
Description
Read replicas are copies of the primary database that receive a continuous stream of changes via replication. Applications route write queries to the primary and read queries to one or more replicas, effectively multiplying read throughput. This is the most common scaling strategy for read-heavy workloads (which describes most web applications) because it requires minimal application changes -- just routing reads to a different connection.
Asynchronous replication means replicas may lag behind the primary by milliseconds to seconds (replication lag). This creates a consistency challenge: after writing data to the primary, an immediately subsequent read from a replica may return stale data. Common solutions include: read-your-writes consistency (route reads to the primary for a short window after a write), monotonic reads (pin a user session to a specific replica), and lag-aware routing (check replication lag and fall back to primary if lag exceeds a threshold).
Managed database services (AWS RDS, Cloud SQL, Azure Database) make provisioning replicas trivial -- often a single click or API call. For self-managed PostgreSQL, configure streaming replication with hot_standby = on and monitor replication lag via pg_stat_replication. Use a proxy like PgPool-II, ProxySQL, or application-level routing to distribute reads. Scale replicas horizontally as read traffic grows, and promote a replica to primary during failover.
Prompt Snippet
Configure PostgreSQL streaming replication with synchronous_commit = remote_apply on the primary for critical read-your-writes paths, and asynchronous replication for general read scaling. Monitor replication lag via pg_stat_replication.replay_lag and alert if lag exceeds 5 seconds. Implement connection routing in the application layer: writes always go to the primary, reads go to replicas unless the request context has a recent write timestamp within the last 2 seconds (read-your-writes guarantee). Use PgPool-II or application-level routing with health checks on each replica.
Tags
Related Terms
Database Replication
Copying data from one database server to another in real-time or near-real-time to provide redundancy, failover capability, and read scaling.
Database Sharding
Distributing data across multiple database instances by splitting rows based on a shard key, enabling horizontal scaling beyond the capacity of a single server.
Database Connection Pooling
Maintaining a reusable pool of database connections to avoid the overhead of establishing new connections for every query.