← Solutions Architect Associate study path

Resilient architectures · 13 min

Amazon RDS Multi-AZ and read replicas

In this lesson you will learn to tell apart a Multi-AZ DB instance, a Multi-AZ DB cluster and Amazon RDS read replicas, and to choose the one a question calls for when it talks about surviving the failure of a zone, a fast failover or too many reads.

Amazon RDS is the AWS service that gives you a database ready to use, without you having to install it or look after it. A database is the program that stores data in an organized way and hands it back when the application asks for it. For example, an online store keeps its products, its customers and every order placed in its database.

A standard Amazon RDS instance runs in a single Availability Zone, which is a group of AWS buildings kept apart from the others inside a Region, so if that zone fails, the database goes down. This happens even if the load balancer and the application instances are in three zones, because the application doesn't work without its database. For example, the store can have its website in three zones, but if its only database is in the zone that fails, nobody can buy anything.

Amazon RDS has two features that are often confused because both create copies of the database, although they solve different problems:

  • Multi-AZ, which keeps the database available if a zone fails.
  • Read replicas, which keep the database from being overwhelmed by read queries, the requests that only ask to see data without changing it.

The same case tells them apart. For example, the store uses Multi-AZ to keep selling if a zone goes down, and it uses a read replica so the sales reports, which only read data, don't slow down purchases.

The three Amazon RDS options: a Multi-AZ DB instance with a standby that doesn't serve reads, a Multi-AZ DB cluster with one writer and two readers in three zones, and the asynchronous read replica that doesn't fail over on its own

How does a Multi-AZ DB instance work?

A Multi-AZ DB instance is the first form of Multi-AZ, where RDS creates and maintains a standby, which is a waiting copy, in another zone. The database that receives the work is called the primary. Replication between the primary and the standby is synchronous, which means that every write, every new or changed piece of data, is stored in both before it is confirmed. That is why a write can take a little longer than with the database in a single zone. For example, when a customer pays, the order is saved in both zones before the store tells them the purchase went through.

An application connects to the instance's DNS endpoint; the primary in zone A replicates synchronously to a standby in zone B that doesn't serve reads

The application connects to a single endpoint, which is the named address where the database receives connections. That name lives in . The application never sends queries to the standby, because the standby doesn't serve reads. To send read-only queries to another copy, the documentation recommends a Multi-AZ DB cluster or a read replica. For example, even though the store's standby has every order, it can't be used to run the sales report.

Failover

Failover is the automatic switch from the primary to its copy when the primary fails. If the primary or its zone fails, RDS changes the endpoint's DNS record so that it points to the standby, and the application reconnects to the same name. According to the documentation, this failover usually takes 60 to 120 seconds. For example, if the zone of the store's database goes down, purchases fail for a minute or two and then continue without anyone changing the application's configuration.

cloud path tip: if the application is written in Java, AWS recommends that the use a DNS TTL of 60 seconds or less. The TTL is how long the application remembers the database's address before asking for it again, and with a short one it doesn't keep the old IP after the failover.

How does a Multi-AZ DB cluster work?

A Multi-AZ DB cluster is the second form of Multi-AZ, and it has one writer and two readers in three zones of the same Region. The writer receives the changes and the readers are copies that get them from it. Its replication is semisynchronous, which means that a change is confirmed when at least one of the readers reports that it received it. According to the documentation, the write latency, the time it takes to save a change, is lower in this form than with a Multi-AZ DB instance. For example, the store confirms each order as soon as it reaches one of the two copies, without waiting for the other one.

One writer and two readers in three zones, with semisynchronous replication and writer and reader endpoints

The cluster's two readers serve two purposes at the same time. They handle read queries and they are also the failover target, and according to AWS the failover usually takes less than 35 seconds. For example, the store runs its sales report from a reader, and if the writer goes down, that same reader starts receiving the orders.

This form of Multi-AZ doesn't exist for every . The Multi-AZ DB cluster isn't available for RDS for MariaDB, Oracle, SQL Server or Db2, so in practice it is left for MySQL and PostgreSQL. Also, it shouldn't be confused with an Amazon Aurora cluster, which is a different AWS database service. For example, if the store uses SQL Server, it can only use a Multi-AZ DB instance.

Multi-AZ DB instance or Multi-AZ DB cluster?

