Sobes.tech
Junior

Tell about Graceful Shutdown — what it is used for and how you implemented it.

sobes.tech AI

Answer from AI

Graceful Shutdown is the proper termination of an application or server, where all current processes are completed neatly, without data loss, and resources are released. It is used to prevent failures and data corruption during system shutdown.

Implementation typically includes:

  • Intercepting the termination signal (e.g., SIGTERM in Unix systems).
  • Stopping acceptance of new requests.
  • Completing processing of current tasks.
  • Cleaning up resources (closing connections, freeing memory).

Example in Node.js:

process.on('SIGTERM', () => {
  console.log('Received SIGTERM signal, starting graceful shutdown');
  server.close(() => {
    console.log('All connections closed, terminating process');
    process.exit(0);
  });
  // You can add a timeout for forced shutdown
});