r/devops • u/UkrMalt • Aug 20 '26
Ops / Incidents When should a small Node service stop accepting work during shutdown?
For a small service with HTTP requests, background jobs, and maybe WebSockets, I’m trying to keep shutdown behavior simple.
My current order is to stop accepting new work, let active requests finish, flush bounded notifications, then close the queue and database connections. I’m less sure how long to wait before forcing the process to exit.
What shutdown steps have actually mattered in production, and which ones turned out to be unnecessary complexity?
5
Upvotes
3
u/ben_bliksem Aug 20 '26
Our entire user journey takes about 2 seconds through multiple services. By default we have prestop sleep set to 15 secs (so it stops receiving traffic and has 15 seconds to complete).
There are one or two special cases where pods can build up an event backlog in memory that needs to be processed first, those we've extended to what makes sense.
But we can afford these longer wait times because we're running scaled and clients aren't impacted.
So case by case - start with 15 if you can afford to wait, check the logs/telemetry and adjust.
1
u/Alvasilev Aug 20 '26
One Node-specific thing that isn't really about ordering: server.close() stops new connections but does nothing about idle keep-alive sockets that are already open. If anything in front of you holds keep-alive, and a proxy usually does, your "let active requests finish" step can sit there long past your measured worst case, waiting on connections that have no request on them. closeIdleConnections() right after close() is what fixed it for us - difference between a 2 second drain and hitting the force-exit timer on every deploy.
On the unnecessary-complexity half: flushing notifications in the shutdown hook was the thing we deleted. If that path matters it has to survive SIGKILL or the box just dying, so it belongs in a durable queue, not in a handler that only runs on the polite exit. Closing DB connections we also stopped bothering with, the server side reaps them fine.
And the one that actually burned us, sideways to your question: watch what work you let into that process to begin with. We had a proxy that also ran builds, and one synchronous build call blocked the event loop. Socket stays bound, kernel keeps completing handshakes, everything behind it just queues, and no shutdown sequence helps because the process looks alive the whole time.
3
u/MulberryExisting5007 Aug 20 '26
You need to wait a reasonable amount of time. What that amount of time is, is dependent on many things, unfortunately. I’d suggest you look at expected behavior (e.g. you measure it at .84 seconds) and you either do a multiplier (10x would be ~ 8 seconds) or just pick an arbitrary value (like 20 seconds). How long is an average “active request” take? What’s your historical maximum time for a request? (The other steps you mention should take like a sec max.) Get that info and you can construct and upper bound for setting your timeout.
1
u/bittrance Aug 20 '26
Wait before killing: one second more than the timeout value of the load balancer or whatever is in front of the service.
1
u/aragossa Aug 20 '26
the order's basically standard, and the readiness probe is honestly the part that actually gets people. if you don't flip it to failing before you stop accepting connections, the LB or k8s keeps routing new traffic to you until the next health check poll fires, so you're still taking work for however long that interval is no matter what your app code does. flip readiness first, everything else can stay in the order you already have. flushing notifications and closing the queue is the part I'd worry about least here.
1
u/RoadsideCookie Aug 20 '26
Stop means finish what can't be interrupted and stop once safe to do so. Kill means fuck whatever you're doing, we're done here.
Shutdown means stop, wait a reasonable amount of time, then kill.
1
u/[deleted] Aug 20 '26
[removed] — view removed comment