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.
- We have to make the changes in a Code.
- 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.
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?
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.
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.