Health Check Endpoints
Expose liveness and readiness probes with different meanings, keep them cheap, and never let them depend on a slow dependency.
On this page
Introduction
This guide covers health check endpoints, a recurring concern in the resilience scenario. A single health endpoint that checks everything is both a liveness and a readiness probe, which confuses the orchestrator. Splitting them, keeping them cheap, and never letting them call a slow dependency means the platform can restart a dead process without waiting on a downstream check.
Liveness probe
Return 200 if the process is up and the event loop is turning, with no dependency checks, so the platform can restart a dead process quickly.
The trade-off worth naming is that return 200 if the process is up and the event loop is turning, with no dependency checks, so the platform can restart a dead process quickly adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
app.get('/healthz', (req, res) => res.status(200).json({ ok: true }));
Readiness probe
Check the deps needed to serve traffic, like the db connection, so traffic is routed only when the process can actually handle it.
The trade-off worth naming is that check the deps needed to serve traffic, like the db connection, so traffic is routed only when the process can actually handle it adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
app.get('/readyz', async (req, res) => {
try { await db.query('SELECT 1'); res.status(200).json({ ok: true }); }
catch { res.status(503).json({ ok: false }); }
});
Keep them cheap
Probes are called often, so keep them fast and free of slow deps, or the probe itself becomes a load problem.
The trade-off worth naming is that probes are called often, so keep them fast and free of slow deps, or the probe itself becomes a load problem adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Short timeouts
Give readiness checks a short timeout so a slow dependency does not hang the probe and keep traffic off longer than needed.
The trade-off worth naming is that give readiness checks a short timeout so a slow dependency does not hang the probe and keep traffic off longer than needed adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Startup probe
For slow-starting processes, add a startup probe so the platform waits for init before applying liveness, which avoids a premature restart.
The trade-off worth naming is that for slow-starting processes, add a startup probe so the platform waits for init before applying liveness, which avoids a premature restart adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Test both
Assert liveness is always 200 and readiness flips with the db, so the platform gets a correct signal.
The trade-off worth naming is that assert liveness is always 200 and readiness flips with the db, so the platform gets a correct signal adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Sources
Sources: Kubernetes probes, Health checks.
FAQ
- Liveness or readiness first?
- Liveness says the process is up; readiness says it can serve traffic. A failing readiness stops traffic but does not restart; a failing liveness restarts, so keep liveness trivial.
- Should readiness check the database?
- Yes, if you need it to serve traffic, but with a short timeout so a slow db does not make the probe hang, which would keep traffic off longer than needed.
Report a problem with this articleOpens a pre-filled issue on GitHub.