0 of 4

Tap the option that meets each requirement.

  1. An RDS for Oracle database that must survive the failure of a zone.

  2. The high availability copies must also serve reads.

  3. Failover in less than a minute with RDS for PostgreSQL.

  4. An RDS for SQL Server database with a standby in another zone.

What is a read replica?

A read replica is a read-only copy of the primary, and it solves the case of a database that can handle the writes but receives too many reads. To create it, RDS takes a of the primary, creates the replica from it and then sends it the changes asynchronously. Asynchronous means that the replica can fall a little behind, which is called lag, so a query on the replica may not see something that was written a second ago. For example, an order that just came in can take a moment to show up in the report that is run from the replica.

The primary in us-east-1 receives the writes; two replicas in the same Region receive reads; a third replica is in sa-east-1, all with asynchronous replication

With replicas, the application writes to the primary and reads from the replicas. Each replica has its own endpoint, and the application is the one that decides where to send each query. A primary can have up to 15 read replicas. For example, the store saves the orders in the primary and shows the catalog to visitors from two replicas.

Replicas in another Region

A replica can also be in another Region, and a replica like that is used to serve reads close to the users in that Region or to recover if the main Region goes down. However, a replica in another Region has more lag and generates data transfer costs between Regions. For example, a store with its database in us-east-1 and many customers in Brazil can place a replica in São Paulo so its catalog loads faster for them.

Promoting a replica

A replica doesn't fail over on its own, so to turn it into the main database you have to promote it, that is, ask RDS to make it an independent database that accepts writes. When you promote it, RDS reboots it before making it available, the process can take several minutes, and from that moment on the replica stops receiving changes from the primary. For example, if the store's main Region is lost, the team promotes the São Paulo replica and the store is selling again from there after a few minutes.

Does the standby work for this?

The data analytics team runs heavy reports every morning on an RDS for MySQL database with a Multi-AZ DB instance, and the application slows down. Someone proposes sending the reports to the standby. What does an architect answer?

What doesn't Multi-AZ cover?

Multi-AZ replicates every change, including mistakes, so if a program deletes a table, the standby receives the deletion too. To recover from a mistake like that there are RDS automated backups, which are backup copies that RDS makes on its own. They are made up of a daily snapshot and the that RDS uploads to every five minutes. With them you can restore the database to a point in time within the retention period, which is how many days those backups are kept. For example, if at 10:30 someone deletes the store's orders table by mistake, the team restores the database as it was at 10:25.

This retention period goes from 0 to 35 days, and its default value is 7 days from the and 1 day from the or the . The restore creates a new instance with a different endpoint and leaves the original as it was. For example, after restoring, the store has to change its configuration to connect to the new database's endpoint.

True or false?

0 of 3
  1. After a Multi-AZ failover, the application has to change the endpoint in its configuration.

  2. Multi-AZ protects against accidental deletion of data.

  3. A read replica can be in another Region.

On the exam

RDS questions usually describe a problem with the database and ask for the feature that solves it. Phrases such as "high availability", "automatic failover" or "survive the failure of a zone" point to Multi-AZ, while "many reads", "reports" or "the database is overwhelmed by queries" point to read replicas. If the question says that "the standby copies must serve reads" or asks for a "failover in less than 35 seconds", the answer is the Multi-AZ DB cluster, as long as the engine is MySQL or PostgreSQL. For example, if the question says the store's reports make the database slow, the answer is a read replica and not Multi-AZ.

cloud path tip: "recover from a deletion" calls for point-in-time restore, even if the database already has Multi-AZ. The features can also be combined, because a Multi-AZ primary can have read replicas.

Review question

An application uses RDS for PostgreSQL in a single zone. The team needs the database to stay available if a zone fails, with a failover of less than a minute, and the read load of some reports must not slow down the application. They want the smallest number of components. What do they choose?

Full account and new experience account

Amazon RDS is in the free plan of the new experience account, the simplified way to open an AWS account, and creating a database is one of the activities that earn credit. In that account you work within us-east-2 and you can't create a replica in another Region, because it doesn't support cross-Region features, something a full account does allow. For example, in the new experience you can practice a read replica in us-east-2, but not one in São Paulo.

cloud path tip: each read replica is billed like a standard database instance, so delete it when you finish practicing.

Next