Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node Js On Multi-Core Processors

So first things first I have a gut feeling that this is an utterly fullish question but anyway hear me out. I was thinking if Node Js is a single threaded application then can we run multiple instances of it on different ports on the same machine. Suppose I have an 8 thread processors does this mean I can have 8 node instances running without a performance hit. Provided I am installing enough ram and then I can have a load balancing for these 8 instances

like image 876
Vansh Narula Avatar asked Dec 03 '22 17:12

Vansh Narula


1 Answers

(V8 developer here.)

Yes, in general, running several instances of Node on the same machine can increase the total amount of work done. This would be similar to having several Chrome tabs, which can each do some single-threaded JavaScript work.

That said, it's most probably not as simple as "8 instances on an 8-thread processor gives 8 times the overall throughput", for several reasons:

(1) If you actually mean "8 threads", i.e. 4 cores + hyperthreading, then going from 4 to 8 processes will likely give 20-40% improvement (depending on hardware architecture and specific workload), not 2x.

(2) V8 does use more than one thread for internal purposes (mostly compilation and garbage collection), which is one reason why a single Node instance likely (depending on workload) will use more than one CPU core/thread.

(3) Another reason is that while JavaScript is single-threaded, Node does more than just execute a single thread of JavaScript. The various things happening in the background (that will trigger JS callbacks when they're ready) also need CPU resources.

(4) Finally, the CPU is not necessarily your bottleneck. If your server's performance is capped by e.g. network or disk, then spawning more instances won't help; on the contrary, it might make things significantly worse.

Long story short: it doesn't hurt to try. As a first step, run a typical workload on one instance, and take a look at the current system load (CPU, memory, network, disk). If they all have sufficient idle capacity, try going to two instances, measure whether that increases overall throughput, and check system load again. Then keep adding instances until you notice that it doesn't help any more.

like image 148
jmrk Avatar answered Jan 17 '23 16:01

jmrk