How a load balancer finds out that a server is sick, and stops sending it traffic.
A health check is a small, regular test that asks a server "are you OK?". A load balancer uses the answers to stop sending traffic to a sick server and to start again when it recovers.
A load balancer shares requests between many servers. But servers fail: a process crashes, a disk fills up, a deployment goes wrong. If the balancer keeps sending requests to a dead server, a share of your users sees errors. The balancer needs a way to find out which servers are alive. That is a health check.
With active health checks the balancer itself calls each server on a timer. It can try to open a TCP connection (is anything listening?) or send an HTTP request to a path such as /health and check the status code (200 means OK). With passive health checks the balancer simply watches the real requests. If a server returns several errors or times out, it is taken out for a while.
To avoid reacting to a single blip, balancers use thresholds: for example "unhealthy after 3 failed checks" and "healthy again after 2 good checks". The interval, the timeout and the thresholds decide how fast you notice a failure, and how likely you are to flap.
| Type | How it works | Good and bad |
|---|---|---|
| Active | The balancer sends a probe on a timer | Finds failures even with no traffic. Adds a little load and can be wrong if the probe differs from real use. |
| Passive | The balancer counts real errors and timeouts | No extra load and very realistic. The first real users hit the failure, and a server with no traffic is never tested. |
| Check | What it proves | Good and bad |
|---|---|---|
| TCP connect | Something is listening on the port | Cheap and works for any protocol. A hung app can still accept connections. |
| HTTP status | The app answers a request with 200 | Proves the web layer works. Needs a /health route. |
| HTTP with content check | The answer contains the expected text | Catches wrong answers. Easy to make too strict. |
| Shallow (local) | The process is up and can serve | Fast and safe. Does not notice a broken database. |
| Deep (dependencies) | The database, cache and others also work | More complete. A shared dependency problem can mark every server unhealthy at once. |
| Probe | Question | When it fails |
|---|---|---|
| Liveness | Is the process stuck or dead? | Restart the container |
| Readiness | Can it take traffic right now? | Remove it from the balancer, but do not restart |
| Startup | Has it finished starting? | Wait before running the other probes, or restart if it never finishes |
Simplified: one health check at every multiple of the interval, and a failed check counts at that second (a real check that times out adds a little delay). The 10 requests per second is an illustrative share of the traffic. Fast checks find failures sooner but add load and make flapping more likely.
interval 5s # how often to probe timeout 2s # how long to wait for an answer unhealthy after 3 # failed probes in a row healthy after 2 # good probes in a row path /health expect status 200
| Term | Meaning in easy words |
|---|---|
| Health check | A test that tells whether a server can handle traffic |
| Active check | A probe sent by the balancer on a timer |
| Passive check | Judging health from real request errors and timeouts |
| Probe | One single health check request |
| Interval | The time between two probes |
| Timeout | How long to wait for a probe answer before counting a failure |
| Unhealthy threshold | Number of failures in a row before a server is removed |
| Healthy threshold | Number of successes in a row before a server is added back |
| Flapping | A server that keeps switching between healthy and unhealthy |
| Liveness / readiness probe | Kubernetes checks: is it alive, and is it ready for traffic |
| Connection draining | Letting in-flight requests finish before a server is removed |
| Choice | We gain | We lose |
|---|---|---|
| Short interval | Faster detection | More probe load and more chance of flapping |
| Long interval | Calm and cheap | More failed requests before a failure is noticed |
| Low unhealthy threshold (1) | Removes bad servers quickly | A single blip removes a healthy server |
| High unhealthy threshold (5) | Stable state | Slow to react to real failures |
| Shallow check | Safe and fast | Misses broken dependencies |
| Deep check | Catches more problems | A shared dependency can remove every server |