Health Checks y Heartbeats

Liveness · Readiness

Escrito por Darjus Kazakevic · revisado el

Ask each instance two questions: is it stuck, and can it take traffic now?

A health check is a request a platform sends to an instance to decide what to do with it. Kubernetes, load balancers and service registries all use them. The answer drives an action: restart the instance, or stop sending it traffic.

The hard part is choosing what each check tests. A liveness check that calls the database restarts every instance when the database is slow, which turns a slow database into a full outage. Keep the questions separate, and match each one to its action.

En esencia — Use a shallow liveness check to restart stuck processes and a readiness check to stop traffic to instances that cannot serve right now.

Cuándo recurrir a él

Recurre a él cuando

  • Instances run behind a load balancer or a Kubernetes Service
  • An instance can be alive but not ready: warming a cache, draining
  • A cluster must notice a dead node and move its work elsewhere

Reconsidéralo cuando

  • One process started by hand, with nothing to act on the result
  • Batch jobs that run to completion: a job timeout fits better
  • Testing business features end to end: use synthetic monitoring for that

Conceptos clave

Liveness probe
Answers "is this process stuck?". After several failures in a row, Kubernetes restarts the container. It must be shallow: no database or network calls, only proof that the process can still answer.
Readiness probe
Answers "can this instance serve traffic now?". If it fails, Kubernetes removes the pod from the Service, so the load balancer stops sending it requests. The pod keeps running and comes back when the check passes.
Startup probe
Protects slow-starting apps. Until it passes, Kubernetes does not run the liveness and readiness probes. Without it, a 60-second boot meets a 30-second liveness limit and the pod restarts forever.
Shallow vs deep checks
A shallow check proves the process responds. A deep check also tests dependencies such as the database. Deep checks find more, but when every instance shares the failing dependency, all of them fail at once and nothing serves.
Heartbeats and failure detection
Instead of being asked, a node sends "I am alive" at a fixed interval, such as every second. After several missed heartbeats, the others treat it as failed. A short limit detects failure fast but marks slow nodes as dead.

Reglas de diseño

Never check dependencies in liveness
A restart fixes a stuck process, not a slow database. If liveness calls the database, a database incident restarts every pod at once, and cold starts make the incident worse.
Keep serving for a few seconds after SIGTERM
Fail readiness, keep answering for about 10 s, then stop. Load balancers and proxies take seconds to drop the pod. If you stop at once, requests hit a closed port on every deploy.
Act on several failures, not one
Mark an instance down after 2–3 failed checks in a row, and up again after one or more passes. One slow check during a garbage-collection pause should not remove or restart it.
Keep shared dependencies out of readiness
Readiness should report what is wrong with this instance: warm-up, draining, a broken local pool. If it fails whenever the shared database fails, every pod turns unready at once.

Errores habituales

  • A liveness check that calls the database. During a database incident, every pod restarts in a loop (CrashLoopBackOff), and recovery takes longer.
  • No startup probe on a slow-booting app. Liveness kills the pod before it finishes starting, again and again.
  • A health endpoint that always returns 200. The load balancer keeps sending traffic to an instance whose database pool is gone.
  • A deep check with no timeout of its own. It hangs on a slow dependency, the probe's 1 s limit fires, and a healthy pod is marked down.