HomeAbout MeContact

How to Run Cluster Using PM2?

By Mayank Patel
September 14, 2021
1 min read
How to Run Cluster Using PM2?

What is Cluster?

A cluster is formed when the pool of the same workers (child processes) runs under the parent Node process. Here both parent and child execute at the same time and share the same server port.

Furthermore, specific to each spawned child, there will be a V8 instance, memory, and event loop. For communication purposes with the parent node, workers use Inter-Process Communication (IPC).

What is PM2?

For the Node. JS applications, there is a “Process Management Module” that we call PM2. The main responsibility of the PM2 module is to initiate the Node.JS application and monitor its processing so that it won’t go down.

If it goes down, PM2 should reinitiate them without any downtime. We can understand it with an example. Suppose the Node.JS application stops in the mid of the processing, PM2 will restart the application spontaneously.

The process management model will make the application available again.

Pass the -I option in order to enable the cluster mode

pm2 start app.js -i max

Max refers, the number of available CPUs will be auto-detected by PM2, and it will run the maximum number of possible processes.

module.exports = {
  apps : [{
    script    : "api.js",
    instances : "max",
    exec_mode : "cluster"
  }]
}

Please Note: To cluster, one requires to set the exec_mode, which further helps PM2, to load balance between individual instances. Can not be executed by default.

Afterward to initiate the process file:

pm2 start processes.json

Tags

PM2 Cluster ModeRun Cluster Using PM2
Previous Article
How to install Nginx on ubuntu 20.04?
Mayank Patel

Mayank Patel

Software Developer | Ideator | CEO

Topics

React
Node.js
MongoDB - Loopback4
Server Configuration
Server Installation

Related Posts

How to Add New SSH/SFTP User to Ubuntu to Specific Directory?
September 16, 2021
1 min
© 2022, All Rights Reserved.

Quick Links

Reach me outAbout UsContact Us

Social Media