We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Read Replicas

Think about how Discord or Slack works for a moment. There are chat channels filled with tons of users. Do you, as a user, read more or write more to the server?

Unless you're an incredibly chatty person, you're going to read waaaaaay more messages than you send. Wouldn't it be nice to offload those reads from an expensive primary database to a cheaper server?

Enter read replicas.

How Read Replicas Work

  • Asynchronous replication: Data is copied from the primary database to replicas continually
  • Read-only: Replicas handle read queries (SELECT statements)
  • Write operations: Still go to the primary database (INSERT, UPDATE, UPSERT, DELETE)
  • Automatic updates: Replicas stay in sync with the primary, give or take a few seconds

Why Use Read Replicas?

  1. Read performance: Distribute read traffic across multiple instances
  2. Reduce load: Take read load off the primary database
  3. Scalability: Add more replicas as traffic grows
  4. Geographic distribution: Place replicas closer to users in different regions, so they at least get fast reads
  5. High availability: Use replicas for read-only failover if the primary fails

Read replicas can lag behind the primary by a few seconds due to asynchronous replication. This is usually fine for most use cases, but if you need real-time consistency, you'll need to read from the primary.

Assignment

Traffic is growing and the database is starting to sweat. Engineering wants to offload read traffic to a replica so the main DB can focus on writes.

Create a read replica (patientping-replica) from your existing patientping-db instance.

Cost check: Read replicas cost the same as the primary instance (given the same instance class). A db.t3.micro replica costs about $0.018 per hour (~$13 per month). We'll delete it after testing.

Once the replica status shows Available, check its domain name. You'll see that it's different from the primary DB.

Run and submit the CLI tests.

Your applications (or data analysts?) can connect to the read replica just like the primary database, but remember: it's read-only. Any write operations (INSERT, UPDATE, DELETE) will fail. Only SELECT queries will work.

Tip

For CLI users, create a read replica with the following command:

aws rds create-db-instance-read-replica --db-instance-identifier patientping-replica --source-db-instance-identifier patientping-db --db-instance-class db.t3.micro --vpc-security-group-ids YOUR-SG-ID --no-publicly-accessible