Grasping the Node JS alternative to multithreading

Multithreadingnode.js

Multithreading Problem Overview


If I understand correctly Node JS is non blocking...so instead of waiting for a response from a database or other process it moved on to something else and checks back later.

Also it is single threaded.

So does all this mean that a given Node JS process can fully and efficiently utilize a single CPU core but it will not use any other core on the machine, as in, it will never use more than one at a time.

This of course means that the other CPUs can still be used by other processes for things like SQL database or other intentionally separated CPU heavy subroutines as long as they are a separate process.

Also in the event that the Node JS process has an endless loop or long running function, that process is no longer useful in any way until the endless loop or long running function is stopped (or whole process killed).

Is all this right? Am I correct in my understanding?

Multithreading Solutions


Solution 1 - Multithreading

Pretty much correct, yes. The node.js server has an internal thread pool so it can perform blocking operations and notify the main thread with a callback or event when things complete.

So I imagine that it will make limited use of another core for the thread pool, for example if you do a non-blocking file system read this is likely implemented by telling a thread from the thread pool to perform a read and set a callback when it's done which means that the read could be happening on a different thread/core while the main node.js program is doing something else.

But from a node.js point of view, it's entirely single threaded and won't directly use more than one core.

Solution 2 - Multithreading

Yes, I'd say that your understanding is entirely correct. This article (archived) explains the rationale behind this design quite well. This is probably the most important paragraph:

> Apache is multithreaded: it spawns a thread per request (or process, it depends on the conf). You can see how that overhead eats up memory as the number of concurrent connections increases and more threads are needed to serve multiple simulataneous clients. Nginx and Node.js are not multithreaded, because threads and processes carry a heavy memory cost. They are single-threaded, but event-based. This eliminates the overhead created by thousands of threads/processes by handling many connections in a single thread.

Solution 3 - Multithreading

Even if this is an old thread, I thought, that I would share with an idea, how to utilize more that one core in Node.JS app. As Nuray Altin have mentioned - JXcore can do that.

Simple example:

var method = function () {
    console.log("this is message from thread no", process.threadId);
};

jxcore.tasks.runOnThread(0, method);
jxcore.tasks.runOnThread(1, method);

// this is message from thread no 1
// this is message from thread no 0

By default there are two threads (you can change it with jxcore.tasks.setThreadCount())

Of course there is much more that you can do with tasks. The docs are here.

Few articles on that subject:

Solution 4 - Multithreading

Since this question asked almost 2 years ago. Things are getting different or there are alternative approaches to multithreading problem on Node.JS

According to below blog post, using the incoming 'task' extension, some can benefit from other available cores directly.

http://oguzbastemur.blogspot.com/2013/12/multithread-nodejs.html

Solution 5 - Multithreading

Node.js is a single-threaded application, but it can support concurrency via the concept of event and callbacks. Here is video by Philip Roberts which explains how the event-loops works in javascript.

Click here to see the video

(Instead of WebAPIs there are C++ APIs in Node.js)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Question700 SoftwareView Question on Stackoverflow
Solution 1 - MultithreadingjcoderView Answer on Stackoverflow
Solution 2 - MultithreadingMichael BorgwardtView Answer on Stackoverflow
Solution 3 - MultithreadinginfografnetView Answer on Stackoverflow
Solution 4 - MultithreadingNuray AltinView Answer on Stackoverflow
Solution 5 - MultithreadingSaurabh LendeView Answer on Stackoverflow