Sub topic 10
Scaling reads with read replicas
This chapter assumes you have provisioned an RDS instance such as demo-pg, and that you know how Multi-AZ standbys and automated backups work. Here you will add read replicas to spread read traffic and learn what they cannot do.
What a read replica is
A read replica is a separate DB instance that receives a copy of the source instance's changes. It has its own endpoint, its own instance class, and its own storage. Your application sends writes to the source and can send read-only queries to the replica.
Replication is asynchronous. The source commits a write without waiting for the replica, so the replica can fall behind. A query on the replica may return data that is a few seconds old.
Creating a replica
Automated backups must be enabled on the source (--backup-retention-period greater than 0). Then create the replica:
aws rds create-db-instance-read-replica \
--db-instance-identifier demo-pg-replica \
--source-db-instance-identifier demo-pgBy default the replica uses the source's instance class and storage settings. You can choose a different class with --db-instance-class, for example a smaller one for light reporting queries. Wait for the replica to reach available, then read its endpoint with describe-db-instances, the same way you did for the source.
If the source is encrypted, the replica is encrypted too. A replica in another Region needs a KMS key from the destination Region.
Using replicas from your application
RDS does not route queries for you. Your application or its database driver must hold two connection settings, one for the source and one for the replica, and send read-only work to the replica.
- Send reports, dashboards, and search-style queries to the replica.
- Keep writes, and any read that must see a just-committed write, on the source.
- The replica rejects writes. This is the default for PostgreSQL and MySQL replicas.
You can create several replicas, and RDS allows up to 15 per source for PostgreSQL, MySQL, and MariaDB. Each replica is billed as its own instance, plus any storage and cross-Region data transfer.
Watching replica lag
The CloudWatch metric ReplicaLag reports how many seconds the replica is behind. Create an alarm on it. Lag grows when the source has a heavy write load, when the replica is too small to apply changes quickly, or when a long query on the replica conflicts with replication.
Promoting a replica
You can promote a replica into a standalone instance. This breaks replication permanently. Use it for a Regional disaster recovery plan or for a planned migration:
aws rds promote-read-replica \
--db-instance-identifier demo-pg-replicaAfter promotion the former replica accepts writes, but data that had not replicated yet is lost. Your application must be repointed to the new endpoint.
Replicas compared with Multi-AZ
| Feature | Multi-AZ | Read replica |
|---|---|---|
| Main purpose | Availability | Read scaling |
| Replication | Synchronous (standby) | Asynchronous |
| Serves reads | No, for a standard Multi-AZ instance | Yes |
| Failover | Automatic | Manual promotion |
| Cross-Region | No | Yes |
Exercise
Your reporting dashboard runs heavy queries that slow down the production source instance. A teammate suggests enabling Multi-AZ to fix this. Is that the right choice, and what would you do instead?
Show answer
No. A standard Multi-AZ standby does not serve queries, so it will not reduce load on the source. Create a read replica, point the dashboard's connection at the replica endpoint, and alarm on the ReplicaLag metric so you know when the dashboard data is stale.