Sub topic 14
Testing Failover and Injecting Faults
A design that has never failed over is a hypothesis. This chapter shows how to turn that hypothesis into evidence by causing controlled failures and measuring what happens.
This chapter assumes you have read the earlier chapters on Multi-AZ VPC layout, load balancing, Auto Scaling groups, RDS Multi-AZ, health checks, and static stability. You should be comfortable with the AWS CLI and reading JSON templates.
Why test failover
Multi-AZ designs fail in ways that diagrams hide. Common examples:
- An application caches the database DNS name and keeps connecting to the old primary.
- A private route table sends all egress through a NAT gateway in the failed AZ.
- An Auto Scaling group cannot launch replacements because the surviving AZ has too little capacity or too few free IP addresses.
- A health check is too lenient, so the load balancer keeps sending requests to a broken target.
You find these problems in a test, on your schedule, or in an incident. Testing is the cheaper option.
The experiment loop
Treat each test as a small experiment, not as "turn something off and see." Chaos engineering practice gives a repeatable loop.
- Define steady state. Pick measurable signals, such as p99 latency, 5xx rate, and successful checkouts per minute. "The site works" is not measurable.
- Write a hypothesis. For example: "If all web instances in one AZ stop, the 5xx rate stays under 1% and the Auto Scaling group restores desired capacity within 5 minutes."
- Limit the blast radius. Start in a non-production environment with one tier and one AZ. Move to production only when you trust the stop conditions.
- Define stop conditions. These are alarms that end the experiment automatically if the impact exceeds what you accepted.
- Run, observe, compare, and fix. Record what differed from the hypothesis. Differences are the findings.
Controlled failover for managed services
The simplest test uses a failover that the service provides. For an RDS Multi-AZ instance, force a failover by rebooting:
aws rds reboot-db-instance \
--db-instance-identifier mydb \
--force-failoverWhile this runs, drive realistic traffic at the application and record:
- How long writes fail. RDS Multi-AZ failover commonly takes tens of seconds to a couple of minutes, depending on the engine and the load.
- Whether the application reconnects on its own or needs a restart.
- Whether connection pools drop stale connections. Pools that hold dead connections cause errors long after the database is healthy.
- Whether the application honors DNS changes. The endpoint name stays the same, but the IP address behind it changes. Long DNS caches, such as an overlong JVM TTL, delay recovery.
Confirm the failover happened by reading the RDS events:
aws rds describe-events \
--source-type db-instance \
--source-identifier mydb \
--duration 60 \
--query 'Events[].[Date,Message]' \
--output table--duration is in minutes. Look for messages about a Multi-AZ failover starting and completing.
For Aurora, the equivalent is aws rds failover-db-cluster --db-cluster-identifier mycluster. Aurora promotes a reader, so the test also shows whether your application separates writer and reader endpoints correctly.
Simulating loss of an AZ with AWS FIS
Managed-service failovers test one component. To test an AZ loss across tiers, use AWS Fault Injection Service (FIS). FIS runs actions against tagged resources, applies stop conditions, and uses an IAM role you control, so every action is auditable.
Useful FIS actions for multi-AZ testing:
| Goal | Typical action |
|---|---|
| Remove compute in one AZ | `aws:ec2:stop-instances` on instances filtered by AZ |
| Cut network paths for one AZ | `aws:network:disrupt-connectivity` with scope `availability-zone` |
| Fail over a database | `aws:rds:failover-db-cluster` or `aws:rds:reboot-db-instances` |
| Add latency or errors to a service | Fault injection through the Systems Manager agent on instances |
FIS also offers scenario library entries that combine actions to approximate an AZ power interruption. Check the current FIS documentation for the scenarios available in your Region.
The template below stops every running web-tier instance in one AZ, then restarts them after ten minutes. Replace the placeholder account ID, role, and alarm names with your own.
{
"description": "Stop all web-tier instances in us-east-1a",
"targets": {
"WebInstancesAz1a": {
"resourceType": "aws:ec2:instance",
"resourceTags": { "Tier": "web" },
"filters": [
{ "path": "Placement.AvailabilityZone", "values": ["us-east-1a"] },
{ "path": "State.Name", "values": ["running"] }
],
"selectionMode": "ALL"
}
},
"actions": {
"StopWebInstances": {
"actionId": "aws:ec2:stop-instances",
"parameters": { "startInstancesAfterDuration": "PT10M" },
"targets": { "Instances": "WebInstancesAz1a" }
}
},
"stopConditions": [
{
"source": "aws:cloudwatch:alarm",
"value": "arn:aws:cloudwatch:us-east-1:111122223333:alarm:checkout-5xx-high"
}
],
"roleArn": "arn:aws:iam::111122223333:role/fis-az-loss-role",
"tags": { "Name": "az-loss-web" }
}Create the template, then start an experiment from it:
aws fis create-experiment-template \
--cli-input-json file://az-loss-template.json
aws fis start-experiment \
--experiment-template-id "$TEMPLATE_ID"create-experiment-template returns the template ID. Export it as TEMPLATE_ID before the second command.
What to watch during the experiment
Open a dashboard before you start, and keep these views side by side:
- Load balancer metrics split by AZ:
HealthyHostCount,UnHealthyHostCount,HTTPCode_Target_5XX_Count, andTargetResponseTime. - Auto Scaling group activity history. It shows when instances were marked unhealthy, when replacements launched, and any launch failures.
- Database connection counts, replica lag, and RDS event messages.
- Your own application's error rate, measured from a client's point of view, such as a canary that runs from outside the VPC.
Compare the timestamps. The gap between "fault injected" and "traffic healthy again" is your measured recovery time, which you can compare directly against your RTO.
Shifting traffic away from an AZ
Not every test needs a fault. Amazon Application Recovery Controller (ARC) zonal shift moves load balancer traffic away from one AZ without touching your instances. It is both a test and a real mitigation tool.
aws arc-zonal-shift start-zonal-shift \
--resource-identifier "$LOAD_BALANCER_ARN" \
--away-from us-east-1a \
--expires-in 30m \
--comment "Game day: verify remaining AZs carry full load"Zonal shifts expire, which makes them safe to forget about. Use this test to answer one question: can the remaining AZs absorb the full load without scaling first? That is the static stability requirement from the previous chapter. If latency rises or targets become overloaded during the shift, your pre-provisioned headroom is too small.
Common mistakes
- Testing only the happy fault. Stopping instances is easy. Test the awkward ones as well: a failed deployment during an AZ loss, an expired credential, or a database failover while an Auto Scaling scale-in is running.
- Testing without load. Failover under zero traffic proves little. Generate traffic that resembles production.
- Skipping the restore step. Verify that capacity rebalances when the AZ returns. Auto Scaling may need to shift instances back, and that rebalancing can cause its own brief disruption.
- No stop condition. An experiment without an abort alarm is an outage you scheduled.
- Running once. Architectures drift. Schedule game days or run FIS experiments from a pipeline after significant changes.
Summary of a good test plan
| Element | Example |
|---|---|
| Steady state | 5xx rate under 0.5%, p99 under 400 ms |
| Hypothesis | One AZ lost, 5xx under 1%, capacity restored in 5 min |
| Fault | FIS stop-instances plus forced RDS failover |
| Stop condition | CloudWatch alarm on checkout 5xx |
| Evidence | Per-AZ dashboards, ASG activity, RDS events |
| Follow-up | Tickets for each gap, retest after fixes |
Exercise
You run a forced RDS failover in staging. The database is back in 40 seconds, but the application returns errors for another 6 minutes until you restart it. Give two likely causes and one fix for each.
Show answer
First, the application caches DNS for the database endpoint, so it keeps connecting to the old primary's IP address. Fix it by lowering the runtime's DNS cache TTL (for example, the JVM's networkaddress.cache.ttl) so it re-resolves the endpoint. Second, the connection pool holds dead connections and does not validate them. Fix it by enabling connection validation or a maximum connection lifetime so the pool discards broken connections and reconnects. In both cases, rerun the failover afterward to confirm the application recovers without a restart.
Exercise
An FIS experiment stops all web instances in one AZ. The load balancer shows no errors, but you cannot tell whether the design is statically stable. Which additional experiment answers that question, and what do you measure?
Show answer
Use an ARC zonal shift (or an experiment that also blocks launches in that AZ) so the remaining AZs must carry full load without scaling out. Measure target response time, 5xx rate, and CPU on the surviving instances during the shift. If these stay within your steady-state limits before any new instances launch, the design has enough pre-provisioned headroom.