A deployment can interrupt accepted work even when the replacement instance is healthy. Closing the database before active requests finish, accepting new jobs while draining, or waiting forever on an open stream all turn an ordinary restart into a recovery problem.
Define shutdown as a bounded lifecycle with explicit admission, draining, and cleanup rules. This guide covers server applications and background workers, with Node.js HTTP and Kubernetes as documented examples. Adapt the sequence to the framework and platform rather than assuming one signal handler covers every resource.
Stop admission before waiting for completion
Record a draining state as soon as shutdown begins. It should be idempotent: repeated termination signals must not create several competing cleanup sequences. Make the state visible to the parts of the process that accept requests or claim jobs.
Coordinate readiness changes with the load balancer's routing behavior. Marking an instance unready does not imply that every upstream has already stopped sending traffic. Existing connections and routing propagation can leave a period during which work still arrives.
Choose the response for requests that reach a draining instance. A bounded rejection can give a client a documented retry opportunity, while allowing selected short requests may be appropriate for another service. Apply that policy consistently before starting new side effects.
For Node.js, the HTTP server documentation explains that server.close() stops accepting new connections and closes idle ones while active requests can finish. The application still needs to decide how to handle additional work associated with established connections and resources outside that server.
Track accepted work by its completion condition
A request is not necessarily complete when the handler returns. Its response may still be streaming, a transaction may still be committing, or a downstream operation may have been accepted without a local outcome. Define which boundary counts as completion for each work type.
In a synthetic export service, a short metadata request and a long download stream need different handling. Waiting for every download without a limit can prevent termination. Abruptly destroying all connections can interrupt a mutation that the server has already committed.
Track active work using bounded ownership rather than a global collection that grows indefinitely. Remove entries when work completes, fails, or is cancelled. Make registration and shutdown admission agree so that a new task cannot escape tracking during the transition.
Stop background job acquisition before waiting for currently owned jobs. If a job cannot finish within the budget, release or expire its claim according to the queue protocol. Do not acknowledge unfinished work merely to make the process exit cleanly.
Close resources in dependency order
Keep resources available while accepted work depends on them. A database pool used by active requests should usually remain open until those requests finish or reach their cancellation boundary. The same reasoning applies to broker publishers, object storage clients, and local file handles.
For a synthetic application, the sequence may be: stop accepting requests and jobs, drain accepted work, complete required durable records, close outbound clients, flush bounded telemetry, and exit. Explain any resource that must close earlier because it controls admission itself.
Telemetry should have its own deadline and sanitized failure handling. A logging service outage should not hold the process alive forever. Preserve the evidence required for recovery through durable application records where necessary rather than relying on a last-second log flush.
Review upgraded protocols separately. Node's forceful HTTP connection closure does not terminate sockets upgraded to another protocol. WebSockets, HTTP/2 sessions, database subscriptions, and application-created timers need owners and explicit lifecycle rules.
Make the deadline part of the deployment contract
Choose an application deadline that fits inside the platform's termination budget with room for cleanup. Derive it from accepted operation durations and recovery requirements, not from a copied example timeout.
Kubernetes Pod termination documentation describes termination grace periods, lifecycle hooks, and eventual forceful termination. A preStop hook consumes that budget rather than providing unlimited additional drain time. Verify how the actual container receives the termination signal.
When the application deadline expires, stop waiting and apply the defined recovery policy. Cancelling a local request does not prove that a remote mutation did not happen. A client that saw a connection failure may need an operation-status query or an idempotent retry path.
Use timeouts and retry design to make those outcomes explicit. Ensure that the shutdown timeout, client timeout, and queue lease do not contradict each other. A job lease that expires before a long operation finishes can create concurrent processing during termination.
Exercise the deployed termination path
Test shutdown while a request is idle, reading its body, writing its response, committing data, and waiting on a slow dependency. Include persistent connections, streams, and a worker that owns a job. Verify the resulting state, not just the process exit code.
In an isolated deployment, terminate the instance through the platform's normal mechanism. Assert that admission stops, accepted work follows its policy, and the process exits within the configured budget. Then restart and confirm that interrupted operations can be identified and recovered.
Also test a second termination signal, a dependency that never responds, and a cleanup operation that fails. Error handling should preserve the deadline and avoid exposing stack traces or resource details to clients. A shutdown handler that works only when every dependency cooperates is not a useful release gate.
Collect the drain duration, number of active operations at the deadline, interrupted work, and recovery outcomes. Keep identifiers out of high-cardinality metric labels. The release is ready when a restart produces a known combination of completed, rejected, and recoverable work rather than an unexplained gap between accepted requests and durable results.
Was this guide useful?
Your rating helps us prioritize clearer, more practical technical content.
Review diffs, run checks, and prepare the release in Arezgit.
Keep Git review, security scanning, API checks, database inspection, and release preparation together in one local desktop application.
Explore Arezgit