7

I would like to set up 4 different node.js instances, each on their own core. Does node.js stack new instances on the same core, or set them on new cores also?

The instances are unrelated and receive requests individually. I would like the cpu load spread out evenly. I haven't been able to find a definitive answer to this question.

GameDevGuru
  • 1,095
  • 2
  • 12
  • 27

2 Answers2

7

In general the system will try to do it this own, to maximize utilization of cpu. However if you want to target a particular CPU, you should check out TaskSet. It set's the affinity of the process.

Also there are several useful questions that discuss the same topic. Have a look.

  1. Upstart: each process on different core Nodejs

  2. Node.js - targeting a cpu core

  3. Node.js on multi-core machines

  4. In AmazonEC2 cpu core and nodejs

  5. How to deploy Node.js in cloud for high availability using multi-core

  6. Multi node modlue to utilize cpu

There is also a module, Cluster, that can also be very useful for CPU utilization. It let's you fork multiple processes to distribute load to multiple cores.


UPDATE


Finally, I have deployed something similar according to OP.


Case 1.


  1. I have a dedicated sever with 8 cores of cpu.
  2. I have deployed a single node thread and used the Cluster module to share the work load (As Described by pl47ypus PS: thanks for his answer ).
  3. The result is good, but some times the child thread may become unresponsive, So I have decided to try the old process, which I had used in my previous application.

Case 2.


  1. Same server, I have deployed 8 processes of node.js with different ports.
  2. Then I put nginx in front of these, listening on port 80 with worker process 8.
  3. Result is best than case 1 and also its very easy to configure nginx , its most stable too.

I also suggest you just try some of the solutions mentioned and keep monitoring your system in each case; like CPU, memory usage and io. Finally, from your tests you will see the best solution for your use case. Every application has its own requirements, so its better to try and find what your applications real need.

Community
  • 1
  • 1
Vivek Bajpai
  • 1,617
  • 1
  • 19
  • 35
0

try this:

var cluster = require('cluster')
  , 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.pid + ' died');
  });

  return;
}

complete code here

also, look here

Community
  • 1
  • 1
pl47ypus
  • 401
  • 2
  • 10
  • 1
    have u read the question its not asking how to do multi-threading in nodejs its asking how to run different node process instance on different core.So is it useful to use cluster ? – Vivek Bajpai Jul 31 '13 at 15:45
  • this is a quote from nodejs api docs "The cluster module allows you to easily create a network of processes that all share server ports." do you want access all processes through the same port? (http://nodejs.org/api/cluster.html#cluster_cluster) – pl47ypus Jul 31 '13 at 16:27
  • First of all cluster module is experimental second thing in the beginning of the doc http://nodejs.org/api/cluster.html#cluster_cluster clearly mention "A single instance of Node runs in a single thread. To take advantage of multi-core systems the user will sometimes want to launch a cluster of Node processes to handle the load" So pls do read things clearly "its very useful for launching multiple instance of same process to balance the load" – Vivek Bajpai Jul 31 '13 at 19:14
  • 1) you mentioned the cluster solution in your answer as well. 2) even though it wasn't mentioned, it's reasonable to believe that we're talking about the same application only running on multi-cores which is the purpose of cluster. – pl47ypus Aug 01 '13 at 01:06
  • PS: The cluster module is now stable – kehers Feb 22 '16 at 05:42