Sub topic 11
Health checks and automatic failover
This chapter assumes you know how an Application Load Balancer (ALB) with target groups works, how an Auto Scaling group (ASG) launches instances, and how to read CloudFormation YAML. Earlier chapters covered spreading traffic across Availability Zones (AZs). Here you will see how AWS decides that something is broken and what it does next.
Why health checks drive failover
Spreading resources across AZs does not help if traffic keeps going to a dead resource. A health check is a probe that tells a system whether a target can serve requests. Automatic failover is the action taken when a probe fails: stop sending traffic, replace the resource, or promote a standby.
Several layers each run their own checks, and they answer different questions.
| Layer | Who checks | What it decides |
|---|---|---|
| Load balancer target group | ALB or NLB | Whether to route requests to a target |
| Auto Scaling group | EC2 status checks, optionally ELB results | Whether to terminate and replace an instance |
| Amazon RDS Multi-AZ | RDS monitoring | Whether to promote the standby |
| Amazon Route 53 | Route 53 health checkers | Whether to return a DNS record |
Failures happen when these layers disagree. For example, an ASG that only watches EC2 status checks will keep an instance whose application has crashed, because the virtual machine is still running.
Load balancer health checks
A target group probes each registered target on a path, port, and protocol that you choose. Two thresholds control how fast it reacts:
- The healthy threshold is how many consecutive successes mark a target healthy.
- The unhealthy threshold is how many consecutive failures mark it unhealthy.
Detection time is roughly the interval multiplied by the unhealthy threshold. With a 30-second interval and a threshold of 3, a dead target can receive traffic for about 90 seconds. With a 10-second interval and a threshold of 2, that drops to about 20 seconds. Shorter intervals react faster but increase the chance that a brief slowdown removes a good target.
Fail-open behavior
If every target in a target group is unhealthy, an ALB does not drop all traffic. It routes requests to all targets anyway, which is called fail-open. This gives a chance that some requests succeed when the health check itself is wrong, for example after a bad deployment of the health endpoint. It also means you cannot assume an unhealthy target never receives traffic.
With cross-zone load balancing on, fail-open applies to the whole target group. With it off, each load balancer node considers only the targets in its own AZ. If all targets in one AZ fail, that AZ's node may fail open or the zone can be removed from DNS, depending on the load balancer type and settings. Test this behavior rather than relying on memory.
Designing the health endpoint
The health endpoint is code you write, and a poor design causes outages.
- A shallow check returns success if the process is up. It is fast and rarely causes false alarms, but it will not catch a broken dependency.
- A deep check calls the database, cache, or other services. It catches more problems, but it can take every target out of service at once when one shared dependency fails.
That second risk matters for multi-AZ design. Suppose each instance checks the database and the database has a brief failover. Every instance in every AZ reports unhealthy, the ASG terminates them all, and a 30-second database blip becomes a long outage. A common rule is to keep load balancer checks shallow, or to check only things local to the instance, and to report dependency problems through metrics and alarms instead.
Auto Scaling health checks
By default an ASG uses only EC2 status checks. That detects hardware and hypervisor problems, not a hung application. Setting HealthCheckType: ELB makes the ASG also treat a target group's unhealthy result as a reason to replace the instance. The template below, from the earlier Auto Scaling chapter, shows the setting.
Resources:
WebAsg:
Type: AWS::AutoScaling::AutoScalingGroup
Properties:
MinSize: "2"
MaxSize: "6"
DesiredCapacity: "2"
VPCZoneIdentifier:
- !Ref PrivateSubnetA
- !Ref PrivateSubnetB
LaunchTemplate:
LaunchTemplateId: !Ref WebLaunchTemplate
Version: !GetAtt WebLaunchTemplate.LatestVersionNumber
TargetGroupARNs:
- !Ref WebTargetGroup
HealthCheckType: ELB
HealthCheckGracePeriod: 120HealthCheckGracePeriod is the number of seconds the ASG waits after an instance enters service before it acts on health results. Set it longer than your real startup time. If it is too short, the ASG will terminate instances that are still booting, and the replacements will fail the same way. This is a common cause of launch loops.
When an AZ becomes impaired, the ASG can rebalance by launching instances in the remaining AZs. That relies on the EC2 control plane and on capacity being available, which the next chapter on static stability addresses.
Failover in RDS Multi-AZ
For a Multi-AZ RDS instance, AWS handles health monitoring. When it detects a failure of the primary host, an AZ problem, or a storage problem, it promotes the standby and updates the DNS record behind the endpoint to point to it. Your application keeps using the same endpoint name.
Two consequences follow:
- Failover typically takes one to two minutes, and connections break during it. Clients need retry logic.
- Clients that cache DNS for a long time keep connecting to the old address. Keep DNS time-to-live (TTL) low in your connection layer and check your runtime's DNS caching, since some JVM and connection-pool settings cache indefinitely.
You can trigger a failover yourself to measure it. Do this in a test environment first.
aws rds reboot-db-instance \
--db-instance-identifier mydb \
--force-failoverRecord how long your application takes to recover. That number is the real recovery time for this layer, regardless of what the documentation says.
DNS failover with Route 53
Route 53 health checks work differently. Checkers in multiple locations probe your endpoint, and a failover or weighted record stops being returned when the health check fails. DNS is cached, so clients keep using the old answer until the TTL expires. A record with a 300-second TTL cannot fail over faster than about five minutes for clients that honor it. Use short TTLs on records that you expect to fail over.
Common errors
- The health check path needs authentication or a redirect, so it always returns a non-success code and every target is unhealthy.
- The grace period is shorter than boot time, which causes a replacement loop.
- The security group on the targets does not allow the load balancer to reach the health check port.
- The ASG uses EC2 checks only, so a crashed application is never replaced.
- The deep health check depends on a shared service, so one dependency failure removes the entire fleet.
- Application clients have no retry logic, so a database failover appears as user-facing errors.
Verifying behavior
Check the state of each target and its reason code, which shows why a target failed.
aws elbv2 describe-target-health \
--target-group-arn "$TARGET_GROUP_ARN" \
--query 'TargetHealthDescriptions[].[Target.Id,TargetHealth.State,TargetHealth.Reason]' \
--output tableReason codes such as Target.FailedHealthChecks and Target.Timeout point you toward the cause. Use them before changing thresholds.
Exercise
Your ASG uses HealthCheckType: EC2. An application on one instance deadlocks, the process is still running, and the ALB marks the target unhealthy. What happens to the instance, and what change fixes it?
Show answer
The ALB stops sending requests to the target, but the ASG does not replace the instance because the EC2 status checks still pass. The instance stays running and unused. Setting HealthCheckType: ELB makes the ASG terminate it and launch a replacement. Set HealthCheckGracePeriod longer than the instance's startup time so new instances are not replaced while booting.
Exercise
Each instance's health endpoint queries the RDS database. During a Multi-AZ failover of about 60 seconds, what is the risk with HealthCheckType: ELB, and how would you reduce it?
Show answer
Every instance fails its check at the same time, so the load balancer has no healthy targets and the ASG may terminate and replace the whole fleet, extending the outage beyond the database failover. Make the load balancer health check shallow or local to the instance, and report database problems through alarms and metrics instead. Also add client retry logic and low DNS caching so the application recovers when the standby takes over.