0
0
Expressframework~8 mins

Graceful shutdown handling in Express - Performance & Optimization

Choose your learning style9 modes available
Performance: Graceful shutdown handling
HIGH IMPACT
This affects server responsiveness and user experience during shutdown by preventing dropped requests and resource leaks.
Shutting down an Express server without dropping active requests
Express
const server = app.listen(3000);
process.on('SIGTERM', () => {
  server.close(() => {
    process.exit(0);
  });
});
Waits for all active connections to close before exiting, ensuring no requests are dropped.
📈 Performance GainImproves INP by preventing request interruption and resource leaks.
Shutting down an Express server without dropping active requests
Express
const server = app.listen(3000);
process.on('SIGTERM', () => {
  server.close();
  process.exit(0);
});
Calling process.exit immediately after server.close can terminate the process before active requests finish.
📉 Performance CostDrops active requests causing poor INP and user experience.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Immediate exit after server.closeN/AN/AN/A[X] Bad
Wait for server.close callback before exitN/AN/AN/A[OK] Good
Rendering Pipeline
Graceful shutdown affects server-side responsiveness and user interaction timing rather than browser rendering directly. It ensures requests complete before the server stops, improving interaction responsiveness.
Request Handling
Network I/O
⚠️ BottleneckPremature process exit causing dropped requests
Core Web Vital Affected
INP
This affects server responsiveness and user experience during shutdown by preventing dropped requests and resource leaks.
Optimization Tips
1Never call process.exit immediately after server.close; wait for the close callback.
2Ensure all active requests finish before shutting down to avoid dropped connections.
3Use shutdown signals (SIGTERM, SIGINT) to trigger graceful shutdown handling.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance risk of calling process.exit immediately after server.close in Express?
AServer will consume more memory
BActive requests may be dropped causing poor user experience
CServer will respond slower to new requests
DIt increases bundle size
DevTools: Network
How to check: Open DevTools Network panel, send requests during shutdown signal, and observe if any requests are aborted or dropped.
What to look for: Look for requests that fail or abort during shutdown indicating poor graceful shutdown handling.