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.
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.
The answer is: It depends. On a system with multiple processors or CPU cores (as is common with modern processors), multiple processes or threads can be executed in parallel. On a single processor, though, it is not possible to have processes or threads truly executing at the same time.
Short answer, yes. A single core cpu(a processor), can run 2 or more threads simultaneously. These threads may belong to the one program, or they may belong different programs and thus processes.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With