running NodeJS on multiple core systems using cluster modules

Cluster Module – The Solution to Running a NodeJS Application on Multiple core systems

You already know that while using NodeJS, if the size of the application increases, the performance decreases. So, how do single-threaded NodeJS Processes handle high concurrent requests? The answer is: by using a Cluster Module.

This post is intended for developers who are already familiar with the basics of NodeJS. If you are looking for hints on improving your architecture when your application has more number of hits or high concurrent requests, then you are at the right place.

How to increase the Performance

Computers consist of multi-core processors so that parallel processing is possible for any programming language. If any Programming Language uses the Multi-Threads, multiple cores may be in use. This thereby increases the speed and performance. However, when it comes to NodeJS application there is only one single thread which runs all the time.

 

The usual way for increasing the throughput of your NodeJS application is to spawn the process for each core of your machine. In this way, the NodeJS’ concurrency can multiply and parallelize.

There are two methods for achieving this. The first is by using NodeJS native Cluster Module.
The second is by using the PM2 Cluster Module.

1) Native Cluster Module

The basic way for scaling a NodeJS application is by using the Cluster Module. In this cluster Module, one instance of your process (‘Master’) is responsible for spawning the other child processes (‘Workers’). Workers are available as one for each core and they run your application. The incoming connections follow a round-robin strategy for distribution across all Workers. Also, these Workers are the ones which expose the services on the same port.

Sample code is below:

const cluster = require('cluster');
const http = require('http');
//First get the number of cores
const numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
   console.log(`Master ${process.pid} is running`);
   // Fork workers.
   for (let i = 0; i < numCPUs; i++) {
       cluster.fork(); }
       cluster.on(‘exit’, (worker, code, signal) => {
       console.log(`worker ${worker.process.pid} died`);
      });
   } else {
   // Workers can share any TCP connection
   // In this case it is an HTTP server
   http.createServer((req, res) => {
   res.writeHead(200);
   }).listen(8000);
console.log(`Worker ${process.pid} started`);
}

Forking the Process more than the cores available in the system is a bad idea. Hence, acquiring the number of available cores in the system should be done. Afterwards, the workers can be forked.

2) PM2 Cluster Module

PM2 is a famous Production Process Manager for NodeJS applications with a built-in Load Balancer. It permits you in keeping applications alive for a really long time. Furthermore, you can also reload them without downtime.

The cluster feature makes scaling process across every core possible, without worrying about the cluster module. The PM2 daemon covers the role of the ‘master’ process and spawns N processes of your application as Workers. Even more, this is done using round-robin balancing. By using the PM2, you attain scaling without any code modification.

PM2 cluster module enabling running of NodeJS app to run on multiple core systems
NodeJS app running on single core system vs running on a multiple core system

To enable the cluster mode, just pass the -i option:

pm2 start app.js -i 4

-i <number of workers> will tell PM2 that you want to launch your app in cluster_mode. If the ‘number of workers’ argument is0, PM2 will automatically spawn as many workers as the number of CPU cores you have.

cmd displaying the required function details of the PM2 module

If any of your workers die, PM2 restarts them immediately. Therefore, you do not need to worry about this either.

Finally, when using the Native module, you are the one who needs to handle the balancing of the load. Whereas, while using PM2 , the process manager automatically handles it.

As you can see, by using either of these two modules, you can run your NodeJS application on multiple-core systems.

Follow this blog to be up to date on the features of NodeJS.

References:

  1. https://nodejs.org/api/cluster.html
  2. http://pm2.keymetrics.io/docs/usage/cluster-mode/
  3. https://blog.pm2.io/node-js-clustering-made-easy-with-pm2/

Software Engineer

No.52, 2nd Floor, Dr.Radhakrishnan Salai,
Mylapore – Chennai – 600 004.
(Opp to Chennai Citi center)
p.charantej@bestirtech.com
 Facebook Icon

Leave a Reply

Your email address will not be published. Required fields are marked *