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).
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
Quick Links