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

Load Balancer Integration

You will configure load balancers to detect when a node is in a 'shutdown' state and stop routing traffic to it.

In this lesson, you will learn how to configure your Node.js application to signal a load balancer when it is entering a 'shutdown' state so that incoming traffic is rerouted before the process terminates.

The Problem: The "Dangling Request"

When you trigger a server restart or deployment, your Node.js process often receives a SIGTERM signal. If your load balancer isn't aware that you’re shutting down, it will continue to route active user requests to your process. These requests then fail midway, or the server hangs while waiting for them to finish, leading to a poor user experience and elevated error rates.

The Solution: Readiness and Shutdown States

The most robust way to handle this is by implementing a "graceful shutdown" sequence paired with a health-check endpoint that your load balancer monitors.

  1. Mark as 'Not Ready': When your process receives a SIGTERM, it should immediately flag itself as "not ready" in your /health/ready endpoint.
  2. Load Balancer Detection: Your load balancer (like Nginx, HAProxy, or a cloud-native ingress) polls this endpoint. Once it sees a 503 Service Unavailable or a specific "down" status, it removes that node from the active rotation.
  3. Graceful Exit: Only after the load balancer has stopped sending new traffic do you allow the Node.js process to finish existing connections and close the server.

Example Implementation

In your Express app, you can track the shutdown state:

let isShuttingDown = false;

app.get('/health/ready', (req, res) => {
  if (isShuttingDown) {
    return res.status(503).send('Shutting down');
  }
  res.status(200).send('OK');
});

process.on('SIGTERM', () => {
  isShuttingDown = true;
  // Wait a few seconds for the LB to receive the update
  setTimeout(() => {
    server.close(() => process.exit(0));
  }, 5000); 
});

By introducing a short delay (the setTimeout), you give the load balancer enough time to propagate the "not ready" status across its fleet. This simple handoff ensures that no request is sent to a process that is no longer prepared to handle it, maintaining high availability even during maintenance.

Key Takeaway: Signal your load balancer via a readiness probe that you are shutting down, and wait for that update to propagate before closing your active connections.