The pooled vs fresh-connection result has a practical side that's easy to miss: in most production setups the client hitting your Node process isn't the browser, it's nginx, an ingress controller or a cloud load balancer, and those usually keep upstream connections alive. So the pooled-client case (7 to 12 failures) is often closer to what you'll see in production, but only because the proxy is quietly reusing sockets, which also means the Connection: close while draining matters even more there, otherwise the proxy keeps pushing requests down sockets the app wants to retire. On Kubernetes, another option for the pause is a preStop hook (newer versions have a native sleep action), so the delay lives in the deployment config instead of the app and every service gets the same value. Your module approach is nicer when you also want the /health 503, though. The "fast docker stop doesn't prove your handler ran" point deserves its own checklist item: grepping for the handler's log line after a staging rollout is a cheap way to catch an npm start or sh -c launcher before it bites in production.
Full-stack developer and technical writer creating hands-on tutorials, deep dives, and product guides for developer tools. I research, build, and test before I write.
Location
India
Education
Self-taught developer through courses, books, and hands-on practice.
Pronouns
Lover of clean UI, simple design, and coding workflows that save time for everyone.
Work
Technical writing for developers, founders, and tech businesses. https://shubhra.dev/work-with-me
Fair point. That's why I reported both numbers rather than treating the pooled result as representative of every setup. I didn't test through nginx, an ingress or a cloud load balancer, so I can't say which connection pattern matches a particular proxy configuration. If it reuses upstream connections, the pooled-client case is the one worth looking at. I did test Connection: close while draining with a pooled client, but not through a real proxy, so that part still needs testing.
Using a preStop sleep is another way to handle the delay in the deployment config. As I covered in the audit, it uses part of the same termination grace period, so the remaining time still needs to cover the app's shutdown and cleanup. I haven't tested the hook itself.
And yes, checking for the handler's log line after a staging rollout is a simple way to verify that SIGTERM actually reached the Node process. A fast Docker stop alone doesn't tell you that.
Hey, I'm Hema 👋
Developer, writer, and creator of Dev Opportunity Radar, a weekly series published every Friday on DEV, helping people discover opportunities they might otherwise miss.
Oh, this was interesting to read, di 😀 I honestly wouldn't have thought about how many things could affect graceful shutdown beyond just calling server.close(). Loved that you actually tested all these scenarios and shared what you found!
Full-stack developer and technical writer creating hands-on tutorials, deep dives, and product guides for developer tools. I research, build, and test before I write.
Location
India
Education
Self-taught developer through courses, books, and hands-on practice.
Pronouns
Lover of clean UI, simple design, and coding workflows that save time for everyone.
Work
Technical writing for developers, founders, and tech businesses. https://shubhra.dev/work-with-me
Thank you so much, Hema! ❤️ I'm glad you enjoyed it. It was interesting to see how much the results changed with different client connection behaviour, keep-alive settings and the way Node was started. Testing these scenarios made me look at graceful shutdown quite differently.
Thank you for measuring this instead of repeating the snippet. The one that would have caught me is SIGTERM not reaching Node through sh -c or npm start, which is the sort of thing you only find out during an incident and it explains a lot of shutdown handlers that never fire. Splitting what server.close does protect, the requests already in flight, from what it does not, the ones that need a fresh connection, is the clear way to say it. The pooled against fresh-connection contrast is a good reminder that the test client can change the answer too. Did you ever put this behind a real load balancer, or was the model of balancer lag close enough to trust?
Full-stack developer and technical writer creating hands-on tutorials, deep dives, and product guides for developer tools. I research, build, and test before I write.
Location
India
Education
Self-taught developer through courses, books, and hands-on practice.
Pronouns
Lover of clean UI, simple design, and coding workflows that save time for everyone.
Work
Technical writing for developers, founders, and tech businesses. https://shubhra.dev/work-with-me
Thanks, Debashish. No, I haven't put it behind a real load balancer. The lab models the lag: the client keeps sending requests for 0, 1000 or 2500 ms after SIGTERM, as if the instance were still in the balancer's list.
The model did what the arithmetic predicted. With a 1500 ms pause and 2500 ms of simulated lag, the 1000 ms gap at 200 req/s predicts about 200 failures. node:http with a fresh connection per request averaged 199.7 failures across repeated runs, and the other three server implementations landed within a request or two. So I trust the relationship between the pause and the lag, but not those numbers as a prediction for a real balancer. How quickly one stops sending traffic depends on its implementation, so that delay needs to be measured in staging.
If you try it on a real cluster, I'd like to see what you get.
Mistake 5 is the one I'd have missed. Measuring it with numbers instead of guessing makes this so useful.
I learned the reverse side the hard way: my containers start from a bash entrypoint that traps SIGTERM and passes it on, and once the app process was killed for running out of memory, the shell stayed up as PID 1. Docker kept saying "Up" while every request got a 502. Did your lab look in that direction too, the app dying while whatever is PID 1 stays alive?
Full-stack developer and technical writer creating hands-on tutorials, deep dives, and product guides for developer tools. I research, build, and test before I write.
Location
India
Education
Self-taught developer through courses, books, and hands-on practice.
Pronouns
Lover of clean UI, simple design, and coding workflows that save time for everyone.
Work
Technical writing for developers, founders, and tech businesses. https://shubhra.dev/work-with-me
Thanks, Milo. My lab didn't test that direction. Mistake 5 covers whether SIGTERM reaches Node and what docker stop does: with sh -c it took 10.3 s and exited 137, with npm start it stopped in 0.8 s with exit 1, and my handler ran in neither. I never tested an OOM kill or the app dying while its launcher stays alive.
Docker keeps a container up as long as PID 1 is running, so a shell that outlives the killed app would explain the "Up" with 502s. If the script only waits on the app, it should exit with it, so I'd look at what else keeps the shell running.
Full-stack developer and technical writer creating hands-on tutorials, deep dives, and product guides for developer tools. I research, build, and test before I write.
Location
India
Education
Self-taught developer through courses, books, and hands-on practice.
Pronouns
Lover of clean UI, simple design, and coding workflows that save time for everyone.
Work
Technical writing for developers, founders, and tech businesses. https://shubhra.dev/work-with-me
One result held up across all four servers and both Node versions I tested. With a pooled client, 7 to 12 requests failed. With a fresh connection per request, it was around 200.
What I couldn't measure is how long traffic keeps reaching a pod after SIGTERM in a real cluster. I tested 0, 1000 and 2500 ms of lag. If you've measured that, or tested a pooled client against a fresh connection per request, I'd be interested in your numbers. The repo is public if you want to rerun the tests, and an issue with your results is welcome.
Full-stack developer and technical writer creating hands-on tutorials, deep dives, and product guides for developer tools. I research, build, and test before I write.
Location
India
Education
Self-taught developer through courses, books, and hands-on practice.
Pronouns
Lover of clean UI, simple design, and coding workflows that save time for everyone.
Work
Technical writing for developers, founders, and tech businesses. https://shubhra.dev/work-with-me
Same here. I expected the two clients to differ a little, not 7 failures against about 200 on the same code. It comes down to the sockets. Under steady load, those pooled connections are rarely idle, so server.close() leaves the busy ones open and the pooled client keeps getting responses. A fresh connection, on the other hand, has to establish a new TCP connection, which fails once the server stops accepting new connections. Worth testing both client patterns in your own staging environment.
We're a place where coders share, stay up-to-date and grow their careers.
The pooled vs fresh-connection result has a practical side that's easy to miss: in most production setups the client hitting your Node process isn't the browser, it's nginx, an ingress controller or a cloud load balancer, and those usually keep upstream connections alive. So the pooled-client case (7 to 12 failures) is often closer to what you'll see in production, but only because the proxy is quietly reusing sockets, which also means the
Connection: closewhile draining matters even more there, otherwise the proxy keeps pushing requests down sockets the app wants to retire. On Kubernetes, another option for the pause is apreStophook (newer versions have a native sleep action), so the delay lives in the deployment config instead of the app and every service gets the same value. Your module approach is nicer when you also want the /health 503, though. The "fast docker stop doesn't prove your handler ran" point deserves its own checklist item: grepping for the handler's log line after a staging rollout is a cheap way to catch an npm start or sh -c launcher before it bites in production.Fair point. That's why I reported both numbers rather than treating the pooled result as representative of every setup. I didn't test through nginx, an ingress or a cloud load balancer, so I can't say which connection pattern matches a particular proxy configuration. If it reuses upstream connections, the pooled-client case is the one worth looking at. I did test
Connection: closewhile draining with a pooled client, but not through a real proxy, so that part still needs testing.Using a
preStopsleep is another way to handle the delay in the deployment config. As I covered in the audit, it uses part of the same termination grace period, so the remaining time still needs to cover the app's shutdown and cleanup. I haven't tested the hook itself.And yes, checking for the handler's log line after a staging rollout is a simple way to verify that SIGTERM actually reached the Node process. A fast Docker stop alone doesn't tell you that.
Thanks for bringing up the proxy side of this.
Oh, this was interesting to read, di 😀 I honestly wouldn't have thought about how many things could affect graceful shutdown beyond just calling
server.close(). Loved that you actually tested all these scenarios and shared what you found!Thank you so much, Hema! ❤️ I'm glad you enjoyed it. It was interesting to see how much the results changed with different client connection behaviour, keep-alive settings and the way Node was started. Testing these scenarios made me look at graceful shutdown quite differently.
Thank you for measuring this instead of repeating the snippet. The one that would have caught me is SIGTERM not reaching Node through sh -c or npm start, which is the sort of thing you only find out during an incident and it explains a lot of shutdown handlers that never fire. Splitting what server.close does protect, the requests already in flight, from what it does not, the ones that need a fresh connection, is the clear way to say it. The pooled against fresh-connection contrast is a good reminder that the test client can change the answer too. Did you ever put this behind a real load balancer, or was the model of balancer lag close enough to trust?
Thanks, Debashish. No, I haven't put it behind a real load balancer. The lab models the lag: the client keeps sending requests for 0, 1000 or 2500 ms after SIGTERM, as if the instance were still in the balancer's list.
The model did what the arithmetic predicted. With a 1500 ms pause and 2500 ms of simulated lag, the 1000 ms gap at 200 req/s predicts about 200 failures.
node:httpwith a fresh connection per request averaged 199.7 failures across repeated runs, and the other three server implementations landed within a request or two. So I trust the relationship between the pause and the lag, but not those numbers as a prediction for a real balancer. How quickly one stops sending traffic depends on its implementation, so that delay needs to be measured in staging.If you try it on a real cluster, I'd like to see what you get.
Mistake 5 is the one I'd have missed. Measuring it with numbers instead of guessing makes this so useful.
I learned the reverse side the hard way: my containers start from a bash entrypoint that traps SIGTERM and passes it on, and once the app process was killed for running out of memory, the shell stayed up as PID 1. Docker kept saying "Up" while every request got a 502. Did your lab look in that direction too, the app dying while whatever is PID 1 stays alive?
Thanks, Milo. My lab didn't test that direction. Mistake 5 covers whether SIGTERM reaches Node and what docker stop does: with sh -c it took 10.3 s and exited 137, with npm start it stopped in 0.8 s with exit 1, and my handler ran in neither. I never tested an OOM kill or the app dying while its launcher stays alive.
Docker keeps a container up as long as PID 1 is running, so a shell that outlives the killed app would explain the "Up" with 502s. If the script only waits on the app, it should exit with it, so I'd look at what else keeps the shell running.
One result held up across all four servers and both Node versions I tested. With a pooled client, 7 to 12 requests failed. With a fresh connection per request, it was around 200.
What I couldn't measure is how long traffic keeps reaching a pod after SIGTERM in a real cluster. I tested 0, 1000 and 2500 ms of lag. If you've measured that, or tested a pooled client against a fresh connection per request, I'd be interested in your numbers. The repo is public if you want to rerun the tests, and an issue with your results is welcome.
I think the pooled versus fresh client result is the part that surprised me most.
Same here. I expected the two clients to differ a little, not 7 failures against about 200 on the same code. It comes down to the sockets. Under steady load, those pooled connections are rarely idle, so server.close() leaves the busy ones open and the pooled client keeps getting responses. A fresh connection, on the other hand, has to establish a new TCP connection, which fails once the server stops accepting new connections. Worth testing both client patterns in your own staging environment.