It is important to shutdown correctly your apps to handle well processing requests and prevent it to accept new ones. I’ll take a web server as exam

Shutdown correctly Node.js apps

submited by
Style Pass
2021-09-27 06:30:07

It is important to shutdown correctly your apps to handle well processing requests and prevent it to accept new ones. I’ll take a web server as example.

As we see our server doesn’t shutdown properly and processing requests don’t get right responses. First of all we need to understand how a Node process is terminated in order to fix it.

A process receives a signal when it is about to be killed. They are different kind of signals. We will focus on three of them in particular:

Node.js emits events when the process receives signals. You can write a handler for these events. In this one we will close our server so it deals with pending requests and prevent getting new ones.

Now our server handles well the request then shutdown correctly. You can read more in Nairi Harutyunyan’s nice article. It explain in details how to shutdown properly a server with a database.

I ran into a situation recently where my server needed to accept new requests in order to shutdown properly. I’ll give some explanations. My server subscribed to a webhook. This webhook has a limited quota of subscriptions. So if I don’t want to exceed this quota I need to unsubscribe properly when my server shutdown. Here is the unsubscription workflow:

Leave a Comment