r/softwarearchitecture • • Feb 27 '21

Why NodeJs is NOT "single threaded"

https://www.stackchief.com/questions/Is%20NodeJs%20really%20single%20threaded%3F
0 Upvotes

5 comments sorted by

7

u/[deleted] Feb 27 '21

Node is single-threaded. It's not a drawback, it's an architectural choice. I don't like PR "let's spin this" exercises in an architecture sub.

BTW, I/O is async, it doesn't need a pool of threads to handle per se. The OS does this.

5

u/heythisispaul Feb 27 '21

In fairness, I don't think that calling Node.js completely single-threaded is fair either - I think it's a little more complicated than a yes or no. The main process that runs the Event Loop and has access to your JavaScript runtime is indeed a single thread and can not be increased.

However there are lots of blocking tasks that can not be handled asynchronously. Anything CPU intensive, a lot of encryption and that type of thing. Node.js has C++ bindings that allow for these types of tasks to be offloaded onto k Workers in the threadpool. Some built in modules do this by default, such as dns, crypto and fs. It does indeed offload these to the process queue of whatever OS you're on, but it's still Node.js-specific logic that is using the single-threaded Event Loop to asynchronously queue up and poll for the completion of these blocking tasks that are offloaded into other threads.

I feel like the real answer is: "Is Node.js single-threaded?" "Yes" "Does Node.js use multiple threads?" "Also Yes"