Sub topic 5
Health checks and automatic failover
This chapter assumes you know basic DNS, load balancers, and the routing options from the previous chapter. It covers how a multi-region system decides a region is unhealthy and moves traffic away from it.
What a health check decides
A health check is a probe that a monitor sends to an endpoint on a schedule. The monitor marks the endpoint unhealthy after a set number of failed probes in a row. The routing layer then stops sending users there.
Failover is automatic in active-active because every region already serves traffic. No region has to be promoted. Unhealthy regions are removed from the rotation, and the healthy ones take over their share.
Shallow and deep checks
A shallow check only confirms the process answers, for example a 200 OK from /ping. It is fast and rarely gives false alarms, but it misses a region whose database is unreachable.
A deep check exercises dependencies such as the database, cache, and queue. It catches real problems, but it has a risk. If a shared dependency is slow, every region can fail its deep check at once, and the router may then have no healthy region to use.
A common compromise is a deep check for diagnosis and alerting, and a lighter check that the router acts on.
Tuning detection speed
Detection time is roughly the probe interval multiplied by the failure threshold, plus the time for clients to stop using the old answer.
| Setting | Faster failover | Fewer false alarms |
|---|---|---|
| Probe interval | Short | Long |
| Failure threshold | Low | High |
| DNS TTL | Short | Long |
For example, a 10-second interval with a threshold of 3 takes about 30 seconds to detect a failure. With a 60-second DNS TTL, some clients may keep using the dead region for another minute. DNS-based failover is never instant, because resolvers and browsers cache answers.
Attaching a health check to a route
In Route 53, you attach a health check to each record. If the check fails, Route 53 stops returning that record.
resource "aws_route53_health_check" "us" {
fqdn = "us.api.example.com"
port = 443
type = "HTTPS"
resource_path = "/healthz"
request_interval = 10
failure_threshold = 3
}Reference its ID from the record with health_check_id, as in the latency record shown earlier. Create one record and one health check for each region.
Common errors
- Checking the wrong thing. A
/healthzthat always returns 200 hides a broken database. - Probing from one location. A network problem near the monitor can look like a regional outage. Use probes from several locations.
- Flapping. A region that alternates between healthy and unhealthy causes traffic to bounce. Require several consecutive passes before returning a region to rotation.
- Failover without capacity. Moving traffic to a region that cannot absorb it turns one outage into two. The capacity planning chapter covers sizing.
- Ignoring data. Routing can move users in seconds, but the data in the failed region may not have replicated yet. Failover does not remove that gap.
Exercise
A router probes each region every 10 seconds and marks it unhealthy after 3 consecutive failures. DNS answers have a 60-second TTL. What is the worst-case time before all clients stop using a failed region, and which two settings would you change to shorten it?
Show answer
About 30 seconds to detect plus up to 60 seconds of cached answers, so roughly 90 seconds in the worst case. Shorten it by lowering the DNS TTL and by lowering the probe interval or failure threshold. Lower values raise the risk of false alarms and flapping, so test the change.