Sub topic 9
High availability with Multi-AZ
This chapter assumes you have already provisioned a DB instance (the earlier chapters on VPC placement and creating an instance), that you know what an Availability Zone (AZ) is, and that you can use the AWS CLI and boto3. Multi-AZ is how RDS keeps a database available when an instance, a disk, or a whole AZ fails. It is also often confused with read replicas, so the chapter spells out the differences.
Two Multi-AZ deployment types
RDS offers two ways to run a database across AZs.
- Multi-AZ DB instance deployment. RDS keeps one primary and one standby in a different AZ. The primary replicates every committed change to the standby synchronously, so a transaction is not acknowledged until the standby has it. You cannot connect to the standby or send it queries. It exists only to take over.
- Multi-AZ DB cluster deployment. RDS runs one writer and two reader standbys in three AZs. Replication is semi-synchronous: the writer waits for at least one reader to acknowledge before it commits. The readers can serve read-only queries. This option is available for RDS for MySQL and RDS for PostgreSQL only.
| Property | Multi-AZ DB instance | Multi-AZ DB cluster |
|---|---|---|
| Standbys | 1, not readable | 2, readable |
| AZs used | 2 | 3 |
| Typical failover | 60 to 120 seconds | Usually under 35 seconds |
| Endpoints | One instance endpoint | Writer, reader, and per-instance endpoints |
| Engines | MySQL, MariaDB, PostgreSQL, Oracle, SQL Server, Db2 | MySQL, PostgreSQL |
Failover times vary with workload, so treat them as planning figures, not guarantees. Check the current RDS documentation for the engines and versions each type supports.
The rest of the chapter focuses on the instance deployment, which is the default choice. The cluster deployment follows the same principles with faster failover and readable standbys.
How a failover works
You connect to the instance endpoint, which is a DNS name. When RDS detects a failure, it promotes the standby and changes the DNS record so the same name points to the new primary. Your connection string does not change.
RDS starts a failover automatically when:
- the AZ becomes unavailable,
- the primary instance fails or becomes unreachable,
- the primary's storage fails,
- the instance class is changed,
- the operating system is patched, or
- you reboot with failover.
After a failover, RDS builds a new standby in the original AZ. During that time you have a single copy of the data, so the instance is not yet protected against a second failure.
What your application sees
Every open connection to the old primary is dropped. For roughly one to two minutes, new connections fail or time out. Then the endpoint resolves to the new primary. Two client behaviors decide how bad this is:
- DNS caching. If your runtime caches the endpoint's IP address for a long time, it keeps connecting to the dead host. Keep the DNS time to live (TTL) low, around 30 seconds or less, and never hard-code an IP address. JVM-based clients are a common offender, because they can cache lookups indefinitely under some security settings.
- Connection pools. A pool full of dead connections returns errors until it discards them. Configure validation queries or connection timeouts, and retry with backoff.
Enabling Multi-AZ
For a new instance, add --multi-az to create-db-instance. For the existing demo-pg instance, modify it:
aws rds modify-db-instance \
--db-instance-identifier demo-pg \
--multi-az \
--apply-immediatelyWithout --apply-immediately, the change waits for the next maintenance window. RDS takes a snapshot of the primary, restores it in another AZ as the standby, and then synchronizes the two. The primary stays online during this, though you may see higher latency until the standby catches up. The instance status shows modifying while this runs.
Your DB subnet group must contain subnets in at least two AZs, which the networking chapter already required. RDS picks the standby's AZ from that group.
To convert a Multi-AZ instance back to single-AZ, run the same command with --no-multi-az.
Testing a failover
Do not wait for a real outage to find out how your application behaves. Force a failover in a test environment:
aws rds reboot-db-instance \
--db-instance-identifier demo-pg \
--force-failoverReboot with failover is available only on Multi-AZ instances. This script does the same through boto3, waits for the instance, and prints the events RDS recorded:
import boto3
rds = boto3.client("rds")
rds.reboot_db_instance(DBInstanceIdentifier="demo-pg", ForceFailover=True)
rds.get_waiter("db_instance_available").wait(DBInstanceIdentifier="demo-pg")
events = rds.describe_events(
SourceIdentifier="demo-pg",
SourceType="db-instance",
Duration=30, # minutes of history
)["Events"]
for event in sorted(events, key=lambda e: e["Date"]):
print(event["Date"], event["Message"])Run a client loop that writes a row every second while the failover happens. The gap between the last successful write and the first one afterward is your real failover time. You can also confirm the roles swapped by checking SecondaryAvailabilityZone in describe-db-instances before and after. The primary's AvailabilityZone and the standby's AZ trade places.
What Multi-AZ does not do
Several things people expect from Multi-AZ are outside its scope.
- It is not a backup. Replication is synchronous, so a bad
DELETEorDROP TABLEis applied to the standby immediately. Recovering from mistakes requires automated backups and point-in-time restore, covered in the previous chapter. - It does not scale reads in the instance deployment. The standby refuses connections. Use read replicas for read scaling. Only the cluster deployment gives you readable standbys.
- It does not span Regions. Both copies live in one Region. Regional disaster recovery needs a cross-Region read replica or cross-Region snapshot copies.
- It does not protect against every failure. A runaway query, a lock pile-up, or an exhausted connection limit hits the primary, and the standby takes over only for infrastructure faults. A failover will not fix a slow query.
- It does not remove the need to size correctly. The standby is the same instance class as the primary, so you pay twice the instance and storage cost.
Multi-AZ and read replicas compared
| Question | Multi-AZ instance | Read replica |
|---|---|---|
| Main purpose | Availability | Read scaling, Regional DR |
| Replication | Synchronous | Asynchronous |
| Serves queries | No | Yes |
| Failover | Automatic | Manual promotion |
| Can lose recent commits on failover | No | Yes, by the replication lag |
| Cross-Region | No | Yes |
You can combine them. A Multi-AZ primary with read replicas is a common production layout.
Operational side effects
Multi-AZ also changes how maintenance and backups behave.
- Maintenance. For OS patching and instance class changes, RDS updates the standby first, fails over, then updates the old primary. Downtime shrinks to roughly the failover time instead of the full patch duration. The maintenance chapter returns to this.
- Backups. Snapshots and automated backups are taken from the standby, so the primary does not pause I/O for the backup.
- Cost. Expect roughly double the single-AZ instance and storage price. Data transfer between the two AZs for replication is not billed separately.
- Write latency. Synchronous replication adds a cross-AZ round trip to every commit. For most workloads this is a millisecond or two, but a chatty single-row-commit workload can notice it. Batch writes into larger transactions where you can.
Choosing a deployment
Use a single-AZ instance for development, tests, and workloads that tolerate an outage of tens of minutes while you restore from a backup. Use a Multi-AZ DB instance for most production databases. Consider a Multi-AZ DB cluster when you need failover in about half a minute, want readable standbys without a separate replica layer, or have high write throughput on supported engines.
Exercise
A team runs a production PostgreSQL instance with Multi-AZ enabled. One afternoon an engineer runs an UPDATE without a WHERE clause and overwrites a column in every row. The team plans to fail over to the standby to get the old data back. Will this work, and what should they do instead?
Show answer
It will not work. Multi-AZ replicates synchronously, so the standby already applied the bad UPDATE and holds the same wrong data. The correct recovery is a point-in-time restore to a moment just before the UPDATE, which creates a new DB instance from the automated backups and transaction logs. The team can then copy the correct data back to the original, or point the application at the restored instance.
Exercise
After enabling Multi-AZ, an application keeps failing for 10 minutes after each failover, even though the RDS console shows the instance as available within two minutes. Name two likely client-side causes and a fix for each.
Show answer
First, the client caches the DNS lookup of the endpoint and keeps connecting to the old primary's IP address. The fix is to lower the runtime's DNS cache TTL (about 30 seconds or less) and never use a hard-coded IP. Second, the connection pool holds dead connections and does not discard them. The fix is to enable connection validation or timeouts in the pool and retry failed requests with backoff.