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 insta
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
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.
Upstart: each process on different core Nodejs
Node.js - targeting a cpu core
Node.js on multi-core machines
In AmazonEC2 cpu core and nodejs
How to deploy Node.js in cloud for high availability using multi-core
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.
Case 2.
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.