Course contents

09. Reliability and Resilience Lesson 7 of 10

Health Checks

See how probe interval and thresholds set the detection gap, why liveness is not readiness, and how deep checks can eject every server at once.

15 minIntermediate8 steps
Loading the interactive simulation…
Loading the knowledge check…

Summary

  • A balancer routes on what its probes report, nothing more.
  • Detection takes about interval × unhealthy threshold; requests in that window fail.
  • Faster probes and lower thresholds detect sooner but cost probes and eject servers on hiccups.
  • Liveness decides restarts, readiness decides traffic; a slow but alive server passes a liveness check.
  • Checks that include shared dependencies fail on every server at once and turn degradation into an outage.
Deep diveHow it works, trade-offs, failure modes, misconceptions and the interview angle.

What it is

A health check is a small, repeated test that tells a load balancer, orchestrator or service registry whether an instance should get traffic or be restarted. Active checks send probes on a schedule (a TCP connect, an HTTP GET /healthz, a gRPC health call). Passive checks watch real traffic and count errors or timeouts per backend.

Why it exists

Instances crash, hang, run out of disk or get stuck during startup. Without a check, a balancer keeps sending a share of traffic to a dead instance forever. Health checks close that loop automatically, so that redundancy turns into availability without a human pulling the broken server out.

How it works

Probes and thresholds. Each balancer node probes each backend every interval. A probe fails if it errors or does not answer within the timeout. After unhealthy threshold consecutive failures the backend is marked unhealthy and removed from rotation; after healthy threshold consecutive successes it is put back. Thresholds above one prevent a single lost probe from ejecting a server, and stop a flapping server from bouncing in and out.

Detection time. From a crash to ejection takes up to one interval (until the next probe) plus threshold − 1 more intervals. With a 1 s interval and a threshold of 3, that is 2–3 s; with a 30 s interval and a threshold of 2 (defaults in some managed balancers are in this range), close to a minute. Requests routed to the instance during that window fail or time out.

Liveness, readiness, startup. Kubernetes separates three probes, and the split is useful anywhere:

  • Liveness: is the process wedged? Failing it gets the container restarted.
  • Readiness: should this instance receive traffic right now? Failing it removes the Pod from the Service's endpoints; nothing is restarted.
  • Startup: holds off the other two while a slow-starting app initialises.

Shallow and deep checks. A shallow check answers from the process itself. A deep check also calls dependencies (database, cache, downstream APIs). Deep checks can find more problems, but every instance shares those dependencies.

Passive checks / outlier detection. Proxies such as Envoy and NGINX can eject a backend after consecutive 5xx responses, connection failures or (in some products) high latency on real traffic, then bring it back after a cool-off. This catches failures that probes do not exercise.

When to use it

Always, for anything behind a balancer or managed by an orchestrator. Use readiness checks for warm-up (loading caches, JIT, model weights) and for graceful shutdown: fail readiness, wait for the balancer to notice, drain in-flight requests, then exit.

When not to

  • Do not use liveness checks for things a restart cannot fix (a dependency being down): restarts make it worse.
  • Do not put shared dependencies in a check that decides routing for every instance.
  • Do not rely on probes alone where gray failures (slow, partly broken instances) matter; add passive checks.

Trade-offs

Choice You gain You pay
Short interval Smaller detection gap Probe load (balancer nodes × backends × rate), more sensitivity to blips
Unhealthy threshold of 1 Fastest ejection Healthy servers ejected on one slow probe or GC pause
High healthy threshold No flapping Recovered capacity returns later
Deep check Catches broken dependency wiring per instance One shared failure ejects every instance
Passive checks Sees real errors and gray failures Needs traffic; can eject on errors caused by bad requests

Alternatives

  • Client-side health in service meshes and gRPC clients: each client tracks backend errors itself.
  • Service registry heartbeats (Consul, etcd leases): instances renew a lease; when they stop, they are deregistered.
  • Synthetic monitoring checks the user journey end to end; it alerts humans rather than routing traffic.

