How to reload a Node.js server without downtime using PM2?

I am using PM2 to manage my Node servers. When I say pm2 restart 0, it restarts my app, but I get 5-10 seconds of downtime. How can I implement a zero down time deployment or at least minimise this downtime?

Also what's the difference between pm2 reload and pm2 restart?

Thanks folks!

Sandeep Panda's photo

The simplest way to do this in PM2 is to start your server in cluster mode. Use the following pm2 command to do this :

pm2 start www -i 1

The above command will spawn a child process in cluster mode so that later on you can reload your app by using the following command :

pm2 reload www

Now PM2 will attempt to reload your process without any downtime. If due to some reason, PM2 can't reload the app, it'll fallback to a restart.

More Info

Denny Trebbin's photo

We had this question here already a month ago :-)

hashnode.com/post/how-do-you-deploy-a-nodej..

James Howard's photo

Cool. How is your experience with NDM? Is it experimental? Can we achieve a similar setup using PM2?

Denny Trebbin's photo

NDM is stable and it works really good. We have a bunch of CentOS servers and some RedHat machines - no incompatibility issues so far. I'd argue about PM2 adding his own Node.js version dependency to the deployment workflow. This is the same with NDM too. PM2 has some more stats like uptime etc, PM2 runs virtually anywhere where Node.js runs. But we had 0 troubles with NDM on our CentOS machines and it's launched pretty fast after restarting our machines. PM2 took ~2 seconds longer, which isn't a big deal but if every second counts to not violate SLA's then 2 seconds can be very important ;-)