1

I have been trying to figure this out for a while. I wrote a very simple http server in node to benchmark the effect of using cluster. Here is my code:

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

if(process.argv.length >= 3)
{
  numCPUs = process.argv[2];
}

if (cluster.isMaster && numCPUs > 0) {
  console.log("launching " + numCPUs + " procs");
  // Fork workers.
  for (var i = 0; i < numCPUs; i++) {
    console.log("launching proc #" + 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(3000);
}

The problem is that I am not seeing any performance gain at all. 1 process has better performance most of the time. And, If I add more work, like retrieving data from redis or mongo then increasing the processes helps, but only modestly (around 15%). I've tried this on an i7 MBPr (quad-core with HT), and an i5 (quad-core) Win7 systems both with the same results.

Can someone please explain whats wrong with this code? Or, why am I not seeing an advantage/benefit in using cluster?

1 Answer 1

1

Your test appears to be almost purely I/O-oriented and in that situation using cluster provides little benefit (as you've seen) because I/O is concurrent regardless.

To see a significant benefit you'd need to have portions of your code that are CPU-bound because it's only then that you can provide additional parallelism between your cluster workers.

Sign up to request clarification or add additional context in comments.

2 Comments

That does make sense, but does that mean there are very few scenarios where cluster would make sense to use?
@George No, I wouldn't say that. Most real apps have enough CPU usage to make it worthwhile, but not a trivial example like you were using.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.