← Solutions Architect Associate study path

Performance and cost · 12 min

Read replicas, caching and Amazon RDS Proxy

In this lesson you will learn what to add when a database falls short based on its symptom, among read replicas, an Amazon ElastiCache cache, Amazon RDS Proxy and DAX, and how a read replica differs from Multi-AZ.

A database falls short when it no longer answers what the application asks of it in time. When that happens, the simplest way out is to move it to a larger , and sometimes it is the right one. However, an SAA-C03 exam question can describe a more specific symptom and expect the tool that solves it. To know which symptom the database has, the AWS database guide asks you to look first at the access pattern, that is, how much the application reads and how much it writes. For example, an online store reads its catalog thousands of times for every order it writes, so its problem is almost always in the reads.

Four tools for a database that falls short, each one with its symptom: read replicas, Amazon ElastiCache, Amazon RDS Proxy and DAX

For each symptom there is a different tool:

  • Read replicas, which help when the database receives a lot of reads.
  • An Amazon ElastiCache cache, which helps when the application repeats the same reads.
  • Amazon RDS Proxy, which helps when the database receives too many connections, which are the open channels between the application and the database.
  • DAX, which helps when an application needs to read from DynamoDB in microseconds, which are millionths of a second.

Amazon RDS read replicas

A read replica is a read-only copy of an Amazon RDS instance: you can query it, but you can't write to it. What you write to the main instance, called the primary, is copied to the replica asynchronously, that is, the primary saves the change and sends it to the replica afterward, without waiting for the replica to confirm it. The application sends its read to the replica instead of the primary. For example, the store writes orders to the primary and sends catalog searches to the replica, so the primary stays free to take payments.

An application writes to the RDS primary and reads from two replicas in the same Region; a third replica in another Region is used for disaster recovery

Writes go only to the primary, and the replicas only serve reads. Because replication is asynchronous, the replica can fall behind the primary, and that delay is called lag. If the application reads right after writing and needs to see the new data, that read has to go to the primary. For example, the store's "your order was received" page reads from the primary, because the order might not show up in the replica yet.

An RDS instance supports up to 15 read replicas, each one with its own . A replica can also be in another , and if the main Region goes down, you promote it to a standalone instance to recover. For example, the store with its database in N. Virginia can have a replica in Ohio and, if N. Virginia fails, turn that replica into its new main database.

Read replicas and Multi-AZ

A read replica and Multi-AZ solve different problems. In a Multi-AZ instance, the standby, which is a backup copy, is in another Availability Zone and receives a synchronous copy, that is, each change is saved to both at the same time. The standby takes the place of the primary if it fails, in an automatic switch called a , but it doesn't serve reads. That is why Multi-AZ is for availability, while replicas are for spreading out reads. For example, Multi-AZ keeps the store running if its database's zone goes down, but it doesn't make catalog searches faster.

Amazon Aurora Replicas

Amazon Aurora has replicas built into the cluster, which is the group of instances of an Aurora database. It supports up to 15 Aurora Replicas on the same volume, which is the storage they all share, with a lag that is usually less than 100 milliseconds. Those replicas also serve as failover targets. Also, with Aurora Auto Scaling the cluster adds or removes replicas based on the load. For example, during a special sale Aurora can add replicas for the store's searches and remove them when the sale ends.

True or false?

0 of 3
  1. The standby of an RDS Multi-AZ instance can serve read-only queries.

  2. An RDS read replica receives the primary's changes asynchronously.

  3. A read replica in another Region can be promoted if the main Region goes down.

Caching for repeated reads

When the application requests the same data many times, for example the same catalog a thousand times per minute, the database doesn't need to compute it each time. Amazon ElastiCache is a cache: it keeps the results that are requested often in and delivers them in microseconds. The application is the one that decides how the cache is filled, and the documentation describes two strategies for doing it.

Lazy loading: the application asks the cache, and if the data isn't there it reads from the database and stores it. Write-through: it writes to the database and to the cache at the same time

Lazy loading only stores what someone has already requested. Each time the data isn't in the cache, which is called a miss, the read costs three trips: asking the cache, reading from the database and storing in the cache. Also, what is stored can become stale if the database changes. For example, if the store changes a product's price, the cache can keep showing the old price for a while.

Write-through, on the other hand, updates the cache on every write. With this strategy the data never becomes stale, but the cache fills up with data that maybe nobody reads. For example, the store would put every new product in the cache even if nobody ever searches for it.

For both problems there is the TTL (time to live), which is set on each key in the cache and says how long that data is valid. With a TTL, stale data expires and what nobody reads is deleted on its own. For example, with a short TTL on prices, the store's cache reads the price from the database again shortly after it changes.

Which caching strategy?

A news site shows articles that are almost never edited, and most old articles are never opened again. They want to use as little cache memory as possible. Which strategy do they use?

Amazon RDS Proxy for connection spikes

To talk to the database, an application first opens a connection, and some applications open and close connections all the time, such as Lambda functions that scale to hundreds of copies at once. Each new connection uses memory and on the database, so a spike in connections can bring it down even if the queries are simple. For example, if the store announces a deal and hundreds of Lambda functions open their own connection at the same time, the database can stop accepting connections.

Lambda functions connect to RDS Proxy, which keeps a pool of connections with the database

RDS Proxy keeps a pool of connections to the database open and shares it among the application's connections, like a telephone switchboard that shares a few lines among many calls. If more connections arrive than the database can handle, the proxy queues them or throttles them, and the database stays up.

Besides sharing connections, RDS Proxy helps with failover and with security. During a failover it connects to the standby and keeps the application's connections, so the application doesn't have to reconnect. It can also require authentication with IAM, the AWS permissions service, from clients and take the database from AWS Secrets Manager, which stores passwords securely. Almost all applications can use it with almost no changes to their code. For example, the store only changes the address its functions connect to so it points to the proxy.

DAX for DynamoDB reads in microseconds

Amazon DynamoDB responds in single-digit milliseconds, and to get down to microseconds there is DynamoDB Accelerator (DAX). DAX is an in-memory cache compatible with the DynamoDB that lowers those reads by an order of magnitude, that is, it makes them about ten times faster. For example, a game that shows the leaderboard to millions of players can read it from DAX instead of going to DynamoDB every time.

This cache has a limit that the exam can test. DAX serves eventually consistent reads, which can show data from a moment ago, so it isn't a good fit if the application needs strongly consistent reads, which always show the latest saved data. For example, a bank account balance that has to be exact after each payment isn't read from DAX.

If your account uses the new experience

In a new experience account, RDS, Aurora and ElastiCache are in the free plan, so you can create a read replica in the same Region. A replica in another Region can't be created, because this account doesn't support cross-Region features, and DAX isn't available. For example, you can practice sending reads to a replica in Ohio, but you can't create a replica in São Paulo.

On the exam

The questions on this topic describe the database's symptom. These words in the question point to the tool:

  • "Reads" or "reports" that overload the primary, which point to read replicas.
  • "Availability" or "automatic failover", which point to Multi-AZ and not to replicas.
  • "Repeated queries" or "data that is read a lot and changes little", which point to ElastiCache.
  • "Lambda", "too many connections" or "connection spikes", which point to RDS Proxy.
  • "Microseconds" together with DynamoDB, which point to DAX, unless the question asks for "strongly consistent reads".

For example, if the question says "the finance department's reports make the database slow for customers", the answer is a read replica for those reports.

Review question

An API on AWS Lambda queries an Amazon Aurora MySQL database. During promotions, Lambda scales to thousands of invocations at the same time and the database starts rejecting connections, even though its CPU stays below 40%. Which change solves the problem with the LEAST effort?

Next