Menu

1 / 4

What server.close() Misses in Node.js Graceful Shutdown

DEV Community: express·Shubhra Pokhariya·10 days ago
#EJS8DlTe
#devtoexpress#devto#dev#node#server
Long article (4141 words) — no AI summary yet.
Reading 0:00
15s threshold

My default Node.js graceful shutdown was short. Listen for SIGTERM, call server.close() , wait for the callback, exit. It looks complete, and I'd have pasted it into a service without a second thought.

So I measured it. I built a small lab, pointed 200 requests per second at four different servers, sent SIGTERM mid-flight, and counted every request that didn't come back with a 200.

server.close() saved the requests already in flight, and in my tests that was all it reliably saved. Four other assumptions sit around that one fact (your test client, your load balancer, your keep-alive timeout and the way your container starts), and each one changed the result. Here are the five, with the numbers.

Mistake What I measured

1. Treating server.close() as the whole shutdown 0 in-flight requests lost, but new connections were refused

2. Testing with a client that reuses connections Plain node:http failed 7 requests with a pooled client and 200 with a fresh connection per request

3.…

Continue reading — create a free account

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