Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple instances of node.js on different cores

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.

like image 888
GameDevGuru Avatar asked Jul 30 '13 14:07

GameDevGuru


People also ask

Can NodeJS run on multiple cores?

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.

Can 2 threads run on different cores?

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.

Can multiple processes run on one core?

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.


1 Answers

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.

like image 72
Vivek Bajpai Avatar answered Oct 24 '22 00:10

Vivek Bajpai