1

Since Node.js is single threaded, if I run an app that doesn't have any IO involved on Node.js, will it always use just one CPU core even if it is running on a 8 core CPU machine?

Say I have a service that returns a sum of two numbers running on Node, and there are 1000 requests hitting that service at the same time (assume the service computes the sum on the main thread and doesn't use call back since it is a simple task). So Node would handle only one request at a time even when there are 7 cores sitting idle? In Java world, if I set the HTTP thread pool size to 8 in an app server Tomcat, the same 1000 requests would be executed 8 times faster than node.

So do I have to run 8 instances of Node.js on an 8 core machine and front it with a load balancer so all 8 cores get used?

Suneel
  • 817
  • 3
  • 10
  • 23
  • *"if I set the HTTP thread pool size to 8 in an app server Tomcat, the same 1000 requests would be executed 8 times faster than node"* - did you measure that or is this just conjecture? – Gimby Oct 25 '16 at 14:35
  • @Gimby, Just a guess, assuming sum of two numbers takes the same time in Java as Node. I am ignoring the cost of additional threads since they are used from a pool and there won't be any synchronization needed in Java for such a method which doesn't share anything with other threads. – Suneel Oct 25 '16 at 14:45
  • Related: [Why is Node.js Single Threaded?](http://stackoverflow.com/questions/17959663/why-is-node-js-single-threaded) – Gimby Oct 26 '16 at 06:57

2 Answers2

2

Since Node.js is single threaded, if I run an app that doesn't have any IO involved on Node.js, will it always use just one CPU core even if it is running on a 8 core CPU machine?

Supposing that you do not fork any child processes, Node will use at most one core at a time, yes, regardless of how many cores are available. Having multiple cores will help a bit, though, because that reduces contention between Node and other processes.

So Node would handle only one request at a time even when there are 7 cores sitting idle?

Basically yes, though in practice, you're unlikely to ever have all other cores idle.

In Java world, if I set the HTTP thread pool size to 8 in an app server Tomcat, the same 1000 requests would be executed 8 times faster than node.

That follows only if the Java webapp processes requests in the same amount of time that Node does. Whether it will do so depends on a great many factors, and therefore is more or less impossible to predict. It can only be measured. It is conceivable that measuring the performance of a Java implementation of the webapp vs. a Node implementation of the same webapp would find that that particular Java implementation is much slower than that particular Node implementation under the test conditions. Or not.

So do I have to run 8 instances of Node.js on an 8 core machine and front it with a load balancer so all 8 cores get used?

I'm sure there are possible variations on the details, but basically yes.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
0

Use a load balancer to distribute load across multiple servers. To run Node.js in multiple processes on the same server, use cluster mode.

Like you, I also wondered what the impact of these different threading models would have in terms of performance which is why I conducted this investigation into that subject. I wrote two functionally identical I/O bound micro-services, one in Node.js and the other in Java / DropWizard (uses Jetty). I ran the same load test on both then captured the performance data and compared the results.

What I found was that latency for the two micro-services was very similar. It was throughput that was different. Without cluster mode, the Node.js service has 40% lower throughput. With cluster mode, the Node.js service had 16% lower throughput.

Glenn
  • 7,874
  • 3
  • 29
  • 38