clustering in node.js using pm2

Posted By :Rakesh Chandra |30th December 2019

Introduction:-

                                        "Clustering is the Process of dividing the Data points into a number of groups".

The clustering module provides a medium to create multiple child processes that run simultaneously and share the same Port (Server port). Node js is single Threaded programming, which means the node is very fast and memory-efficient, the cluster module allows us to create child processes easily that run on their single tread, it allows us to handle the load. It is very easy to Made clustering in node.js using pm2,


Node.js Offers the Cluster Module, which basically Shares TCP connection with some Workers(are the processes running under the parent process).

 

 

 

How Does Clustering Works in node.js?

The cluster module will set a master and then fork our server app as a number of time as you want and also called a worker.


It Communicates with workers through IPC ( inter-process communication or interprocess communication) channels and Comes with an Embedded Load balancer that uses RR.Algo (Round-Robin Algorithm) for better Load.


When using the Round-robin Scheduling Policy, The master accepts all Incoming Connections and Send the TCP handle for the particular connection to
The chosen workers.

How to use this Features?

First, you have to install Cluster and all Dependency to your node project 

 

Here the Example:

Built-in Clustering :


PM@ internally hanldes all of the Above logics for you so you dont have to change anythings in our code .


The previous code becomes:

For starting the pm2 we have to type :


pm2 start ecosystem.config.js -i 4

 


 then here the output we can see in Terminal 


 

use pm2 show <id| name > to get more details about any app 

if will keep the app in running mode.

  if any of our workers happens to die .pm2 will restart them immediately so the user does not have to worry about that, or in any case, you can 


restart that app using the following 


< pm2 restart  >
  
how to check the log or pm2 

  by using following you can view the logs of pm2


<   pm2 logs >

Some useful command for Pm2


$ pm2 restart app_name
$ pm2 reload app_name
$ pm2 stop app_name
$ pm2 delete app_name


Conclusion 

A cluster module is a powerful tool it gets even better and easy to use with pm2. The clustering module provides a medium to create multiple child processes that run simultaneously and share the same Port (Server port). Node js is single Threaded programming, which means the node is very fast and memory-efficient, the cluster module allows us to create child processes easily that run on their single tread, it allows us to handle the load    It is very easy to Made clustering in node.js using pm2.

we suggest you use the latest version of Node js and pm2 in your project.

Thanks.


About Author

Rakesh Chandra

Rakesh has a good Knowledge of Node js,React js ,Web-Crawling/Scraping, Mysql/NOSql ,mongodb/couchdb ,AWS and GCP.

Request For Proposal

[contact-form-7 404 "Not Found"]

Ready to innovate ? Let's get in touch

Chat With Us