skip to content

As the lead for several Node.js HTTP services, which shutdown defaults would you standardise so each one drains within its deploy budget, and what does each choice cost?

level: principalimportance: should knowfreq 30%

answer

  1. the budget is set from outside
  2. the longest legal request decides
  3. cut at a deadline or be killed
  4. long-lived connections need their own plan
  5. one shared module, tested under load

basics

~20 s

Standardise one shutdown module: a hard-exit timer below the platform's grace period, a fixed close order, Connection: close while draining, request time limits that fit the budget, and a rule for long-lived connections. Each trades deploy speed against dropped work.

solid answer

~40 s

I would ship one shared shutdown module rather than letting each service write its own. It installs the `SIGTERM` handler, fails readiness, calls `server.close()` and sends `Connection: close` while draining, then runs registered hooks in a fixed order: consumers, then pools, then telemetry. Its hard-exit timer is `unref()`'d and set a few seconds below the platform's grace period, so a stuck drain logs `process.getActiveResourcesInfo()` and exits with a code we chose instead of being killed silently. Each service declares its longest legitimate request, which must fit the budget, enforced with `server.requestTimeout` or per-route limits. Streams and upgraded connections get an explicit cut-off at the deadline. The cost is a longer budget, which slows every rollout, against a shorter one that drops work and pushes retries onto clients.

go deeper

for a junior

Know that a service gets a fixed time to stop and must finish its in-flight work before that time runs out.

for a middle

Explain how the budget limits the longest request a service can safely serve and why the hard-exit timer sits below it.

for a senior

Show how long-lived connections, leaked handles and slow requests each stretch a drain, and how you would measure drain time per deploy.

for a principal

Weigh rollout speed against dropped work, choose shared defaults and a module every service uses, and decide when long work leaves the request path.

## The decision a lead owns Every Node service needs a drain, and left alone each team writes a slightly different one: one forgets the hard-exit timer, another closes the pool first, a third logs on `SIGTERM` and never exits. The lead's choice is not *how* to drain, which has a well-known sequence, but **which defaults the whole fleet shares** and **how much time each service may take**. There is no single right answer; each default moves cost between deploy speed, dropped requests and engineering effort. ## Default 1: where the budget comes from The platform's grace period is a hard ceiling set outside the process. The Node side should finish **before** it: - the hard-exit timer fires a few seconds below the grace period, `unref()`'d so a fast drain still exits immediately; - the timer logs what is still alive with `process.getActiveResourcesInfo()`, calls `server.closeAllConnections()`, and exits non-zero. **Cost of a long budget:** every rollout of N instances waits up to N times the budget when instances drain one after another. **Cost of a short budget:** long requests are cut at every deploy, and clients must retry. ## Default 2: the longest legal request A drain can only be as short as the slowest request it waits for. Node's own server limits are generous: `server.requestTimeout` defaults to 300000 ms, five minutes, for receiving a request, and by default nothing bounds how long a handler may take to respond. So the policy should make each service **declare its longest legitimate request** and enforce it with timeouts so it fits inside the budget. A service that genuinely needs ten-minute requests should move that work to a job queue, not demand a ten-minute grace period from the platform. ## Default 3: a fixed close order, in one module Teams standardise a module, often a few dozen lines, that every service imports: 1. installs `process.once` handlers for `SIGTERM` and `SIGINT`; 2. flips a draining flag that readiness and the `Connection: close` header read; 3. calls `server.close()` and awaits its callback; 4. runs registered hooks in a fixed order: consumers, then database and cache pools, then telemetry exporters; 5. sets `process.exitCode` and lets the loop empty. **Cost:** a shared dependency every service must upgrade, and hooks that services must remember to register. **Benefit:** a bug fixed once is fixed everywhere, and the order can no longer be wrong. ## Default 4: long-lived connections Streaming responses, server-sent events and WebSocket sessions never finish on their own, so "wait for in-flight work" means waiting for the full budget. Note that `server.closeAllConnections()` does **not** destroy sockets upgraded to WebSocket or HTTP/2; whatever owns those must close them, ideally with a protocol-level close message that tells the client to reconnect elsewhere. The policy choice: | Option | Cost | |---|---| | Close long-lived connections at the start of the drain | every client reconnects at each deploy | | Close them at the deadline | the drain always takes the full budget | | Move them to dedicated instances with a longer budget | more infrastructure to run | ## Default 5: what counts as healthy A drain policy that nobody measures decays. The useful signals: - **drain duration** per instance per deploy, from the signal to exit; - **forced exits**, every time the hard-exit timer fires, logged with the resource list; - **errors at deploy time** seen by clients or the load balancer. A service whose drain always takes the full budget has a leaked handle or an unbounded request, and the metrics find it before users do. ## Testing the policy The cheapest guard is a CI test that starts the service, holds a slow request and a keep-alive connection open, sends `SIGTERM` with `process.kill(child.pid, 'SIGTERM')`, and asserts that the slow request completes, the process exits within the budget, and the exit code is the clean one. How the platform itself stops routing, delays the signal or sizes its grace period belongs to the orchestrator material; the lead's Node-side decision is what the process does with the time it is given.

  • A team asks for a fifteen-minute grace period because one Node.js endpoint generates large exports. How would you respond?
    I would push back on the budget, not the export. A long grace period slows every rollout of that service and still loses the export if the instance is killed. Moving the export to a background job that survives restarts, with progress the client can poll, keeps the HTTP drain short and makes the long work restartable rather than relying on one process staying alive.
  • Should the shared Node.js shutdown module exit with 0 or a non-zero code after a clean drain?
    Either works if it is consistent and distinct from a forced exit. Many teams use 0 for a clean drain and 1 when the hard-exit timer fired, so restarts and alerts distinguish the two. Some set 143 to keep the meaning of a SIGTERM exit for tools that expect it. The mistake is letting each service choose, because then no dashboard can tell a clean drain from a stall.

  • A longer grace period is free, so always use the maximum
  • Each service team should write its own SIGTERM handler
  • closeAllConnections() also ends WebSocket sessions at the deadline
  • requestTimeout already guarantees requests fit in the drain budget
  • If deploys look fine nobody needs to measure drain time

Assumes Node 24; server.requestTimeout has defaulted to 300000 ms since Node 18 (no limit before), and closeAllConnections() does not destroy upgraded sockets.