Reputation: 357
Why all examples of cluster mode in node.js forks to number of processes that equals number of CPUs?
var cluster = require('cluster');
var numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', function(worker, code, signal) {
console.log('worker ' + worker.process.pid + ' died');
});
} else {
// worker process
// do the job....
}
Is it possible to fork to higher number of processes? I've tried this code with 10 processes on core-4 and it seems to be working. But- is it a good idea?
Upvotes: 27
Views: 8177
Reputation: 3331
It is a bad practice if you intend to share this code with others or potentially run it yourself on different hardware in the future. Consider some scenarios:
I ran a test of the Kubernetes scenario. I took a node app with this numCores line. I observed around 250mb of ram on a 2 core machine. I then faked it to be 64 "cores", then the application immediately crashed as it ran out of memory. At just 16 "cores" I observed a spike in ram usage.
Having a large amount of low power cpu cores doesn't imply the user (who may be your future self) wants to scale up massively and there are costs associated with adding processes. Those costs will vary based on the application. It would be better to set a limit on the number of processes ran or write documentation on how to manage these settings and tailor them to the environment and use case needs.
Upvotes: 0
Reputation: 11677
The reason why the number of forks equals the number of CPU cores, is because that is the optimal number. Increasing it past that, can decrease performance, reason being is that since if your processor has N number of cores, it can only process N number of processes at the same time.
For example: if you have 4 cores, and you have 10 processes where each process will have at minimum 1 thread, only 4 of these threads can ever be executed by your CPU concurrently. The rest of the threads will be waiting for their turn to be executed by the processor. Your OS will then intermittently perform a context switch , whereby it will pause a running thread, and switch to a thread that is waiting, and execute that thread instead. This "switching" process results in additional processing overhead. Therefore, for the most efficient use of your CPU cycles, you want the number of your forks (processes) to match the number of your cores in order to minimize context switches.
Upvotes: 30