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?

Null
  • 1,950
  • 9
  • 30
  • 33
Filipe Santos
  • 1,629
  • 3
  • 18
  • 27
  • 6
    I suggest to read this great article entitled 'Node is Not Single Threaded': http://rickgaribay.net/archive/2012/01/28/node-is-not-single-threaded.aspx – Wilk Nov 09 '12 at 14:44
  • You Can't do parallel tasking/processing in NodeJS. – Samuel0Paul Jun 18 '13 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. – Chris Vilches Jul 01 '17 at 09:16

4 Answers4

118

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.

Femi
  • 64,273
  • 8
  • 118
  • 148
  • 150
    And the JavaScript environment runs in a single thread. All the other threads are handled by a C level thread pool. – Raynos Aug 10 '11 at 21:44
  • 1
    I wish I could upvote you too, Raynos. (Edit: well I can, sort of) – Benjamin Atkin Aug 10 '11 at 22:16
  • But how to explain that node just uses one core then? – ren May 03 '13 at 13:01
  • 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 . – Harish Kayarohanam Jan 03 '14 at 03:08
  • 1
    @ren Multi-threading and parallel processing are two entirely distinct concepts. – Ant P May 19 '14 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 '14 at 13:08
  • If I understand correctly - libuv implements the underlying async io operations. But just to clearify - Does libuv support actual async io if such is available by the operation system and hardware (for example, the disk hardware issues a command to the OS once a write is finished), and if I understand correctly - in other cases, libuv will fill in the gap by having C threads that actually do block and wait for a response (So asynchronicity is a JS illusion). So theoretically, depending on libuv's implementation and the OS/Hardware, it can be actually single-threaded async across the board). – user967710 Feb 16 '16 at 19:14
  • Asp.net MVC uses. Multithreading to concurrently process requests, by doing so it utilities all The cores. If a node server Handel's incoming requests on the same single.thread, isn't. Something lost performance wise? – eran otzap Mar 28 '17 at 05:29
  • This answer is completely wrong. Node correctly does not use threads to wait on select/epoll/kqueue etc. It waits on them on the single thread. Node's additional threads are only involved in I/O that cannot be waited on with select/epoll/kqueue etc. namely disk I/O (technically it can but cross-platform code for this is a nightmare due to Windows), DNS and certain cryptographic functions (because crypto is not I/O but is CPU intensive) – slebetman Feb 06 '20 at 13:06
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

serkan
  • 6,885
  • 4
  • 41
  • 49
0

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

Node utilizes V8 engine for concurrency.

Pujan
  • 3,154
  • 3
  • 38
  • 52
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

yaswanthkoneri
  • 408
  • 4
  • 16