9

My question is quite simple. though, it may require different variable to be answered (i guess)

I'm playing around with node.js and I'm thinking of how to use it in a multi core architecture.

Latest version provides child_process.fork()and child.spawn() methods for multi-process programming. I've read on this very good (but dated) article about using Node.js as a large-scale Comet server. Now then nodejs provides multi-process programming, I really have no idea about how many processes should I have to spawn for serving large number of request (assuming my server is running on just one machine). Is there a way of choosing the 'best' (or at least a good) number of child processes doing the same job?

Any link to a starting guide would be very appreciated.

Thank you

ArtoAle
  • 2,939
  • 1
  • 25
  • 48

2 Answers2

14

The Node.js official documentation has an example of how to use cluster:

var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
  // Fork workers.
  for (var i = 0; i < numCPUs; i++) {
    cluster.fork();
  }

  cluster.on('death', function(worker) {
    console.log('worker ' + worker.pid + ' died');
  });
} else {
  // Worker processes have a http server.
  http.Server(function(req, res) {
    res.writeHead(200);
    res.end("hello world\n");
  }).listen(8000);
}

As you can see in the code above, you should use as many forks as the number of cpus you have, so that all cores work in the same time (distribute the work between your processor's cores).

alessioalex
  • 62,577
  • 16
  • 155
  • 122
  • 2
    I will give you a +1 if you updated your answer and explain why you use as many forks as the number of cpus. what if I don't have multi-core systems? should I only fork 1? what if the slave fails and exits using domains and I only got 1 cpu? what is the effect if I would use more then total of cpus? is there any senario were i would and wouldn't want to use all available cpus? – Endless Oct 08 '14 at 13:57
  • @Endless, the answer you are looking for is well explained here: https://stackoverflow.com/questions/9275654/how-many-child-processes-can-a-node-js-cluster-spawn-on-a-64bit-wintel-pc – bobbytables Sep 20 '17 at 09:32
5

In Node.js example, they want each core to run a single process only. There is no technical restriction on how many number of workers per core, so you can run as many as you can as long as your systerm can handle.

However, running more than one process per core does not improve performance of the app.

hope it helps!

Nam Nguyen
  • 5,668
  • 14
  • 56
  • 70