65

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/… Commented Nov 9, 2012 at 14:44
  • You Can't do parallel tasking/processing in NodeJS. Commented 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. Commented Jul 1, 2017 at 9:16

4 Answers 4

123

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.

Sign up to request clarification or add additional context in comments.

9 Comments

And the JavaScript environment runs in a single thread. All the other threads are handled by a C level thread pool.
I wish I could upvote you too, Raynos. (Edit: well I can, sort of)
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 .
@ren Multi-threading and parallel processing are two entirely distinct concepts.
@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.
|
11

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

Comments

1

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

Node utilizes V8 engine for concurrency.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.