Search code examples
node.jsdeploymentproduction

What's the process of updating a NodeJS running in production?


I am working on a webapp that will be published in production and then updated on regular basis as features and bug fixes are coming.

I run it like node app.js which loads config, connects to database, starts web server.

I wonder, what's the process of updating the app when I have next version? I suppose, I have to kill the process and start after update and deploy? It means, that there will be some downtime?

Should I collect stats on the least use during the week/month and apply the update during that period? Or should I start the current version on another machine, redirect all requests to it and update the main one, then switch back?


Solution

  • I think the second approach is better.

    The first one won't prevent downtime, it will just make sure it impacts the least number of users, while the second one creates no down-time at all.

    Moreover, I think you should keep the old version running in the other machine for some time in case you will find out the new version must be reverted due to whatever reason. In that case you will just have to redirect traffic to the old node without any downtime.

    Also, if you're setting up production environment I would recommend that instead of just running your process with "node" command, you will use something like forever or pm2 in order to do automatic restarts and some other advanced features.