Tuesday, February 1, 2022

Cluster management in node js application by PM2 Lib

 In my previous blog, we discussed about how to manually manage cluster in node js application by writing the required code. In production mode, we may have to write more code to manage it.

Link: Cluster in node js application

We can avoid writing the manual code to manage the cluster in node js application. It can be done by using PM2 npm lib. Now, suppose we have below code where express js is creating a server and listening a port and we have to make it cluster supported by using PM2 lib.

const express = require('express');
const port = 8000;

const app = express();  

app.get('/', (req, res) => {
  console.log(`Incoming req accepted by Process ${process.pid}`);
  for(let i=0; i<999999999999999; i++) {

  }
  res.send('hello world');
});

app.get('/test', (req, res) => {
  console.log(`Incoming req accepted by Process ${process.pid}`);
  res.send('Quickly say Hello World');
});

app.listen(port, () => {
  console.log(`app is listening at port ${port} by Process ${process.pid}`);
});




Here, not a single line is written to manage cluster and if we run it and trigger "/" route first then it will keep running for few minutes as it is processing a large loop and in the mean time if we trigger "/test" route, it will have to wait till the completion of first route.

Now, if we enable cluster with multiple processes, then 2nd route will not have to wait for the completion of the first route. It can be done by PM2 lib as given below.

First, install PM2 lib at global level by using command like:

npm install pm2 -g

Now, if we are working in Windows environment, we can open cmd and go to the path where our app.js file is located where server creation code is written and run the command like:

pm2 start app1.js -i -1

This command will spawn number of processes = Number of CPU - 1

We can see the list of these initiated processes with below command:

We can monitor all the processes status by command like:

pm2 monit


If needed we can stop all or any one process by using command like:

pm2 stop all

pm2 stop id

Till, here, we have seen that we are able to initiate number of processes in node application by using pm2 commands where we provides the required number of processes each time we are running the command. We can create a config js file where we can add the required config related with cluster and now pm2 command we always read from this config file and accordingly it will start the processes. To create the config file, we can use below command like:

pm2 ecosystem

It will create a file named as ecosystem.config.js with some required config. We can add the required changes in this config file as per our requirement. The config file will look like as follows:

module.exports = {
  apps : [{
    script: 'app1.js',
    watch: '.',
    instances: 0,
    exec_mode: "cluster"
  }],

  deploy : {
    production : {
      user : 'SSH_USERNAME',
      host : 'SSH_HOSTMACHINE',
      ref  : 'origin/master',
      repo : 'GIT_REPOSITORY',
      path : 'DESTINATION_PATH',
      'pre-deploy-local': '',
      'post-deploy' : 'npm install && pm2 reload ecosystem.config.js --env production',
      'pre-setup': ''
    }
  }
};

Now, to start cluster by using PM2 command by utilizing the above ecosystem.config.js file, we need to run below command like:

pm2 start ecosystem.config.js

This is all about PM2 lib. We can find more details from https://www.npmjs.com/package/pm2











No comments:

Post a Comment

Please provide your precious comments and suggestion