Cluster

suggest change

The cluster module allows one to start the same application multiple times.

Clustering is useful when the instances follow the same flow and do not depend on one another. One master starts forks (the children). Each child is its own process, with its own memory and event loop.

Clusters can help websites and APIs. Any worker can serve any customer, because it does not depend on the other workers. Shared data, such as sessions, has to live somewhere else (for example Redis), because variables are not shared between processes.

// runs in each instance
var cluster = require('cluster');
var numCPUs = require('os').cpus().length;

console.log('I am always called');

if (cluster.isMaster) {
    // runs only once (within the master);
    console.log('I am the master, launching workers!');
    for(var i = 0; i < numCPUs; i++) cluster.fork();

} else {
    // runs in each fork
    console.log('I am a fork!');
  
    // here one could start, as an example, a web server
  
}

console.log('I am always called as well');

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents