Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maximum number of node.js processes in cluster mode

Tags:

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?

like image 205
Petr Skokan Avatar asked Jan 23 '15 11:01

Petr Skokan


People also ask

Is it possible to cluster multiple node processes?

Node. js applications can be parallelized using cluster modules in order to use the system more efficiently. Running multiple processes at the same time can be done using few lines of code and this makes the migration relatively easy, as Node.

Can NodeJS use more than 1 CPU?

Node. js absolutely does scale on multi-core machines. Yes, Node. js is one-thread-per-process.

What is cluster mode in NodeJS?

A Look at Clustering The Node. js Cluster module enables the creation of child processes (workers) that run simultaneously and share the same server port. Each spawned child has its own event loop, memory, and V8 instance. The child processes use IPC (Inter-process communication) to communicate with the parent Node.

How many cores can NodeJS use?

Javascript is a single threaded language, therefore one call stack and one memory heap. NodeJS uses Javascript to develop server side applications and shares the same behavior. It runs on one CPU core regardless of how many CPU cores you have in your machine or a virtual machine in the cloud.


1 Answers

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.

like image 63
Yuri Zarubin Avatar answered Oct 03 '22 21:10

Yuri Zarubin