Sub topic 8
Health checks and what makes a target healthy
This chapter assumes you know how to create a target group and register targets, and that you have a basic grasp of HTTP status codes and security groups. It covers how a load balancer decides whether a target can receive traffic, how to tune that decision, and the errors that cause healthy servers to be marked unhealthy.
What a health check does
A load balancer sends traffic only to targets it considers healthy. It decides this by running health checks, which are probes the load balancer sends to each registered target on a fixed schedule. Health checks are configured on the target group, not on the load balancer or the listener. Every target in the group is probed with the same settings.
Each load balancer node in each enabled Availability Zone runs its own probes. A target therefore receives one probe per node per interval, so your access logs will show several health check requests in each interval. This is normal.
The health check settings
For an Application Load Balancer, a target group has these settings.
| Setting | Meaning | Typical default |
|---|---|---|
| Protocol and port | Where the probe is sent. The port can be the traffic port or an override. | HTTP, traffic port |
| Path | The URL the probe requests. | `/` |
| Interval | Seconds between probes. | 30 |
| Timeout | Seconds to wait for a response before the probe counts as failed. | 5 |
| Healthy threshold | Consecutive successes needed to become healthy. | 5 |
| Unhealthy threshold | Consecutive failures needed to become unhealthy. | 2 |
| Success codes | HTTP status codes that count as success. | 200 |
Defaults differ by load balancer type and by how the target group was created, so check the values on your own group. The success codes setting accepts a single value (200), a list (200,204) or a range (200-299).
A simple model for detection time: a failing target is marked unhealthy after roughly interval × unhealthy threshold seconds. With a 30-second interval and a threshold of 2, that is about 60 seconds. A target is brought back after roughly interval × healthy threshold seconds of passing probes.
Target health states
A target is always in one of these states. You can read the state in the console or from the API.
| State | Meaning |
|---|---|
| `initial` | The target is being registered and has not yet passed enough probes. |
| `healthy` | The target passed its checks and receives traffic. |
| `unhealthy` | The target failed its checks. |
| `unused` | The target is not registered, is in a zone that is not enabled, or its group is not used by a listener. |
| `draining` | The target is being deregistered and is finishing existing requests. |
| `unavailable` | Health checking is disabled for the target. |
The describe-target-health call also returns a reason code for states other than healthy, such as Target.Timeout, Target.ResponseCodeMismatch or Target.FailedHealthChecks. Read the reason first when you troubleshoot.
import boto3
elbv2 = boto3.client("elbv2")
def print_target_health(target_group_arn):
response = elbv2.describe_target_health(TargetGroupArn=target_group_arn)
for description in response["TargetHealthDescriptions"]:
target = description["Target"]["Id"]
health = description["TargetHealth"]
reason = health.get("Reason", "-")
print(target, health["State"], reason)Configuring a health check from the CLI
The target group in the earlier example set only the path. You can set every parameter when you create the group, or change them later with modify-target-group.
aws elbv2 modify-target-group \
--target-group-arn "$TG_ARN" \
--health-check-protocol HTTP \
--health-check-path /health \
--health-check-interval-seconds 15 \
--health-check-timeout-seconds 5 \
--healthy-threshold-count 3 \
--unhealthy-threshold-count 2 \
--matcher HttpCode=200-299This probes every 15 seconds. A failing target is removed after about 30 seconds, and a recovering target returns after about 45 seconds. Shorter intervals detect failures faster but add probe traffic and make a brief hiccup more likely to remove a target.
What a good health endpoint checks
The path you choose decides what "healthy" means. There are two common choices.
- A shallow check returns
200as soon as the process can answer HTTP. It tells you the web server is running and nothing else. - A deep check also tests dependencies such as the database or a cache. It tells you the application can do real work.
Deep checks are risky. If the database has an outage, every target fails its check at once. The load balancer then has no healthy targets, and the whole site returns errors even though the web servers are fine. When all targets in a group are unhealthy, the load balancer fails open and sends requests to all of them anyway, but you should not design around that behavior.
A practical compromise is a health endpoint that checks only what the instance itself controls, such as local disk space and whether the application finished starting, and leaves dependency monitoring to CloudWatch alarms.
Health checks for Network Load Balancers
A Network Load Balancer works at layer 4, so the default health check is a TCP connection to the target port. A target is healthy if the connection succeeds. You can also configure HTTP or HTTPS checks against a path, in which case the success code rules above apply. A TCP check only proves that something is listening, so prefer an HTTP check when the target speaks HTTP.
Health checks and Auto Scaling
An Auto Scaling group has its own idea of health. By default it uses only EC2 status checks, which detect a stopped or impaired instance but not a crashed application. To make the group replace instances that fail load balancer checks, set the health check type to ELB, as in the earlier group definition.
The grace period matters here. After an instance launches, the group ignores health check results for the number of seconds you set. Without enough grace, an instance that needs three minutes to install software and start the application is reported unhealthy and terminated before it ever serves a request, and the group repeats the cycle. Set the grace period longer than your slowest normal startup.
Common errors
The most frequent causes of unhealthy targets are configuration mismatches, not broken servers.
- Security group blocks the probe. The target's security group must allow inbound traffic on the health check port from the load balancer's security group. The reason code is usually
Target.Timeout. - Wrong port. The application listens on 8080 but the check uses the traffic port 80, or the reverse.
- Wrong path or status code. The path returns
404,301or403. The reason code isTarget.ResponseCodeMismatch. Fix the path or widen the success codes. - Slow response. The endpoint takes longer than the timeout. Raise the timeout, keeping it below the interval, or make the endpoint cheaper.
- Target still starting. The application is not ready within the thresholds. Use the Auto Scaling grace period, and check that the service starts on boot.
- Check runs against the wrong host. If the application routes by
Hostheader and rejects unknown hosts, the probe, which uses the target's IP address as the host, gets an error. Add a default virtual host for the health path.
Exercise
A target group uses an interval of 20 seconds, an unhealthy threshold of 3 and a healthy threshold of 4. Roughly how long does a failing target keep receiving traffic before it is marked unhealthy, and how long after the application recovers until it is healthy again? Also name one cause if the reason code is Target.Timeout on a brand new target.
Show answer
About 60 seconds (20 × 3) before it is marked unhealthy, and about 80 seconds (20 × 4) of passing probes to become healthy again. A common cause of Target.Timeout on a new target is that the target's security group does not allow inbound traffic from the load balancer's security group on the health check port.