Sub topic 11
Health checks and automatic failover
This chapter assumes you know how an Application Load Balancer (ALB), target groups, and Auto Scaling groups (ASGs) fit together from earlier chapters. Here you will see how health checks decide when traffic moves away from a failed instance or zone, and where the defaults can mislead you.
Two layers of health checking
Failover on AWS rests on two separate checks that are easy to confuse.
- Load balancer health checks probe each target. A target that fails is removed from rotation, so users stop reaching it. The target is not replaced.
- Auto Scaling health checks decide whether an instance should be terminated and replaced. By default an ASG only checks EC2 status (instance state and system status). It does not know your application has hung.
To connect the two, set HealthCheckType: ELB on the ASG. The ASG then also treats a target group "unhealthy" result as a reason to replace the instance. The ASG template from the earlier chapter already does this:
HealthCheckType: ELB
HealthCheckGracePeriod: 120The grace period is the time after launch during which the ASG ignores failed checks. Set it longer than your application's startup time. If it is too short, the ASG kills instances that are still booting and you get a replacement loop.
Tuning the target group check
A target group check has a path, a port, and thresholds. The thresholds control how fast you detect failure and how often you get false alarms.
| Setting | Effect of a low value | Effect of a high value |
|---|---|---|
| Interval | Faster detection, more probe traffic | Slower detection |
| Unhealthy threshold | Quicker removal, more flapping | Slower removal |
| Healthy threshold | Quicker return to service | Slower return to service |
| Timeout | Fails slow responses | Tolerates slow responses |
Detection time is roughly interval multiplied by unhealthy threshold. With a 10 second interval and a threshold of 3, a dead target takes about 30 seconds to leave rotation.
aws elbv2 modify-target-group \
--target-group-arn "$TARGET_GROUP_ARN" \
--health-check-path /healthz \
--health-check-interval-seconds 10 \
--healthy-threshold-count 2 \
--unhealthy-threshold-count 3Shallow versus deep checks
A shallow check returns 200 if the process is running. A deep check also tests dependencies such as the database. Deep checks look safer, but they carry a risk. If the database has a brief outage, every instance in every AZ fails its check at once. The load balancer then has no healthy targets, and a partial problem becomes a full outage.
A common compromise is to check only what the instance itself needs to serve traffic, and to report dependency problems through metrics and alarms instead. When all targets in a target group are unhealthy, an ALB sends traffic to all of them anyway (fail-open behavior), which limits the damage but does not fix the cause.
How a zone failure plays out
Note that traffic shifts first, within seconds. Replacement is slower and depends on launch time.
Database failover
RDS Multi-AZ fails over automatically. It changes the DNS record of the endpoint to point at the standby, so your application must reconnect and must not cache the resolved IP address indefinitely. Failover usually takes 60 to 120 seconds, and you can measure it yourself:
aws rds reboot-db-instance \
--db-instance-identifier mydb \
--force-failoverCommon errors
- Leaving the ASG on the default EC2 health check, so hung applications are never replaced.
- A grace period shorter than startup time.
- A health check path that needs authentication or a redirect, which returns 301 or 401 and marks every target unhealthy.
- A deep check that takes the whole fleet out when one dependency blips.
Exercise
Your ASG uses the default health check type. An application process on one instance deadlocks, but the instance itself keeps running. The load balancer stops sending it traffic. Will the ASG replace the instance, and what change fixes this?
Show answer
No. The EC2 status checks still pass, so the ASG considers the instance healthy and never replaces it. Set the ASG health check type to ELB so a failed target group check also triggers replacement, and set a grace period longer than the application's startup time.