PM2 Clustering mode in NodeJS

Hi Guys! today we will learn pm2 clustering mode in NodeJS, it is mainly used to make the performance better and reliability for the NodeJS applications.

As we know that NodeJS runs on a single thread, if we want to set up the clustering then we have two options.

  1. We have to make the changes in a Code.
  2. OR we have to use the PM2 clustering mode.

Now the PM2 cluster mode has it’s own benefit’s,

You don’t want to make the changes in your NodeJS code.

Easy to scale up and Scale down.

It depends upon the Numbers of core you have in your Server.

Pm2-cluster-Max

So, we had used the command “sudo pm2 start tastethelinux.js -i max” which checks the number of CPU core we have in our server, and according to that, it will start that many clusters mode into the server.

It happens due to my server has 8 cores, So it started the 8 clusters mode into the server.

So, Can we define the Number of cores that can be used by our NodeJS Applications that are running at our server?

Cluster3_ioption_used

So, we have used -i 3 this will start the 3 clusters for the node’s application.

Now we will try to scale up the cluster mode by 2 means total we have 5 cluster mode running in our server.

Command: 
sudo pm2 scale tastethelinux.js +2

So now we are used scale option in PM2 with the +2 option to increase the cluster mode.

Let’s try to decrease by -3, means that total we have 2 cluster mode running in our server.

Clusterdown
Command: 
sudo pm2 scale tastethelinux.js 2

So, I have used pm2 scale tastethelinux.js 2 this will only run 2 NodeJS applications on our server

Also, on the above output, you can see that 3 process ID was getting deleted

learn the Installation of PM2, PM2 command, and PM2 logrotate.

Package for the Node PM2.

Learn the PM2 Clustering Command
To run the MAX PM2 cluster for the NodeJS.

Command:
#sudo pm2 start tastethelinux.js -i max
To run the 4 PM2 cluster for the NodeJS.

Command:
#sudo pm2 start tastethelinux.js -i 4
To Scale up the PM2 cluster for the NodeJS.

Command:
#sudo pm2 scale tastethelinux.js +2
To Scale Down the PM2 cluster for the NodeJS.

Command:
#sudo pm2 scale tastethelinux.js 2

3 Replies to “PM2 Clustering mode in NodeJS

Give your valuable time