Graceful Shutdown in Node.js: Stop Dropping Requests (2026)

Graceful Shutdown in Node.js: Stop Dropping Requests

Your server gets a SIGTERM. It dies immediately. In-flight requests get 502s. Here is how to shut down properly.

The Basic Pattern

import http from “http”;
let isShuttingDow…


This content originally appeared on DEV Community and was authored by Young Gao

Graceful Shutdown in Node.js: Stop Dropping Requests

Your server gets a SIGTERM. It dies immediately. In-flight requests get 502s. Here is how to shut down properly.

The Basic Pattern

import http from "http";
let isShuttingDown = false;
const server = http.createServer(app);

async function gracefulShutdown(signal: string) {
  isShuttingDown = true;
  server.close();
  const timeout = setTimeout(() => process.exit(1), 30000);
  await Promise.all([closeDatabase(), closeRedis(), flushLogs()]);
  clearTimeout(timeout);
  process.exit(0);
}

process.on("SIGTERM", () => gracefulShutdown("SIGTERM"));
process.on("SIGINT", () => gracefulShutdown("SIGINT"));

Health Check During Shutdown

app.get("/health", (req, res) => {
  if (isShuttingDown) return res.status(503).json({ status: "shutting_down" });
  res.json({ status: "healthy" });
});

Returning 503 tells the load balancer to stop sending new traffic.

What to Clean Up

  1. Stop accepting new requests
  2. Finish in-flight requests (with timeout)
  3. Close database connection pools
  4. Disconnect from Redis and message brokers
  5. Flush log buffers
  6. Deregister from service discovery

Part of my Production Backend Patterns series. Follow for more practical backend engineering.


This content originally appeared on DEV Community and was authored by Young Gao


Print Share Comment Cite Upload Translate Updates
APA

Young Gao | Sciencx (2026-03-21T09:57:42+00:00) Graceful Shutdown in Node.js: Stop Dropping Requests (2026). Retrieved from https://www.scien.cx/2026/03/21/graceful-shutdown-in-node-js-stop-dropping-requests-2026/

MLA
" » Graceful Shutdown in Node.js: Stop Dropping Requests (2026)." Young Gao | Sciencx - Saturday March 21, 2026, https://www.scien.cx/2026/03/21/graceful-shutdown-in-node-js-stop-dropping-requests-2026/
HARVARD
Young Gao | Sciencx Saturday March 21, 2026 » Graceful Shutdown in Node.js: Stop Dropping Requests (2026)., viewed ,<https://www.scien.cx/2026/03/21/graceful-shutdown-in-node-js-stop-dropping-requests-2026/>
VANCOUVER
Young Gao | Sciencx - » Graceful Shutdown in Node.js: Stop Dropping Requests (2026). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2026/03/21/graceful-shutdown-in-node-js-stop-dropping-requests-2026/
CHICAGO
" » Graceful Shutdown in Node.js: Stop Dropping Requests (2026)." Young Gao | Sciencx - Accessed . https://www.scien.cx/2026/03/21/graceful-shutdown-in-node-js-stop-dropping-requests-2026/
IEEE
" » Graceful Shutdown in Node.js: Stop Dropping Requests (2026)." Young Gao | Sciencx [Online]. Available: https://www.scien.cx/2026/03/21/graceful-shutdown-in-node-js-stop-dropping-requests-2026/. [Accessed: ]
rf:citation
» Graceful Shutdown in Node.js: Stop Dropping Requests (2026) | Young Gao | Sciencx | https://www.scien.cx/2026/03/21/graceful-shutdown-in-node-js-stop-dropping-requests-2026/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.