Lesson 3 of 7 Amazon RDS and Aurora

Scaling Reads with RDS Read Replicas

This lesson explains read replicas, asynchronous copies of an RDS database that serve read traffic, offload reporting and can be promoted for disaster recovery.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe how read replicas work.
  • Explain how replica lag affects reads.
  • Describe how cross-Region replicas serve reads and disaster recovery.

How read replicas work

Read replicas offload read traffic from the primary database. MySQL, MariaDB and PostgreSQL support up to 15 replicas, and Oracle and SQL Server fewer, in the same Region or in other Regions. Replication is asynchronous, so replicas can lag behind the primary.

Each replica has its own endpoint, and the application must send read queries there explicitly. A replica can itself be made Multi-AZ for availability, and it can be promoted to a standalone primary, for example to create a separate database for reporting or for disaster recovery.

The following diagram shows a primary with replicas in two Regions.

RDS read replicas. The primary replicates asynchronously to replicas in the same or other Regions, so replicas can lag behind. Each replica has its own endpoint, and the application must send read queries there explicitly. A replica can be promoted to a standalone primary, for example for disaster recovery.
The Regions are illustrative. Because replication is asynchronous, a read just after a write may not see it on a replica.
True or false

Read replicas

Exam skills 3.3.1

Check how RDS read replicas work.

Log in to try this activity Interactive activities are free with an account.

Replica lag

Because replication to read replicas is asynchronous, a replica can be seconds or more behind the primary. A user who updates their profile and immediately reads it from a replica may see the old value. Reads that must reflect the latest writes, such as reading back a user's own changes or checking a balance before a payment, should therefore go to the primary, while reads that tolerate slight staleness, such as reports and product listings, go to replicas. The ReplicaLag CloudWatch metric shows how far behind each replica is and should be alarmed on.

Sort into groups

Primary or replica?

Exam skills 3.3.1

Decide where each read should go, given replica lag.

Log in to try this activity Interactive activities are free with an account.

Cross-Region replicas

Read replicas can also be created in other Regions. They serve reads close to users who are far from the primary Region, reducing latency. They also provide a disaster recovery copy: if the primary Region fails, a cross-Region replica can be promoted manually to become a standalone primary, with data loss limited to the replication lag.

Cross-Region replication incurs inter-Region data transfer charges, while replication within a Region is free. For Aurora, Aurora Global Database provides faster cross-Region replication and managed failover.

Worked example

A stale read after an update

After adding read replicas, users report that their profile changes sometimes do not appear when the page reloads. The application sends every read to the replicas.

CauseFix
Asynchronous replication lags behind the primaryRead a user's own data from the primary right after they change it
Lag grows under heavy write loadMonitor ReplicaLag and alarm on it
Reporting queries slow the replicasGive reports their own replica

Read replicas suit reads that can tolerate slight staleness, such as product listings and reports. Reads that must reflect the latest write go to the primary.

Check yourself

How can a read replica help with disaster recovery?

Show answer

A cross-Region read replica keeps a copy of the data in another Region and can be promoted to a standalone database if the primary Region fails.

Exam Tips

Remember for the exam

  • To scale reads or offload reporting, use read replicas.
  • For cross-Region disaster recovery for RDS, use a cross-Region read replica, promoted on failure.
  • When users do not see their latest write when reading from a replica, the cause is replica lag; read from the primary.
  • Promoting a read replica is manual and breaks replication.