Sub topic 5
Health checks and automatic failover
This chapter assumes you know how DNS records and TTLs work, what a load balancer does, and the basics of global traffic routing from the previous chapter. It covers how a multi-region system decides that a region is unhealthy, how it moves traffic away, and how that decision goes wrong.
What a health check decides
In active-active, every region serves live traffic. A health check answers one question: should this region receive traffic right now? Failover is the routing change that follows a "no" answer. In an active-active design, failover means removing one region from the rotation. The other regions already serve users, so nothing needs to be promoted or started.
That is the main operational advantage over active-passive. There is no cold standby to wake up. The cost is that the surviving regions must absorb the extra load immediately. The capacity chapter covers sizing for that.
Layers of health checking
Health is checked at several layers, and each layer has its own failure behavior.
| Layer | Example | Typical interval | What it protects against |
|---|---|---|---|
| Instance or container | Load balancer target check | 5 to 30 s | A crashed or hung process |
| Regional load balancer | Healthy target count per region | Derived from the layer above | A region with too few working targets |
| Global routing | DNS or global load balancer probe of the regional endpoint | 10 to 30 s | A region that is unreachable or failing as a whole |
| Client | Retry against another endpoint | Per request | Failures that routing has not detected yet |
The layers feed each other. Instances fail first, the regional load balancer stops using them, and only when too few remain should the global layer remove the region. Each layer hides small failures from the layer above it.
Shallow and deep checks
A shallow check confirms that the process answers. It returns 200 if the web server is up. A deep check also exercises dependencies such as the database, cache, or a downstream service.
Shallow checks miss real outages. A service can return 200 on /healthz while every database call times out. Deep checks catch that, but they create a worse risk when a dependency is shared.
Suppose every region's deep check calls one global authentication service. That service fails. Every region now reports unhealthy, and the routing layer tries to remove all of them. You have turned a partial dependency failure into a total outage.
Use these rules:
- Check dependencies that are local to the region, such as its own database replica or cache.
- Do not fail a region's health check because of a dependency that every region shares.
- Give the endpoint a strict time budget, shorter than the checker's timeout.
- Return a status code the checker understands, and keep the endpoint cheap so probes do not add load.
Configuring a health check
The following Terraform defines an HTTPS check against a regional endpoint. The latency record from the routing chapter references it through health_check_id.
resource "aws_route53_health_check" "us" {
fqdn = "us.api.example.com"
port = 443
type = "HTTPS"
resource_path = "/healthz"
request_interval = 10
failure_threshold = 3
}Route 53 runs this probe from several checker locations. By default it treats the endpoint as healthy when more than 18 percent of the checkers report healthy. This stops a network problem between one checker and your region from triggering failover.
How long failover takes
Failover time is the sum of several delays. With the settings above:
- Detection: 10 s interval times 3 failures is about 30 s.
- Propagation: the routing layer updates its answers, usually within seconds.
- Caching: resolvers may keep the old answer for up to the record TTL, 60 s here.
- Client behavior: some clients and resolvers hold answers longer than the TTL. Long-lived connections never re-resolve at all.
A realistic estimate is one to three minutes, with a tail of users who take longer. Lowering the interval and TTL shortens this, at the price of more probe traffic and a higher chance of false positives. Anycast and global load balancers avoid the caching step because they steer traffic at the network layer, but the detection delay remains.
Your RTO target from the goals chapter must be larger than this total. If it is not, add client-side retries to a second region so the application does not wait for DNS.
Avoiding flapping with hysteresis
A region that alternates between healthy and unhealthy causes repeated routing changes. Each change can shift load, which stresses the surviving regions and can cause more failures. The fix is hysteresis: require several failures to mark a region down and a larger number of successes to mark it up.
class RegionHealth:
"""Mark a region down after `fail_after` consecutive failures
and up again only after `recover_after` consecutive successes."""
def __init__(self, fail_after=3, recover_after=10):
self.fail_after = fail_after
self.recover_after = recover_after
self.healthy = True
self.streak = 0 # consecutive results that disagree with current state
def record(self, probe_ok):
if probe_ok == self.healthy:
self.streak = 0
else:
self.streak += 1
needed = self.recover_after if probe_ok else self.fail_after
if self.streak >= needed:
self.healthy = probe_ok
self.streak = 0
return self.healthy
h = RegionHealth()
results = [True, False, False, True, False, False, False]
print([h.record(r) for r in results])
# [True, True, True, True, True, True, False]The single success at position 4 resets the failure streak, so only three consecutive failures trip the check. Recovery is deliberately slower than failure.
Failure patterns that checks miss
- Gray failures. The region is slow, or fails 20 percent of requests. A simple up or down probe passes. Track error rate and latency percentiles from real traffic and alert or drain on those signals.
- Probe path differs from user path. Checkers may reach a region that users cannot, for example when a regional ISP is down. Combine external probes with real-user monitoring.
- Healthy but stale. The region responds correctly but its data replica is minutes behind. Consider adding replication lag to the health decision, or at least to an alarm. Serving stale reads might be acceptable. Accepting writes on a lagging region might not be.
- Checker failure. If your health-check system itself fails, you may get no signal at all. Know whether it fails open or closed.
Failing back
Failing back, the return of traffic to a recovered region, is riskier than failing over. When the region comes back, its caches are cold and its data may be behind. Sending full traffic at once can overload it and cause a second outage.
A safer approach:
- Confirm the region has caught up on replication.
- Warm caches and connection pools.
- Shift traffic gradually, for example with weights of 5, 25, 50, then 100 percent.
- Watch error rates at each step before moving on.
Many teams make failover automatic and failback manual or gated by a person. Failover is urgent and failback is not.
Common errors
- Setting the failure threshold to 1, which turns every dropped probe into a failover.
- Using a deep check that depends on a shared global service.
- Setting a low RTO but a high TTL, or relying on clients that ignore TTLs.
- Never testing failover, so the first real test is a real outage. The observability chapter covers failure drills.
- Letting automation remove a region without checking that the others have capacity for the extra load.
Exercise
A team uses a 30 s probe interval, a failure threshold of 3, and a DNS TTL of 120 s. Their RTO target is 60 s. Does the design meet it? Name two changes that would help without adding client retries.
Show answer
No. Detection alone takes 30 s times 3, which is 90 s, and cached answers can add up to 120 s more, so worst-case failover is over three minutes. Two changes: reduce the probe interval (for example to 10 s, giving 30 s detection) and lower the TTL (for example to 30 or 60 s). A third option is moving to anycast or a global load balancer that does not depend on DNS caching.
Exercise
Every region's /healthz endpoint calls a single global identity service. Why is this risky, and what would you change?
Show answer
If the identity service fails, all regions report unhealthy at once, so the routing layer tries to remove every region and a partial failure becomes a total outage. Change the check to test only regional dependencies, such as the local database and cache. Report the identity service through monitoring and alerts rather than through the routing health check.