Graceful Shutdown

Production Node.js services should handle shutdown signals such as SIGTERM. Graceful shutdown means stopping new work, allowing active requests to finish when appropriate, closing database connections, stopping consumers, and then exiting.

This is especially important in containerized environments where orchestration systems terminate processes during deployments and scaling operations.

Graceful HTTP Shutdown
const server = app.listen(3000);

process.on('SIGTERM', async () => {
  console.log('Shutdown started');

  server.close(async () => {
    await database.close();
    process.exit(0);
  });
});
💡 The process stops accepting new connections, closes the HTTP server, releases resources, and exits.
Key Takeaways
  • Handle termination signals explicitly in production services.
  • Close external resources during shutdown.
  • Graceful shutdown prevents dropped work and connection leaks.
Knowledge Check

Which signal is commonly used by container orchestrators to request graceful termination?