64

Node.js solves "One Thread per Connection Problem" by putting the event-based model at its core, using an event loop instead of threads. All the expensive I/O operations are always executed asynchronously with a callback that gets executed when the initiated operation completes.

The Observation IF any Operation occurs is handled by multiplexing mechanisms like epoll().

My question is now:

  • Why doesn't NodeJS block while using the blocking Systemcalls select/epoll/kqueue?

  • Or isn't NodeJS single threaded at all, so that a second Thread is
    necessary to observe all the I/O-Operations with select/epoll/kqueue?

3
  • 6
    I suggest to read this great article entitled 'Node is Not Single Threaded': rickgaribay.net/archive/2012/01/28/…
    – Wilk
    Nov 9, 2012 at 14:44
  • You Can't do parallel tasking/processing in NodeJS. Jun 18, 2013 at 17:37
  • You rarely run only one instance of Node in your deployment, so you'll have several threads. If you are using something like Sails.js (MVC framework for Node), you need to make sure your controller actions are atomic, otherwise the operations will get mixed and the result will be unexpected. Jul 1, 2017 at 9:16

4 Answers 4

119

NodeJS is evented (2nd line from the website), not single-threaded. It internally handles threading needed to do select/epoll/kqueue handling without the user explicitly having to manage that, but that doesn't mean there is no thread usage within it.

9
  • 151
    And the JavaScript environment runs in a single thread. All the other threads are handled by a C level thread pool.
    – Raynos
    Aug 10, 2011 at 21:44
  • 1
    I wish I could upvote you too, Raynos. (Edit: well I can, sort of) Aug 10, 2011 at 22:16
  • 1
    So does it mean that once after the threads in the internal thread pool is exhausted(that is all are busy) , requests to node js will also have to wait . Jan 3, 2014 at 3:08
  • 1
    @ren Multi-threading and parallel processing are two entirely distinct concepts.
    – Ant P
    May 19, 2014 at 12:06
  • 3
    @AntP Hm, to me they are not entirely distinct, on one machine you achieve parallelism by using multiple threads. No? Anyway that doesn't explain why nodejs uses only one core if it utilizes multiple threads.
    – ren
    May 19, 2014 at 13:08
10

No.

When I/O operations are initiated they are delegated to libuv, which manages the request using its own (multi-threaded, asynchronous) environment. libuv announces the completion of I/O operations, allowing any callbacks waiting on this event to be re-introduced to the main V8 thread for execution.

V8 -> Delegate I/O (libuv) -> Thread pool -> Multi threaded async

0

JavaScript is single threaded, so is event-model. But Node stack is not single-threaded.

Node utilizes V8 engine for concurrency.

0

No Nodejs in the whole is not single-threaded, but Node-Event loop (which nodeJS heavily uses) is single-threaded

Some of the node framework/Std Lib are not single-threaded

Your Answer

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

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