0
0
Node.jsframework~8 mins

Graceful shutdown handling in Node.js - Performance & Optimization

Choose your learning style9 modes available
Performance: Graceful shutdown handling
HIGH IMPACT
This affects server responsiveness and resource cleanup during shutdown, impacting user experience and server restart speed.
Handling server shutdown without dropping active requests
Node.js
process.on('SIGINT', () => {
  server.close(() => {
    // cleanup resources here
    process.exit(0);
  });
});
Waits for all active connections to close before exiting, ensuring no dropped requests.
📈 Performance GainImproves INP by avoiding dropped requests and allows clean resource release.
Handling server shutdown without dropping active requests
Node.js
process.on('SIGINT', () => {
  server.close();
  process.exit(0);
});
Exiting immediately after close() can drop active requests and does not wait for cleanup.
📉 Performance CostCauses dropped connections and poor INP due to abrupt termination.
Performance Comparison
PatternActive Requests HandlingResource CleanupShutdown DelayVerdict
Immediate exit after close()Drops active requestsNo cleanupMinimal but unsafe[X] Bad
Wait for close callback before exitWaits for active requestsAllows cleanupSlight delay but safe[OK] Good
Rendering Pipeline
Graceful shutdown affects server-side responsiveness and resource management, indirectly influencing client interaction speed and stability.
Request Handling
Resource Cleanup
Connection Termination
⚠️ BottleneckWaiting for all active requests to finish before shutdown
Core Web Vital Affected
INP
This affects server responsiveness and resource cleanup during shutdown, impacting user experience and server restart speed.
Optimization Tips
1Always wait for server.close() callback before exiting process.
2Track active connections to ensure they finish before shutdown.
3Set a timeout to force exit if shutdown takes too long.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance benefit of graceful shutdown in Node.js servers?
AIt reduces server startup time
BIt prevents dropping active requests during shutdown
CIt decreases memory usage during runtime
DIt improves CSS rendering speed
DevTools: Network
How to check: Simulate server shutdown and observe if active requests complete or get dropped in the Network panel.
What to look for: Look for requests that abruptly end or error out during shutdown indicating poor graceful handling.