How to Gracefully Shut Down Your Application: A Step-by-Step Guide

Graceful application shutdown prevents blocked requests, accidental data writes, and operation failures by releasing resources, closing database connections, and handling SIGTERM signals, and this article outlines a practical step-by-step process—including load balancer coordination and sample Node.js code—to ensure clean termination.

Node Underground
Node Underground
Node Underground
How to Gracefully Shut Down Your Application: A Step-by-Step Guide

Earlier I saw an article about gracefully shutting down applications, which mentioned common ideas such as release resources before shutting down and clean up database connections. If an application is not closed properly, disastrous reactions can occur, like blocked requests, accidental data writes, operation failures, etc.

The article also described a possible graceful shutdown process:

Application receives a stop message (received SIGTERM).

Tell the load balancer to stop accepting external requests.

Application server completes all ongoing requests.

Application server releases all connections, including databases, queues, etc.

Application exits, returning a success status code.

The simplest and most common method is to listen for process exit signals, for example:

process.on('SIGTERM', function onSigterm () {
  console.info('Got SIGTERM. Graceful shutdown start', new Date().toISOString())
  // start graceful shutdown here
  shutdown()
})

If your program does not implement this, you should add it promptly.

The article also mentions other ways to shut down an HTTP server; you can read the original article for more details.

Graceful ShutdownSIGTERMResource Cleanup
Node Underground
Written by

Node Underground

No language is immortal—Node.js isn’t either—but thoughtful reflection is priceless. This underground community for Node.js enthusiasts was started by Taobao’s Front‑End Team (FED) to share our original insights and viewpoints from working with Node.js. Follow us. BTW, we’re hiring.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.