How-To Guide

How to Implement Health and Readiness Probes

Split liveness from readiness: fail liveness only when a restart helps, fail readiness when the instance cannot serve, and test both.

On this page

  1. Introduction
  2. Define liveness narrowly
  3. Define readiness for the traffic
  4. Keep the probes cheap and side-effect free
  5. Do not let readiness flap on a transient dependency
  6. Test the transitions and the independence
  7. Common pitfalls
  8. Conclusion

Introduction

A health check tells an orchestrator whether an instance is alive and whether it is ready for traffic, and the two questions have different answers. Liveness asks is the process stuck; a failure restarts it. Readiness asks can this instance serve requests now; a failure removes it from the load balancer. Conflating them is a common and damaging mistake: a dependency outage that fails liveness restarts every instance in a loop.

This tutorial covers defining liveness narrowly, defining readiness for traffic, keeping both cheap and side-effect free, and testing the transitions. The examples use the standard probe endpoints, and the split applies to any orchestrator or load balancer.

Define liveness narrowly

Liveness should fail only when a restart would help: the process is deadlocked, out of memory, or otherwise stuck in a way a fresh process resolves. It should not fail because a downstream is down, because a restart does not fix the downstream and it restarts every instance at once. The liveness probe is a restart trigger, so it must be rare and correct.

Keep the liveness check local to the process: can it respond at all, is its event loop not blocked indefinitely. Do not check external dependencies in liveness, because an external outage is not fixed by a restart and failing liveness turns the dependency outage into a restart loop. The liveness check answers only whether this process is functional.

// liveness: the process can respond; no dependency check
app.get('/healthz', (req, res) => {
  // if the event loop can serve this, the process is alive
  res.status(200).json({ status: 'alive' });
});

Define readiness for the traffic

Readiness should fail when the instance cannot serve requests: during startup before the app is initialized, during a shutdown drain, or when a dependency it needs is unavailable. A readiness failure removes the instance from the load balancer, so traffic goes to the ready instances. This is the correct use for a dependency check: a not-ready instance is not sent traffic.

Include the startup check so the instance is not ready until its initialization is complete, such as its caches warmed and its connections open. Include the drain so that on shutdown the instance fails readiness and the load balancer stops sending it new traffic before it stops accepting. The readiness signal is the switch that controls whether the instance receives traffic.

let ready = false;
let draining = false;

app.get('/readyz', async (req, res) => {
  if (!ready || draining) return res.status(503).json({ status: 'not_ready' });
  // a fast check of a critical dependency, with a short timeout
  const dbOk = await withTimeout(() => db.ping(), 200);
  res.status(dbOk ? 200 : 503).json({ status: dbOk ? 'ready' : 'db_unavailable' });
});

// on shutdown: draining = true, so the load balancer drains this instance first

Keep the probes cheap and side-effect free

A health probe runs often, so it must be fast and free of side effects. A probe that does heavy work, such as a full query or a write, adds load on every interval and can become a failure itself. A probe that mutates state or holds a lock is worse, because it changes the system it checks. The probe should read a ready flag or run a trivial check.

Give the dependency check inside readiness a short timeout, so a slow dependency fails the check quickly rather than hanging the probe. A probe that hangs is worse than one that fails, because the orchestrator waits for the timeout. Keep the check to a fast ping or a cached status, and let the real dependency handling be in the request path, not the probe.

Do not let readiness flap on a transient dependency

A readiness check that fails on a brief dependency slowdown removes every instance at once, which can turn a blip into an outage. If readiness depends on a dependency, make it tolerant: a few consecutive failures before failing, or a cached status, so a single slow response does not remove the fleet. The trade is that a genuinely down dependency may keep some traffic going, which is usually better than removing all instances.

Where the dependency is not critical for all endpoints, keep the instance ready and let the affected endpoints fail, rather than removing the whole instance. The readiness is for the instance’s ability to serve its traffic in general, not for every dependency of every endpoint. A too-strict readiness check that mirrors every dependency makes the fleet fragile.

Test the transitions and the independence

Test that readiness fails during startup and passes once initialized, so the orchestrator does not send traffic early. Test that readiness fails during a drain, so the instance is removed from the load balancer before it stops. Test that liveness stays healthy when a dependency is down, so a dependency outage does not restart the fleet. Test that the probes are cheap, so the interval does not add meaningful load.

# during startup: readyz fails, healthz passes
curl -s -o /dev/null -w '%{http_code}\n' http://localhost:3000/readyz   # 503
curl -s -o /dev/null -w '%{http_code}\n' http://localhost:3000/healthz  # 200
# with the database down: readyz fails, healthz still passes (no restart loop)
curl -s -o /dev/null -w '%{http_code}\n' http://localhost:3000/healthz  # 200

Common pitfalls

One probe for both so a dependency outage restarts the fleet, a liveness check that depends on a downstream, a readiness check with no startup or drain state so traffic arrives early or lingers, a probe that does heavy work or mutates state, and a readiness check that flaps on a transient dependency so all instances drop at once are the recurring mistakes. A narrow liveness, a readiness with startup and drain, cheap side-effect-free probes, and a tolerant dependency check address them.

Conclusion

Liveness and readiness answer different questions and trigger different actions. Fail liveness only when a restart helps, fail readiness when the instance cannot serve or is draining, keep both cheap and side-effect free, and make a dependency-dependent readiness tolerant. Self-check by asserting readiness fails during startup and drain while liveness stays healthy when a dependency is down. Then a restart happens only when it helps and traffic goes only to ready instances.

Sources: Kubernetes: Liveness, readiness, and startup probes, Google Cloud: Health checks, AWS: ELB health checks.

FAQ

Why not have one probe for both?
Liveness and readiness trigger different actions: a liveness failure restarts the process, a readiness failure removes it from the load balancer. If a dependency outage fails liveness, every instance restarts in a loop, which makes the outage worse. Split them so a restart happens only when it helps.
Should readiness check the database?
Carefully. A readiness check that fails when a database is briefly slow removes all instances at once, which can cascade. Check that the instance can serve, including a fast check of a critical dependency, but avoid a slow or flapping dependency failing readiness for every instance.

Related Examples