Sub topic 9
High availability with Multi-AZ
This chapter assumes you can create an RDS instance (see "Creating a DB instance"), you know what an Availability Zone (AZ) is, and you understand how a DNS name maps to an IP address.
What Multi-AZ does
A Multi-AZ deployment keeps a second copy of your database in a different AZ in the same region. If the primary instance fails, RDS promotes the standby and your application keeps using the same endpoint. The goal is availability and durability. It is not a way to scale reads.
RDS offers two forms:
- Multi-AZ DB instance deployment. One primary and one standby. The standby is not readable. Replication is synchronous, so a write is acknowledged only after it is stored in both AZs.
- Multi-AZ DB cluster deployment. One writer and two readable standbys in three AZs. It is available for MySQL and PostgreSQL and uses semi-synchronous replication. Failover is usually faster, and the reader endpoint can serve read queries.
The rest of this chapter focuses on the instance deployment, which is the most common choice.
How failover works
You connect to a DNS name, the endpoint. During failover RDS updates that name to point at the standby's IP address. The standby becomes the new primary and RDS then builds a new standby.
Failover starts automatically when RDS detects any of these:
- An AZ outage or loss of the primary host
- Failure of the primary instance or its storage
- A change of instance class or an OS patch on the primary
- A manual reboot with failover
Failover typically takes 60 to 120 seconds. Existing connections drop, so your application must reconnect.
Enabling Multi-AZ
Add --multi-az when you create an instance. You can also turn it on for an existing instance:
aws rds modify-db-instance \
--db-instance-identifier demo-pg \
--multi-az \
--apply-immediatelyRDS takes a snapshot, creates the standby from it, and syncs the changes. The instance stays available, but you may see some performance impact while this runs.
To test your application's behavior, force a failover:
aws rds reboot-db-instance \
--db-instance-identifier demo-pg \
--force-failoverAfterwards, check the instance's events to confirm the failover and see which AZ is now primary.
Common mistakes
- Treating the standby as a read replica. In an instance deployment you cannot query the standby. Use a read replica for read scaling (next chapter).
- Caching DNS for too long. If your client or runtime caches the endpoint's IP address, it keeps contacting the old primary. Keep the DNS TTL low and use the endpoint name, never an IP address.
- No reconnect logic. Connections break during failover. Use retries with backoff, or a connection pool that validates connections.
- Expecting zero downtime. There is a short outage. Multi-AZ protects against infrastructure failure, not against mistakes such as
DROP TABLE. That is what backups and point-in-time restore are for. - Forgetting the cost. You pay for the standby instance and its storage, roughly double the single-AZ price.
Exercise
Your application reads from the RDS endpoint and runs heavy reporting queries. A teammate suggests enabling Multi-AZ on a DB instance deployment to offload those queries to the standby. Will that work? What should you use instead?
Show answer
No. In a Multi-AZ DB instance deployment the standby is not readable, and it exists only for failover and durability. To offload reporting queries, create a read replica with create-db-instance-read-replica, or use a Multi-AZ DB cluster deployment, whose reader endpoint serves reads. You can combine a read replica with Multi-AZ if you need both read scaling and high availability.
Exercise
After a failover, one service keeps failing for several minutes while others recover within two minutes. It connects using a hard-coded IP address obtained earlier from a DNS lookup. What is the cause and the fix?
Show answer
The IP address belongs to the old primary. RDS repoints only the endpoint's DNS name to the standby. Change the service to connect with the endpoint hostname, keep DNS caching short, and add reconnect logic.