How to achieve truly zero downtime deployment using pm2?

Sandeep Panda

ยท

ยท

5.1K views

If you are running any Node/Express apps in production, you are most likely using PM2 to manage them. One of the cool features of pm2 is that it lets you run your app in cluster mode with multiple instances. One of the benefits of this approach is that you can achieve zero downtime deployment.

pm2 start app.js -i 2
pm2 reload app

PM2 achieves this by restarting the instances one by one. After instance-0 is successfully restarted, instance-1 is rebooted. This makes sure that there is at least one instance to accept the requests.

However, if your app is doing something heavy during the startup or if there is a slight delay, you might see some downtime (maybe for a couple of seconds). After instance-0 is restarted, it may not be able to service the requests immediately. So, the deployment is not zero-downtime anymore.

Fix

Use --wait-ready flag to tell pm2 when your process is up and is able to accept requests.

pm2 start app.js -i 2 --wait-ready

In your Node app, you add the following piece of code when it's ready:

process.send('ready');

This makes sure pm2 reload moves to the next instance of the process only after the previous one is truly up and is able to service the incoming requests.

If your app takes more than 3s to start, you need to increase the timeout as following:

pm2 start app.js -i 2 --wait-ready --listen-timeout 5000

Now pat yourself on the back! You can now deploy ๐Ÿš€ your app with 0 downtime.

For more details, visit https://pm2.keymetrics.io/docs/usage/pm2-doc-single-page/

1 comment
Add a comment