Nodejs callback mechanism - which thread handles the callback?
Asked Answered
E

1

11

I'm new to nodeJS and was wondering about the single-instance model of Node. In a simple nodeJs application, when some blocking operation is asynchronously handled with callbacks, does the main thread running the nodeJs handle the callback as well?. If the request is to get some data off the database, and there are 100s of concurrent users, and each db operation takes couple of seconds, when the callback is finally fired (for each of the connections), is the main thread accepting these requests used for executing the callback as well? If so, how does nodeJs scale and how does it respond so fast?.

Ellsworthellwood answered 6/8, 2013 at 13:0 Comment(1)
Found this really helpful : rickgaribay.net/archive/2012/01/28/…Ellsworthellwood
A
10

Each instance of nodejs runs in a single thread. Period. When you make an async call to, say, a network request, it doesn't wait around for it, not in your code or anywhere else. It has an event loop that runs through. When the response is ready, it invokes your callback.

This can be incredibly performant, because it doesn't need lots of threads and all the memory overhead, but it means you need to be careful not to do synchronous blocking stuff.

There is a pretty decent explanation of the event loop at http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/ and the original jsconf presentation by Ryan Dahl http://www.youtube.com/watch?v=ztspvPYybIY is worth watching. Ever seen an engineer get a standing ovation for a technical presentation?

Altdorfer answered 6/8, 2013 at 13:35 Comment(5)
This is still a bit unclear to me. So if there are 100 concurrent users, will there be just one instance of node running or 100?. Considering that there is an asynch call with a callback, and the thread (say threadA) exits that piece of code after placing the network/db request, when the callback is fired, is it threadA that executes this callback?.Ellsworthellwood
Yep. If there are 100 "concurrent users" (i.e. users making network requests of the nodejs instance), then there is still just one instance of node. When the request is made, the single thread handles it. When it makes an async call to read a file or access a network service or whatever, it is still the same thread. When the access is done and the callback is invoked, it is still the same thread. You can have multiple mode instances running, if you can front them with some sort of load-balancer, and node-cluster is planned but is still experimental.Altdorfer
@Altdorfer How can the only thread not block when it does 100 things at the same time? Let's say of the 100 things, each takes 1 second to complete, if all of them are in 1 thread, they have to been done in series, so does it mean it will block for 100 seconds?Airdrop
The question is, of that 1 sec/user, is it 1 sec of CPU-intensive processing, or 10ms of processing and another 990ms of waiting for network, disk, etc.? In most Web cases, it is the latter, and that is what nodejs (and nginx) are best suited for. If it is the former, then you will need a lot of node processes, or move to something like Java/Scala (which in any case will still use up the same amount of CPU, just have more threads...)Altdorfer
When this one thread request and I/O operation, it is handled by the Operating System. Your thread does not need to wait for a response so it just keep moving and waits for the event loop to notify the completion of the I/O request.Derzon

© 2022 - 2024 — McMap. All rights reserved.