Health checks
A health check is a small test the load balancer sends to each backend on a schedule, for example a request for /health. A backend that fails several checks in a row is taken out of rotation, so users don’t reach a broken server. When it passes again, it is put back automatically. You don’t have to remove or re-add anything yourself.
When to use each type of check
The first three are active checks: the load balancer sends its own test traffic. A passive check only watches real requests.
How it works
- Every Interval (default 10 seconds), the load balancer sends one check to each backend and waits up to the Timeout (default 2 seconds) for an answer.
- After Unhealthy threshold failures in a row (default
3), the backend is taken out of rotation. A failure is a wrong status code, a closed connection or no answer within the timeout. - Checks keep running against the removed backend. After Healthy threshold passes in a row (default
2), it is put back. - If no backend is healthy, requests fail until at least one backend passes its checks again.
How long it takes, with the defaults
To react faster, lower the interval: an Interval of 5,000 ms with an Unhealthy threshold of 2 takes a backend out after about 10 seconds. Shorter intervals send more check requests to your backends.
Before you begin
- A health endpoint on every backend, such as
/health, that returns HTTP200without needing a login. Return200only when the app can really serve requests (for example when it can reach its database). - The backend port open to the load balancer in each VM’s firewall group. Health checks use the same port as traffic. See Create firewall rules.
- For a backend served over TLS: the load balancer connects to backends without encryption (the backend TLS option isn’t applied yet). Use a TCP check, or serve the health endpoint over plain HTTP on the backend port.
Test the endpoint from your computer before you create the load balancer:
The first line of the answer should be HTTP/1.1 200 OK. Any other status, including a redirect such as 301, can count as a failure.
Set a health check in the portal
The portal sets the health check only when you create the load balancer.
Set or change a health check with the API
Send health_check when you create the load balancer, or change it later with PATCH. This request sets an HTTP check every 5 seconds and a passive check that takes a backend out after 5 errors in a row:
For a TCP check, use "type": "tcp" and leave out path; a TCP check with a path is refused. For an L4 load balancer, send the request to /networking/load-balancers/l4/LB-ID. If you create a load balancer through the API without health_check, it has no active check.
Passive check fields (API only):
Verify
- Stop the app on one backend (or make its
/healthreturn503). - Wait for the removal time from the table above: about 30 seconds with the defaults.
- Send requests to the load balancer. They should all reach the other backends.
- Start the app again. After about 20 seconds, that backend gets traffic again.
