1 min read

Cluster Module in Node JS

The cluster module provides a way of creating child processes that runs simultaneously and share the same server port.
Cluster Module in Node JS

Node.js runs single threaded programming, which is very memory efficient, but to take advantage of computers multi-core systems, the Cluster module allows you to easily create child processes that each runs on their own single thread, to handle the load.

Example

var cluster = require('cluster');

if (cluster.isWorker) {
console.log('I am a worker');
} else {
 console.log('I am a master');
 cluster.fork();
 cluster.fork();
}

Output:

I am a master

I am a worker

I am a worker

List of Functions

fork-->Create a new worker from a master.

disconnect-->Disconnect All workers.

isWorker-->Returns true if the current process is worker, otherwise false.

isMaster-->Returns true if the current process is master, otherwise false.

isDead-->Returns true if the worker's process is dead, otherwise false.

isConnected-->Returns true if the worker is connected to its master, otherwise false.

https://miro.medium.com/max/2000/1*VUhrsBC1AkJ0UDL4PgPG8Q.png
Node.js Cluster Module
https://www.w3schools.com/nodejs/ref_cluster.asp

#JavaScript #Node JS #Cluster #Probyto #ProbytoAI

Subscribe and follow us for latest news in Data Science and Machine learning and stay updated!

Facebook: https://facebook.com/probyto
Twitter: https://twitter.com/probyto
LinkedIn: https://linkedin.com/company/probyto
Instagram: https://instagram.com/probyto