Failure modes

  • Detection gap: errors for interval × threshold after every crash.
  • Gray failure: an instance is alive and answers probes but serves real requests slowly or wrongly. A liveness-style check keeps it in rotation.
  • Cascading ejection: a deep check against a shared database fails on every instance at the same time; the balancer has nothing left and the outage is total, even for requests that did not need the database.
  • Restart loops: a liveness probe that fails under load restarts busy instances, which pushes their load onto the rest, which then fail their probes too.
  • Lying endpoint: /healthz returns 200 from a thread that is fine while the request threads are deadlocked.

In production

Keep liveness trivial (the process can answer). Make readiness about the instance's own ability to serve: started, warmed up, not draining, local resources fine. Leave shared dependencies out of both, and handle them in the request path with timeouts, circuit breakers and degraded responses. Know your balancer's last-resort behaviour: AWS Application and Network Load Balancers route to all targets when every target is unhealthy (fail open), and Envoy ignores health status when the healthy share drops below its panic threshold (50% by default). Add outlier detection with a cap on how many hosts can be ejected at once.

Common misconceptions

  • "200 from /healthz means the instance is fine." It means the health handler ran. Slow disks, exhausted pools or deadlocked worker threads may not show up.
  • "A more thorough check is always better." Including shared dependencies couples every instance's fate to them.
  • "Faster probes fix gray failures." A probe that already succeeds succeeds more often.
  • "Liveness and readiness are the same probe with different names." One restarts, the other routes; using one for the other causes restart storms or keeps broken instances in service.

Interview angle

When you place a load balancer, say how it learns about failures: probe type, interval, thresholds and the resulting detection time. Distinguish liveness and readiness, explain how draining uses readiness during deploys, and point out the deep-check trap with a shared database. Mentioning passive checks or outlier detection for gray failures, and fail-open behaviour when everything looks unhealthy, shows production experience.

Model assumptions

This lesson runs the simulation engine for traffic, crashes and ejection. Arrivals are Poisson; each app server has 8 workers, a 40 ms mean service time (lognormal, cv 0.35) and room for 16 waiting requests; reads go through a shared cache with a fixed 80% hit ratio and to a 20-connection database on a miss. The balancer probes every 1 s (3 failures eject, 2 successes restore), later every 250 ms (2 failures eject). Probe packets are drawn slower than the modelled 5 ms hop so they are visible. The engine's probes only see whether a node is up or down: a degraded (slow) node passes them, like a liveness check. Readiness is shown by removing the server from the balancer's backend list, and a failing deep check by every app server reporting down to the balancer; neither is a separate probe type in the model. Numbers are illustrative, not benchmarks.

