Highlights

Contents

Part VII
07

Health checks

We give the platform a way to ask each instance whether it is alive and whether it should receive traffic. These checks are tiny endpoints with outsized consequences, because the platform acts on them automatically by restarting instances and removing them from rotation. We will cover liveness, readiness and startup checks, and why a health check must not depend on everything.

CHAPTER ATLAS / FOLLOW THE READING PATHCHAPTER ATLAS / FOLLOW THE READING PATH1Logging2Metrics3Latency percentiles4Distributed tracing5Golden signals,dashboards and alerts6SLIs, SLOs and SLAs7Health checks8Performance engineering9Failure scenarios10Production debugging

#Section 23Liveness, readiness and startup

A load balancer or orchestrator cannot read Wren’s dashboards. It decides which instances get traffic, and which should be restarted, by calling a small endpoint on each one and looking at the status code. Kubernetes, Unit XV, distinguishes three questions, and most platforms ask some version of them.

Liveness asks whether the process is alive in a useful sense, or stuck in a way only a restart can fix. A deadlocked thread pool, an event loop blocked forever, or a process wedged after running out of file descriptors still holds its port and may still accept TCP connections, but will never answer a request. A liveness endpoint such as /livez returns 200 if the process can run code and answer, and the platform restarts the container if it fails several times in a row. With a check every 10 seconds and a failure threshold of 3, a stuck instance is restarted after about 30 seconds.

THREE PROBES, THREE CONSEQUENCESTHREE PROBES, THREE CONSEQUENCESpod/startupz30 × 10 s = 300 s budgetgates the others/livez3 fails × 10 s ≈ 30 srestart the container/readyz3 fails × 5 s ≈ 15 sremove from the balancer
Figure 14.23Startup gates the other two. Readiness controls traffic. Liveness controls restarts.

Readiness asks whether this instance should receive traffic right now. An instance can be alive but not ready while it is warming its cache, while it is shutting down, or while it is overloaded and wants a break. A failing readiness check, /readyz, removes the instance from the load balancer’s pool without restarting it, and a passing one adds it back. With a check every 5 seconds and a threshold of 3, an unready instance stops getting new requests within about 15 seconds. Readiness is also the key to graceful shutdown, Unit XV, where an instance fails readiness on purpose, waits for the balancer to notice, finishes its in-flight requests and then exits.

Startup asks whether the process has finished starting. Some services take a long time to boot, loading a large model, warming a JIT compiler or running a migration check. Without a startup check, a liveness check with a 30-second budget would kill a service that needs 90 seconds to start, and the instance would loop forever, restarting before it is ever ready. A startup probe runs first, with its own generous budget, such as 30 attempts 10 seconds apart, 300 seconds in all, and liveness and readiness begin only after it passes.

The endpoints themselves should be cheap and fast, answering in a few milliseconds without allocating much or taking locks, because they run every few seconds on every instance for the life of the service. They should run on the same server and thread pool as real requests, so that a blocked request path also blocks the check. And they should not need authentication, but should be reachable only from inside the cluster, since a detailed health page can reveal versions and dependencies to an attacker.

#Section 24Why health checks must not depend on everything

A well-meaning engineer makes Wren’s liveness check thorough. It pings PostgreSQL, Redis, Kafka and the payment provider, and returns 500 if any fails. One evening, PostgreSQL fails over to its replica, which takes about 20 seconds, Unit VI. During those 20 seconds every instance’s liveness check fails three times in a row, and the platform restarts all four instances at once. They come back with empty caches and empty connection pools, all open 10 connections each to the newly promoted database at the same moment, and all their requests miss the cache together. A 20-second database blip has become a full outage of several minutes, and the restarts made it worse.

The central rule for health checks is that liveness must check only the process itself. A restart fixes a stuck process. It cannot fix a database, and restarting every instance because a shared dependency is down turns one failure into two, with the restarts often arriving as a thundering herd on the recovering dependency.

A LIVENESS PROBE THAT PINGS THE DATABASEA LIVENESS PROBE THAT PINGS THE DATABASEdatabaseapi-1api-2api-3api-4sfailover 20 srestartingcold cacherestartingcold cacherestartingcold cacherestartingcold cache40 new connections + 10× misses
Figure 14.24A liveness check that pings the database restarts the whole fleet during a 20-second failover and hits the new primary with a cold herd.

Readiness needs more care, because there is a real argument for checking dependencies. If one instance has lost its own connection to the database, because its pool is broken, it is better to take that one instance out of rotation. But if the database is down for everyone, every instance fails readiness at once, the load balancer has no healthy instances, and it returns errors for every request, including ones that did not need the database, such as menu pages served from cache. A partial outage becomes a total one.

Wren’s rules come from these failures. Liveness checks only that the process can serve a request, by answering from the request path without touching dependencies. Readiness checks the instance’s own condition, startup finished, not shutting down, not overloaded beyond its concurrency limit, and its own critical local state, such as a pool that is not broken. It does not fail because a shared dependency is down. Dependency failures are handled by the request path itself, with timeouts, circuit breakers and fallbacks, Unit X, which can return cached data or a clear 503 for the affected endpoints only. Dependency health is reported on a separate, detailed status endpoint for dashboards and humans, never wired to automatic restarts.

Some balancers add a safeguard of their own. Envoy and several cloud balancers enter panic mode when too large a share of instances is unhealthy, and spread traffic across all instances anyway, on the reasoning that a mass failure of health checks is more likely a bad check than a dead fleet. That is a useful backstop, not a design.