Answer the question
In order to leave comments, you need to log in
How to properly run a nodejs application in a cluster on a multiprocessor machine?
We take an example from documentation:
if (cluster.isMaster) {
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`);
});
} else {
// start my application
}
Answer the question
In order to leave comments, you need to log in
Think of each worker as if it were running on a remote machine that doesn't know anything about another worker running on another remote machine. Then it will become clear that the tasks should be added to the queue server, and he will already scatter the tasks among the workers so that they do not try to perform the same task at the same time.
For queues, you can use kue . Works on top of redis, which many people already use to scale the application across several physically remote servers, caching and everything else.
The config can be included in the index file and given to each copy of the application.
const Application = require('path/to/Application');
const config = require('path/to/config');
if (cluster.isMaster) {
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`);
});
} else {
new Application(config).run();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question