0

There are articles claiming superior nodejs performance due to its single threaded event loop. I'm not asking for opinions, I'm asking for a mechanics explanation.

A thread starts to process a request, computes a little, and finds out that it needs to read from a database. This gets done asynchronously. No delay involved and the thread can continue... but what should it do without the data?

  • A1 Answer "don't know yet"?
  • A2 Grab another request?

A1 makes little sense to me. I can imagine a client issuing other requests in the meantime (like loading multiple resources on first site access), but in general, no.

A2 When it grabs another request, then it loses the whole context. This context gets saved in the promise which will get fulfilled when the data arrive, but which thread does process this promise?

  • B1 The same thread later
  • B2 A different thread.

In case B1 you may be lucky and some relevant data may be still in the threads' cache, but given that a DB request takes a few milliseconds, the gain is IMHO low.

Isn't case B2 practically equivalent to a context switch?

2
  • I guess the performance part of the article is not about thread cache or context switch, but more about handling multiple requests with a limited thread pool (in case all threads are blocked). Also the article doesn't consider other java server architectures, nor low-level or external bottlenecks for the nodejs server which would lead to different situations. Commented Sep 28, 2014 at 2:57
  • @Volune The article seems to be rather bad and I read others, but this one was the first and boldest I could find now. Actually, the image shows a single nodejs thread, and a couple of Java threads, out of which more than one is running => Java wins. ;) Commented Sep 28, 2014 at 14:36

2 Answers 2

1

Node.js is based on libuv C library.

Threads are used internally to fake the asynchronous nature of all the system calls. libuv also uses threads to allow you, the application, to perform a task asynchronously that is actually blocking, by spawning a thread and collecting the result when it is done.

A thread starts to process a request, computes a little, and finds out that it needs to read from a database. This gets done asynchronously. No delay involved and the thread can continue... but what should it do without the data?

Pass a callback to a DB module's method, and return from the current function which was invoked as an event listener too. Event loop will continue to next event in a queue. Context is accessible inside callback as function's closure.

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

Comments

0

A: Node.js will not respond to any request unless you write code that actively sends a response. It doesn't matter whether that code runs synchronously or asynchronously.
The client (or even the server's networking stack) cannot know or care whether asynchrony happened in the meantime.

B: There is only one Node.js thread, period.
When a response arrives for an asynchronous operation kicked off in Node.js code, an event is raised in the Node.js event loop thread, and the appropriate callback/handler is called.

4 Comments

Note: the answer for B: here is not true at least for filesystem operations on all platforms. There is a thread pool for those kinds of operations. However all network I/O is performed on the main thread using epoll/kqueue/iocp/whatever.
@mscdex: I'm talking about the Node.js thread which processes the response.
Perhaps, but "There is only one Node.js thread, period" (emphasis mine) is a bit misleading. Something like "There is only one node.js thread for all network i/o" is clearer though.
I agree with @mscdex , there is not only one Node.JS thread, it's probably preferable to say that all your userland native JavaScript code written in NodeJS will likely run on a single thread unless you explicitly ask you not to. For the very least it would be beneficial to soften the phrasing.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.