Petr Skokan
Petr Skokan

Reputation: 357

Maximum number of node.js processes in cluster mode

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

Answers (2)

Bufke
Bufke

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:

  • You have a 128 CPU server with 32GB of ram. The server runs multiple apps. You deploy a node service that gets 1 request per hour that uses 200mb per forked process. 1 process would be sufficient. numCPUs would cause 25.6GB of ram usage, likely exhausting all available ram and crashing other services if no ram limit is in place.
  • You have a Kubernetes cluster to deploy your node app. You measure memory usage and set requests/limits. The node the pod is scheduled on happens to have 1 cpu. Later the pod is scheduled to a node with 512 cpus. It launches 512 processes until the OOM killer stops it. There is no reason a sys admin would expect the physical aspects of the node that a pod is deployed in to radically change it's system resource requirements.
  • You are a developer in the very near future and you have the new 16GB ram/ 256 core ARM cpu laptop. You run this code, your laptop locks up immediately having run out of ram.

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

Yuri Zarubin
Yuri Zarubin

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

Related Questions