63

Node.js servers are very efficient concerning I/O and large number of client connection. But why is node.js not suitable for heavy CPU apps in comparison to a traditional multithreading server?

I read it here Felix Baumgarten

5
  • 5
    Regardless of its truth; where did you pick this claim up? Why do you think this is the case?
    – poke
    Jun 7, 2013 at 0:51
  • 2
    If you know that it's not, then you should know why it's not. Otherwise you're just parroting somebody else's opinion.
    – user229044
    Jun 7, 2013 at 0:58
  • I read it here Felix Baumgarten
    – vuvu
    Jun 7, 2013 at 1:10
  • 1
    Node is a web server/app server. It was designed with the high concurrent users and heavy I/O in mind. CPU intensive jobs were not the key idea. Gradually it added cluster to make use of multi-core CPU.
    – user568109
    Jun 7, 2013 at 4:00
  • @vuvu Personally i don't think that article is a good article, most of the stuff is more like random jabber
    – sunitj
    Jul 21, 2015 at 5:56

5 Answers 5

100

Node is, despite its asynchronous event model, by nature single threaded. When you launch a Node process, you are running a single process with a single thread on a single core. So your code will not be executed in parallel, only I/O operations are parallel because they are executed asynchronous. As such, long running CPU tasks will block the whole server and are usually a bad idea.

Given that you just start a Node process like that, it is possible to have multiple Node processes running in parallel though. That way you could still benefit from your multithreading architecture, although a single Node process does not. You would just need to have some load balancer in front that distributes requests along all your Node processes.

Another option would be to have the CPU work in separate processes and make Node interact with those instead of doing the work itself.

Related things to read:

5
  • 2
    what about the worker threads doing the call backs, these belong to a threadpool according to this article: aaronstannard.com/post/2011/12/14/…
    – vuvu
    Jun 7, 2013 at 1:20
  • 7
    Your code won’t run on those worker threads. They are only used when you start something asynchronously (I/O stuff), but as soon as they call the callback, you are back on the event loop. So all the code you write will be on a single thread.
    – poke
    Jun 7, 2013 at 13:12
  • 1
    This youtube video youtu.be/8aGhZQkoFbQ explains the event loop and the above answer in detail. Feb 27, 2019 at 14:08
  • 1
    This doesn't sounds accurate to me. event loop is single thread, but it works as a task queue managers to different threads and process your work in parallel if you write them as a async task in Node. While one thread could be CPU busy, but others can do different works once their current task are finished, and the current task doesn't necessarily the CPU busy work. Nov 8, 2019 at 2:05
  • @user2734550 No, unless you explicitly use the newer worker threads, CPU-bound tasks, which is usually the JavaScript code you write, will run in a single thread. As I stated in the answer, I/O operations run separately but your code is restricted to that single main thread because JavaScript is by design single-threaded. That is still the case today.
    – poke
    Nov 8, 2019 at 10:19
26

A simple Node.js server is single-threaded, meaning that any operation that takes a long time to execute will block the rest of your program from running. Node.js apps manage to maintain a high level of concurrency by working as a series of events. When an event handler is waiting for something to happen (such as reading from the database), it tells Node to go ahead and process another event in the meantime. But since a single thread can only execute one instruction at a time, this approach can't save you from a function that needs to keep actively executing for a long time. In a multithreaded architecture, even if one function takes a long time to compute the result, other threads can still process other requests — and as long as you have a core that is not fully used at the time, there's a good chance they can do it about as quickly as if no other requests were running at all.

In order to deal with this, production Node.js apps that expect to hog a lot of CPU will usually be run in clusters. This means that instead of having several threads in one program's memory space, you run several instances of the same program under the control of one "master" instance. Each process is single-threaded, but since you have several of them, you end up gaining the benefits of multiple threads.

3
  • I read this article, where is says only the event loop is single threaded but not the computation fot the events, look there is a threadpool in the dawing aaronstannard.com/post/2011/12/14/…
    – vuvu
    Jun 7, 2013 at 1:15
  • @vuvu: That article is a little confusing. He explains it incorrectly first (suggesting each event handler gets its own thread), but later explains it correctly (many utility functions that you'd use in your event handlers run on a background thread so you don't block when you are, say, reading a file). This article explains it more clearly.
    – Chuck
    Jun 7, 2013 at 1:36
  • @Chuck I know Promises didn't exist at the time of this answer, but with its introduction, you can just wrap any long running function that's "blocking" in a Promise callback and make it behave like an asynchronous function since NodeJS will put your wrapped function in a Microtask queue. And if that function is very CPU intensive and frequently used, you might as well create a dedicated server in a language built for computation to handle those functions/requests. Now you can just use your NodeJS server purely as an API layer that interfaces with your server(s) handling those functions. Sep 9, 2021 at 5:01
2

Node is flawless if you are having asynchronous tasks because java script will run these things by worker pool. But if you run CPU intense tasks (where you heavily use CPU ) Ex you have a billion users and you want to sort those people on name. Its quit a Intense tasks, and this is synchronous which will block other code from running.

So its not a good idea to use node for these kind of applications. Technically you can find alternatives to address those kind of tasks. The above example is better addressed in a Db. then passing that result is great.

In the same way avoid Intense task and keep your CPU cool for better performance

1

You can have a look at this package, the-computer, which may help you do some cpu intensive works in a single instance of node.js app in a simple way.

Definitely it is not as effective as raw c++ libs, but it can cover most general computing cases, keeping you in node.js garden while allowing you leverage the cores of the cup.

0

Node.js runs JavaScript code in a single thread, which means that your code can only do one task at a time. However, Node.js itself is multithreaded and provides hidden threads through the libuv library, which handles I/O operations like reading files from a disk or network requests. Through the use of hidden threads, Node.js provides asynchronous methods that allow your code to make I/O requests without blocking the main thread.

Although Node.js has hidden threads, you cannot use them to offload CPU-intensive tasks, such as complex calculations, image resizing, or video compression. Since JavaScript is single-threaded when a CPU-intensive task runs, it blocks the main thread and no other code executes until the task completes. Without using other threads, the only way to speed up a CPU-bound task is to increase the processor speed.

💡 Node.js introduced the worker-threads module, which allows you to create threads and execute multiple JavaScript tasks in parallel. Once a thread finishes a task, it sends a message to the main thread that contains the result of the operation so that it can be used with other parts of the code. The advantage of using worker threads is that CPU-bound tasks don’t block the main thread and you can divide and distribute a task to multiple workers to optimize it.

ref: https://www.digitalocean.com/community/tutorials/how-to-use-multithreading-in-node-js

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.