Suneel Suneel - 1 month ago 28
Node.js Question

Single threaded Node.js on a multi core CPU vs Java (Tomcat)

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?

Answer

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.