pm2

Node process manager

What is a process manager?

Let’s start with defining what I’m writing about.

The process manager is an application which (obviously) manages set of processes. What does it mean is that it takes care that those processes are running and starts them again if they crash. Other things provided is unified logging and ability to provide easy access to the current process statistics, things like the CPU and memory usage.

Why you may need a process manager?

Process managers are particularly helpful in Node.js. There are three reasons for that.

Scale

Node.js is a single threaded application. This means it cannot use more than one CPU core. With current CPUs sporting more and more of them this is a serious limitation.

The only way to scale Node.js is to run it multiple times. This gives desired performance improvement but complicates administration and deployment. Fortunately, process manager can simplify it.

Uptime

Second reason, not really specific to Node.js, is that you want your application to run all the time. It doesn’t do any good if it crashes and your users can’t access it. Process manager will help ensure that even in the case of errors your application is functional. It will start again crashed process.

No downtime upgrades

Third reason, mostly applicable to websites, is the ability to upgrade the backend code without any downtime. To get that you need to run at least two copies of your web process. The simplest approach is when upgrading, one of those copies can be stopped, replaced with the new version and started again. Then the same process can be repeated with the second process. All this will happen without bringing the site down.

My process manager of choice

As always with Node.js there are many options to chose from. The most popular are:

I chose (somehow arbitrary) pm2. It has all that I need and so far it didn’t disappoint me.

What I use in pm2 for?

I use pm2 in two types of situations. The first is to run regular web traffic serving the application. The second is a message processing application, not accessible from the web but which is critical to be running all the time. More specifically I designed it so that in the case of any errors it would crash and give pm2 chance to restart it.

Advantages

The biggest advantage of pm2 over other solutions is a single file which describes my whole application, which can contain multiple processes.

In pm2 parlance it’s called ecosystem.json. It contains all the required settings for my code. I can also easily describe how many copies of each process I want pm2 to run. That way if I need to scale certain part of the application, it’s easily done by just tweaking one argument in the configuration file.

The second indispensable feature is the ability to generate startup script for my deployment platform (Ubuntu). With one easy command I can be sure that the application will be running even after the system reboot or if pm2 itself will crash (so far that never happened).

Disadvantage

My only complaint about pm2 is that the application description file doesn’t allow to specify common environment variables. I know I could just set it up outside of pm2 and let the processes inherit the setting that way. But the problem with doing it that way is that now application settings are in two places. It would be nice to not have to repeat myself so many times in the JSON file.

Summary

It’s hard to imagine any serious Node.js application without a process manager. Because of multiple available solutions, there should be no problem with choosing one which fits the requirements of any particular project.

So far my experience with pm2 is very positive. Other than small complain about the configuration file it’s a solution which fits what I need perfectly.