Normally, node.js server runs on a single port and utilize only single core of the OS system which leads to wastage of hardware
capabilities. So, in order to take the full use of multi-core system, we launch cluster of node.js processes to handle the load.
This process is known as clustering. Clustering in node.js allows us to create different processes which can share the same server
port.
we can simply access cluster module using the following :
var cluster = require('cluster');
Let's understand clusters with the help of an coding example as shown below :
//Name of the file : cluster.js
var cluster = require('cluster');
var http = require('http');
var numofCPUs = require('os').cpus().length;
if (cluster.isMaster) {
console.log(`Master with Process ID : ${process.pid} is running`);
// Fork workers.
for (var i = 0; i < numofCPUs; i++) {
cluster.fork();
}
cluster.on('exit', (worker, code, signal) => {
console.log(`worker with Process ID : ${worker.process.pid} died`);
});
} else {
// Workers sharing an HTTP server
http.createServer((req, res) => {
res.writeHead(200);
res.end('An example of clusters\n');
}).listen(3000);
console.log(`Worker with Process ID : ${process.pid} started`);
}
>node cluster.js
Master with Process ID : 6832 is running
Worker with Process ID : 7616 started
Worker with Process ID : 5932 started
Worker with Process ID : 7056 started
Worker with Process ID : 7292 started
We have two methods of distributing incoming connections in cluster module of node.js which are as follows :
Using Round-robin approach
: In this approach, the master process listens on a port, accepts new
connections and distributes them across the workers in a round-robin fashion. It also contains some built-in algorithmic smart to
avoid overloading of any particular worker process. This is the default approach in all the operating systems except windows. Using Sockets
: In this approach, the master process creates listen sockets and send the incoming
connection to the interested worker and then the worker will accept the connection directly.
The methods of the cluster module are divided into 2 parts as shown below :
Worker
: These methods are for the worker processes :
close
event on those servers and then disconnect the IPC channel, within the worker. .kill()
or .disconnect()
, before that the value is undefined
. This method is used to distinguish
between the voluntary and accidental exit. If the value of .exitedAfterDisconnect
is True
, then
it signifies a voluntary exit.
True
if the worker is connected to its master via IPC channel,
otherwise it will return false
. True
if the worker process is terminated or dead , otherwise it will
return false
. cluster
: Most of these methods are for the master process with couple of exceptions as given below :
True
if the current process is master , otherwise it will
return false
. True
if the current process is worker , otherwise it will
return false
. cluster.settings
method.Events emitted while working with child processes are as follows :
Worker
: Worker can emit any of the following event : disconnect
, error
, exit
,
listening
, message
and online
. Cluster
: Cluster can emit any of the following event : disconnect
, exit
, fork
,
listening
, message
, online
and setup
. node.js
.