The lesson as textEvery step of the simulation, written out.
  1. 1. The balancer keeps asking “are you there?”

    Users send 180 requests per second through a load balancer to three app servers. Each server reads through a shared cache and falls back to the database on a miss.

    Once a second the balancer sends every server a small health probe (here GET /healthz) and expects a quick 200. The probes are drawn slower than they really travel so you can follow them. A server stays in rotation while it keeps answering. Nothing is wrong yet: every server is roughly a third busy and every request succeeds.

    Scene: Users reach three app servers through a load balancer. Every second the balancer probes each server and gets a 200 back. All requests succeed.

    Key idea: A load balancer only knows what its probes tell it: interval, timeout and thresholds decide how quickly it notices anything.

  2. 2. A crash, and two and a half seconds of errors

    At 1.5 s App 3 crashes. The balancer does not know: its next probe is due at 2 s, and it wants three failed probes in a row before it gives up on a server. App 3 is taken out at the 4 s probe.

    For those 2.5 seconds a third of all requests are still sent to App 3 and fail (red). Worst case, detection takes up to one interval for the first failed probe plus two more intervals: about 3 s with these settings. Real balancers often default to 10–30 s intervals, so the gap can be a minute.

    Scene: App 3 crashes at 1.5 seconds. Requests routed to it fail until the third failed probe at 4 seconds takes it out of rotation; then errors stop.

    Key idea: Detection time ≈ interval × unhealthy threshold (plus up to one interval of bad luck). Every request in that window is a candidate error.

  3. 3. Probe faster, eject sooner

    App 3 is back. We probe every 250 ms and eject after 2 failures, then crash App 3 again at 1 s. It is out of rotation within about half a second and far fewer requests fail.

    This is not free. Probes cost requests on every server from every balancer node (a fleet of 20 balancer nodes probing 100 servers four times a second is 8,000 probes per second). And a low threshold ejects servers on a hiccup, such as a garbage-collection pause or one slow probe, moving their traffic onto the others. Pair fast probes with a probe timeout well above normal latency and a healthy threshold of 2–3 before readmitting.

    Scene: With probes every 250 ms and a threshold of two, App 3 crashes at 1 second and is ejected about half a second later. Far fewer requests fail than with the slower settings.

    Key idea: Shorter intervals and lower thresholds shrink the error window but raise probe cost and the risk of ejecting servers that were only briefly slow.

  4. 4. Slow but alive: the probe says 200

    App 3 is back. Now App 2 turns slow: its disk is failing and every request takes four times as long. The process is alive and /healthz still answers 200, so the balancer keeps sending App 2 a third of the traffic.

    App 2 can only finish about 45 requests per second of its 60. Its queue fills, it rejects the rest with 503, and P95 latency jumps for everyone. The balancer never ejects it. This is a gray failure: a liveness-style check (is the process up?) cannot see it.

    Scene: App 2 becomes four times slower but keeps passing its health probes. The balancer keeps sending it a third of the traffic; its queue fills, some requests are rejected and P95 latency rises.

    Key idea: A check that only asks “are you alive?” keeps slow and broken-but-running servers in rotation.

  5. 5. Readiness: alive, but do not send me traffic

    Separate the two questions. Liveness: is the process stuck, should it be restarted? Readiness: should this instance receive traffic right now? A readiness check fails when the instance is starting up, draining for shutdown, or cannot do its own job (its local disk is failing, its queue is full).

    App 2’s readiness check now fails, and the platform takes it out of the endpoint list (in Kubernetes: removed from the Service’s endpoints) while the process keeps running. Here that is shown by removing its link from the balancer. The other two servers carry 90 requests per second each, about half busy, and P95 drops back.

    Scene: App 2 is still running but no longer receives traffic. App 1 and App 3 serve all requests, about half busy each, and latency is back to normal.

    Key idea: Liveness decides restarts; readiness decides traffic. Mixing them up either restarts healthy processes or keeps sick ones in rotation.

  6. 6. The database fails: shallow checks keep serving

    App 2 is healthy and back in rotation. At 1 s the database goes down. The app servers’ health checks only test the server itself, so all three stay in rotation.

    Requests that hit the cache (about 80%) still succeed. Only cache misses fail, because they need the database. The service is degraded, not down.

    Scene: The database goes down. All three app servers stay in rotation; requests answered from the cache succeed, cache misses fail. About a fifth of requests fail.

    Key idea: A check that tests only the instance keeps it in rotation when a shared dependency fails, so partial service survives.

  7. 7. Deep checks eject every server at once

    Same failure, but now /healthz also queries the database (a deep check). When the database dies at 1 s, every app server’s check fails at the same moment. The balancer ejects all three within half a second and answers every request itself with “no healthy backend”.

    The error rate goes from about 20% to 100%, including the cached reads that would have worked. One shared dependency turned into a full outage. (The model’s probes only see up or down, so a failing deep check is shown as each server reporting DOWN to the balancer.)

    Some balancers guard against this: AWS load balancers fail open and route to all targets when every target is unhealthy, and Envoy ignores health status below a panic threshold (50% healthy by default).

    Scene: With deep health checks, the database failure makes every app server report unhealthy. The balancer ejects all three and every request fails, including ones the cache could have answered.

    Key idea: Don’t put shared dependencies in the check that decides routing: when they fail, every instance fails the check together.

  8. 8. What to check, and what not to

    Everything is healthy again, with probes every 250 ms.

    A practical split: a liveness check that only fails if the process is wedged (restart it); a readiness check for what the instance itself needs to serve: started, warmed up, not draining, local resources fine. Leave shared dependencies out of both. Handle a failing database inside the app with timeouts, circuit breakers and degraded responses, and alert on it separately.

    Add passive checks (outlier detection: eject a server whose real requests fail or time out) to catch gray failures that probes miss, and cap how many servers can be ejected at once.

    Scene: All three app servers, the cache and the database are healthy. The balancer probes every 250 ms and every request succeeds.

    Key idea: Health checks are routing decisions. Make them fast, local and honest; deal with dependencies elsewhere.