Course contentsShow
← Node.js
Lesson 1407 of 1,98013. Error Handling and Resilient SystemsFree lesson

Implementing Health Check Endpoints

You will build liveness and readiness probes to inform orchestration layers of the current application state.

In this lesson, you will learn how to implement liveness and readiness probes to ensure your Node.js application communicates its operational health to orchestration layers like Kubernetes.

When deploying applications in distributed environments, orchestration layers need to know when to restart your process (if it’s dead) or when to stop sending traffic to it (if it's overloaded or initializing). We handle this by exposing two distinct endpoints: /healthz/live and /healthz/ready.

The Liveness Probe

A liveness probe answers the question: "Is the process still running?" If this fails, the orchestrator assumes the process is in a non-recoverable state—such as a deadlock—and will restart the container. Keep this simple; it should only check if the event loop is responsive.

app.get('/healthz/live', (req, res) => {
  res.status(200).send('OK');
});

The Readiness Probe

A readiness probe answers: "Is the application ready to handle traffic?" This is crucial during startup or when your application relies on external dependencies. If your service requires a database connection or a cache cluster to be fully warmed before serving requests, the readiness probe should return a 503 error until those conditions are met.

app.get('/healthz/ready', (req, res) => {
  if (db.isConnected && cache.isReady) {
    res.status(200).send('Ready');
  } else {
    res.status(503).send('Service Unavailable');
  }
});

Why this matters

By separating these, you prevent "flapping" scenarios where a healthy application is restarted prematurely just because it is busy initializing a connection. Using 503 Service Unavailable signals the load balancer to remove the instance from the pool temporarily, allowing it to recover without user-facing impact.

Always ensure your probes are lightweight; they should never perform complex logic that could itself cause a timeout or strain the resources you are trying to monitor. Integrate these signals with your SIGTERM handlers to ensure that during a graceful shutdown, your readiness endpoint immediately signals "not ready," allowing the orchestrator to route traffic away before your cleanup routines finish.

Key Takeaway: Liveness probes report process survival, while readiness probes report service availability, allowing orchestrators to manage traffic and restarts without human intervention.