Menu

Graceful shutdown in Node.js: draining HTTP requests on SIGTERM without dropping traffic
📰

Graceful shutdown in Node.js: draining HTTP requests on SIGTERM without dropping traffic

DEV Community: node·Ali Ahmed·about 17 hours ago
#67M0Fif9
#devtonodejs#devto#dev#close#server
Long article (865 words) — no AI summary yet.
Reading 0:00
15s threshold

On every deploy, the platform sends your Node.js process SIGTERM and, after a grace period, SIGKILL . If the process exits immediately, requests in progress fail; if it calls server.close() and waits, idle keep-alive connections can hold it open until the kill. A graceful shutdown does four things in order: fail the readiness check and wait a few seconds so the load balancer stops sending traffic; stop accepting connections and close idle keep-alive sockets ; let in-flight requests finish ; and force-close whatever remains before the platform's deadline. Here's a tested implementation with node:http and nothing else.

Why isn't server.close() enough?

server.close() stops new connections and waits for existing ones to end. Two problems:

Keep-alive. Clients reuse connections, and on Node.js 18 an idle keep-alive socket keeps close() waiting with nothing actually happening.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.