The short answer. Graceful shutdown is an agreement between the platform, the server and your workers. Stop admitting work, finish or safely hand off what is in flight, and leave before the platform forces termination.
Define what a successful shutdown preserves
A rolling deployment can interrupt the same business operation at several points: before a request is accepted, while a database transaction is running, after an external service succeeds, or before the response reaches the caller. Calling server.close() addresses only part of that path.
Write down the guarantees you need. A short read may finish. An upload may be canceled and retried. A leased queue job may become eligible for another worker. A payment operation may need reconciliation. These decisions belong to the operation, not to a single universal shutdown timeout.
This guide proposes a protocol for a Node HTTP service with background workers. The fragments illustrate coordination; adapt the policy to your platform, transport and job ownership model before using it.
Stop admitting new work
Expose a readiness state that can change independently from process liveness. Once shutdown begins, report that the instance should no longer receive new work, stop polling for jobs and stop accepting new connections. Existing proxies and connections may still send requests during propagation. On Kubernetes, removing the pod from Service endpoints happens asynchronously with the termination signal, so many services keep the listener open for a short, bounded delay (for example through a preStop hook) before closing it. Define how the application responds while draining.
Node's HTTP server close method stops accepting connections and handles idle connections on current supported versions. Active requests need time to finish. A forceful close is a separate action; upgraded protocols such as WebSockets require their own shutdown handling.
A common mistake is to close the database pool immediately after closing the listener. Requests already accepted may still need that pool. Treat resources as dependencies: drain their users before disposing of the resource, unless you have reached the forced termination deadline.
Use a single, repeatable state transition
A shutdown function should be idempotent. Multiple signals or administrative triggers should converge on the same operation rather than closing shared clients twice. Installing signal listeners changes Node's default termination behavior, so the application must now complete the exit path itself.
let shutdownPromise;
function requestShutdown(reason) {
shutdownPromise ??= shutdown(reason); // your bounded implementation
return shutdownPromise;
}
process.on('SIGTERM', () => {
void requestShutdown('SIGTERM').catch(reportFatalShutdown);
});
Here, shutdown and reportFatalShutdown are application functions, not Node APIs. Make their responsibilities explicit. A rejected cleanup promise must not disappear. Record the failure and let the bounded termination policy decide the final exit status. Avoid assuming that operating-system signals behave identically on every development platform.
Fit inside the platform deadline
Choose an application deadline shorter than the platform's termination grace period. Leave a margin for routing changes and cleanup. Kubernetes documents a termination grace period followed by forceful termination of remaining processes; your application cannot negotiate an unlimited extension merely by awaiting a promise.
A practical sequence is: mark unready, stop admission, wait for in-flight work, flush bounded telemetry, close resources and exit. A deadline may cut that sequence short. Write down what happens then: abort requests, relinquish work where safe, record unfinished operation IDs and terminate with a non-success status.
Do not wait forever for telemetry to explain why the process never ended. Give each cleanup dependency a budget within the overall deadline. If a timer is the last protection against a stuck process, understand how ref and unref affect whether it can keep the process alive.
Keep job ownership correct during handoff
A worker that stops polling can still be running a job. Its lease may expire while it drains, allowing another worker to acquire the same task. Either maintain ownership long enough to finish, or cancel and let another worker resume from a durable checkpoint. Both strategies require a defined timeout.
Do not acknowledge a queue item simply because shutdown has begun. Acknowledgment should follow the queue contract and the durable result. If the task calls an external provider, preserve its stable operation key and store any returned identifier before claiming completion.
Consider this failure drill: the provider accepts the action, the process receives a termination signal, and the result write fails. On restart, the new worker must reconcile or safely repeat that operation. Graceful shutdown reduces interruption; idempotency and durable state handle the interruption that still happens.
Run a deployment drill with observable outcomes
- Terminate an idle instance and confirm it exits within the configured budget.
- Terminate during a slow request and observe whether it completes or receives the documented failure.
- Terminate a worker immediately after an external side effect and inspect the resumed job.
- Keep a connection open, including any upgraded protocol you use, and confirm the forced path.
- Make telemetry or database cleanup hang and verify that termination remains bounded.
Record the runtime, platform grace period, longest drain duration and unfinished work count. The target is a predictable transition with recoverable state. A green deploy indicator alone does not show whether customers received duplicate emails or whether a background task vanished between instances.
Primary sources
Consult the current documentation when implementing. Links checked during preparation of this edition.