0
0
Node.jsframework~8 mins

Graceful shutdown on errors in Node.js - Performance & Optimization

Choose your learning style9 modes available
Performance: Graceful shutdown on errors
HIGH IMPACT
This affects server responsiveness and resource cleanup during error conditions, impacting user experience and server stability.
Handling unexpected errors to avoid server crashes and blocked requests
Node.js
process.on('uncaughtException', (err) => {
  console.error('Uncaught Exception:', err);
  server.close(() => {
    // cleanup resources here
    process.exit(1);
  });
});
Stops accepting new requests and cleans up resources before exit, preventing request blocking and memory leaks.
📈 Performance GainReduces INP spikes by quickly freeing resources and avoiding stuck requests.
Handling unexpected errors to avoid server crashes and blocked requests
Node.js
process.on('uncaughtException', (err) => {
  console.error('Uncaught Exception:', err);
  // No shutdown or cleanup, server keeps running
});
Server continues running in an unstable state, causing unpredictable behavior and potential request blocking.
📉 Performance CostBlocks new requests indefinitely, increasing INP and causing poor user experience.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
No graceful shutdown on errorN/AN/AN/A[X] Bad
Graceful shutdown with cleanupN/AN/AN/A[OK] Good
Rendering Pipeline
Graceful shutdown does not directly affect browser rendering but impacts server response times and availability, which influence interaction responsiveness (INP).
Server Response Time
Request Handling
Resource Cleanup
⚠️ BottleneckRequest Handling when server is unstable or blocked
Core Web Vital Affected
INP
This affects server responsiveness and resource cleanup during error conditions, impacting user experience and server stability.
Optimization Tips
1Always stop accepting new requests before shutting down on error.
2Clean up resources like database connections to prevent leaks.
3Exit the process after cleanup to avoid unstable server state.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance risk of not implementing graceful shutdown on errors in Node.js servers?
AServer will use less memory
BServer will respond faster to requests
CServer may block new requests causing slow or no responses
DServer will automatically restart without delay
DevTools: Network
How to check: Open DevTools Network panel, simulate server error, and observe if requests hang or fail quickly.
What to look for: Look for stalled or hanging requests indicating blocked server; fast error responses indicate good shutdown handling.