Stay up to date with AI tools, model releases, and developer workflows that matter.

Weekly. Free. One click to leave.

7 Day Free Trial. Cancel Anytime.

Most APIs have a health endpoint, and most of them lie. The classic version is a route that returns 200 OK with {"status":"ok"} no matter what. It proves that the Node.js process is running and that the router works. It indicates nothing about whether the API can actually serve a request, because the database, the cache and the third-party services the API depends on are never touched.

According to recent reports, that kind of endpoint is worse than having none. The load balancer keeps sending traffic to a broken instance, the orchestrator never restarts it, and the dashboard stays green while consumers get errors. This guide shows how to build health checks that report what is really happening, how to keep them cheap and safe, and how to watch them from outside your own infrastructure.

In a recent development, A single "health" route usually tries to answer three different questions at once. It works better to separate them.

Kubernetes formalizes the first two as liveness and readiness probes, and its documentation gives the same warning about liveness checks that depend on other services. The distinction is just as useful behind a plain load balancer or on a single VPS.

The example below uses Express 5 and PostgreSQL through node-postgres, plus one HTTP dependency: a recommendations service the API can live without. Install the two packages in an ES module project:.

The pool.on("error") line is easy to forget and it matters here more than anywhere. When the database restarts, idle connections in the pool emit an error event. With no listener, Node.js treats it as unhandled and the process exits, so the outage your health check was supposed to report takes the API down with it. The node-postgres pool documentation calls this out for the same reason.

In a recent development, without a timeout, a hung database connection makes the health endpoint hang too, and the caller sees a timeout instead of a clear answer. The clearTimeout() in finally matters on a route that gets called several times a second: without it, every successful check leaves a timer running in the background.

For HTTP dependencies, fetch() can enforce its own limit with AbortSignal.timeout(), which also cancels the request instead of just ignoring its result.

SELECT 1 is the lightest possible round trip: it proves the connection works without touching any table. The critical flag decides what a failure means. The API can't serve requests without its database, but it can still serve most of them without recommendations. Error messages go to the log, not into the response.

The checks run in parallel with Promise.all, so the endpoint takes as long as the slowest dependency rather than the sum of all of them. Caching the promise, not the finished result, has a useful side effect: if ten load balancer probes arrive while a check is still running, all ten wait for the same check instead of starting ten more.

The route can answer in three ways:.

Here is what the route returned while the database was stopped and started again during testing:.

Throughout the outage, /healthz kept answering 200, which is exactly what you want: nothing restarted, the instance just left the load balancer pool until the database came back.

A health endpoint is called constantly by load balancers, orchestrators and monitors, often several times a second across a fleet. That changes how you should write it.

According to recent reports, everything above runs inside your infrastructure. The load balancer calls the instance over a private network, and the orchestrator calls it on localhost. That means a whole class of failures stays invisible to them:.

Customers see all of these as "the API is down", while every internal probe reports green. The only way to catch them is to call the endpoint the way a client does: over the public internet, through DNS, TLS and every proxy in between.

An external monitor should do more than check for a 200. Because the readiness route returns structured JSON, the monitor can assert on the value that matters instead of the status code alone. Most uptime tools that support API checks let you do this. With an API uptime monitor, for example, you can send the same headers a real client would, including an Authorization header if the route is protected, and require that the JSON path checks.database equals ok. If the value changes, or the body stops being valid JSON, the check fails even when the status code still looks fine.

In a recent development, this is also where the degraded state pays off. An internal load balancer ignores it, because the instance still answers 200. An external monitor that asserts on checks.recommendations can still warn you that an optional service has been failing for an hour.

A few settings are worth getting right whichever tool you use:.

Recent findings suggest that when you're debugging and just want a one-off answer, it also helps to check the URL from outside your own network before assuming the problem is in your code. If the public request fails while a request from inside the cluster succeeds, you can skip straight to DNS, TLS and the proxy layer.

According to recent reports, A health endpoint is a promise to everything that calls it. Make it a promise you keep, and the next outage will announce itself in seconds instead of in a support ticket.

7 Day Free Trial. Cancel Anytime.

πŸ“Œ Read also:

β†’ Read original article