Running Node.js in production using all of the cores
2nd Jan 2018
Did you know that the JavaScript environment is single threaded? This means that in the Node.js world, the main event loop runs on a single thread. IO operations are pushed out to their own thread, but if you are doing a CPU intensive operation on the main thread (try not to do this), you can get into problems where your server stops responding to requests.
The proper way to get around this is by programming in such a way that is considerate as to how the Node.js / Javascript runtime works.
However, you should also be running your Node.js application in production with this in mind to get the absolute best performance that you can. To get around this limitation, you need to do some form of clustering.
Production Clustering
You can run your Node.js app in production by using the "node" command, but I wouldn't recommend it. There are several Node.js wrappers out there that will manage the running of your Node.js app in a much nicer, production grade manner.
One such wrapper is PM2.
In it's most basic form, PM2 (which stands for Process Monitor) will keep an eye on your Node.js app for any crashes, and will attempt to restart the process should it crash. Crucially, it can also be configured to run your Node.js app in a clustered mode, which will enable us to take advantage of all of the cores that are available to us.
PM2 can be installed globally via npm:
npm install pm2 -g
Helpfully, we don't need to change any of our application code in order to have PM2 cluster it.
How many Workers?
PM2 has an optional argument - i, which is the "number of workers" argument. You can use this argument to instruct PM2 to run your Node.js app in an explicit number of workers:
pm2 start app.js -i 4
Where 4 is the number of workers that we want to launch our app in.
However, I prefer to set the "number of workers" argument to 0, which tells PM2 to run your app in as many workers as there are CPU cores:
pm2 start app.js -i 0
Et voilla!