Ch. 6 · Node.js

Node.js Graceful Shutdown and In-Flight Requests

Node.js Graceful Shutdown and In-Flight Requests. Learn the reasoning, a practical example, common mistakes and an interview exercise.

~2 min readintermediateupdated Oct 3, 2026

Shutdown should stop new work, allow bounded completion and release dependencies. An unbounded wait can prevent termination, while immediate exit can interrupt writes.

Before you start

You should know JavaScript promises, asynchronous errors and the distinction between a process and a request. When following a server example, identify the resource owner and the point where work completes. Try experiments locally with bounded input instead of assuming production traffic behaves like a single request.

The practical goal is to reason through this situation: On termination, close the server to new requests and drain existing work within a deadline. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.

Step-by-step walkthrough

Step 1: Stop admitting work

Mark the service unready and stop accepting new requests before draining existing work.

Step 2: Use a bounded deadline

Allow active operations to complete only within an explicit grace period. Track resources still holding shutdown open.

Step 3: Release or cancel resources

Close owned pools and subscriptions, then apply the deadline policy to work that cannot finish safely.

Worked scenario

On termination, close the server to new requests and drain existing work within a deadline.

Termination arrives while one request is committing and another is idle. Immediate exit can interrupt writes, but an unlimited wait can prevent replacement deployment. A grace deadline makes the tradeoff explicit. Shutdown should be idempotent because multiple signals can arrive; repeated cleanup must not start competing termination sequences.

Common mistake

Calling process.exit immediately can discard buffered logs or incomplete operations.

Verify the behavior

Test idle, active and stuck requests, repeated signals and dependency cleanup. Check that no new work starts after draining begins.

Interview exercise

Handle stuck requests during shutdown.

Answer and reasoning

Set a grace deadline, cancel owned operations where possible and report work that could not finish safely.

Continue learning

Compare the scenario with the Node.js interview questions and test your understanding with the Node.js MCQs. For terminology and implementation details, consult the reference material.

More in Node.js

read ✓Node.js · hard

Node.js Clustering Across CPU Cores

Use cluster to run several workers on all cores, restart crashed workers, and understand shared-port and shared-state limits.

~2 min readread →
read ✓Node.js · hard

Node.js Password Hashing with scrypt

Store passwords as salted hashes with a slow key-derivation function, and compare candidates in constant time.

~2 min readread →
esc