0

I know this question has been discussed in the past in much details (How is Node.js inherently faster when it still relies on Threads internally?) but I still fail to properly understand node.js event loop model and being a single threaded model how it handles concurrent requests.

Uptil now my understanding is : We receive an IO request --> a thread is spawned internally by node.js and IO request is handed to it --> since this is an IO request so CPU hands it to DMA controller and frees this thread --> this thread again goes into the thread pool to serve a different request --> DMA is still doing the IO, once DMA get all the data a sort of event is fired --> this event is captured by the node.js system and it puts the supplied callback function on the event loop --> whenever event loop get the opportunity it executed the callback on the data fetched by the IO -- > thanks to closures, callback function executes on the data fetched by the callback only

So this process goes on repeatedly. Please someone elucidate on my understand and provide some information

Community
  • 1
  • 1
JackSparrow
  • 187
  • 1
  • 2
  • 15
  • well, you clearly understand how the eventloop works, i'm not sure what we can say that would make it *click* for you. While events such as database interactions, file accessing, etc are taking place, the event loop continues on, processing other requests while waiting for said events to complete. Those other events are happening in separate threads by the system, not node. – Kevin B Nov 04 '14 at 17:11
  • Maybe the answer here will help? http://stackoverflow.com/questions/22887216/how-does-a-single-threaded-event-driven-webserver-like-node-js-work-at-the-soc – Kevin B Nov 04 '14 at 17:14
  • I do believe my general understanding is clear ... but I am looking for finer details ...when I say "since this is an IO request so CPU hands it to DMA controller and frees this thread --> this thread again goes into the thread pool to serve a different request" is this correct and is this the cause of less number of thread consumption (hence less memory usage) in node ? In another systems like java, if I code the same logic i.e hand an IO request to a thread then will that thread wait until the request is complete ? – JackSparrow Nov 04 '14 at 18:10

2 Answers2

2

There is only one thread (the main thread) for dealing with network I/O (file I/O is a slightly different story because not all platforms provide usable asynchronous, non-blocking file I/O APIs, so the synchronous file I/O APIs are used on those platforms in a threadpool).

So when network requests come in, they're all handled by the main thread which uses (indirectly via libuv) epoll/kqueue/IOCP/etc. for detecting (in a non-blocking way) when data is available (or when there is an incoming TCP connection for example). If there is data available, it calls out appropriately to javascript as needed, passing the socket data. If there is no data on the socket (and there's nothing else for the event loop to do, e.g. firing timers), then execution proceeds to the next iteration of the event loop where the process starts all over again.

As far as associating socket data with socket javascript objects goes, it's the combination of C++ wrapper objects (e.g. tcp_wrap, udp_wrap, etc.) and javascript objects that makes sure the data gets to the appropriate place.

Here's a slightly older diagram that explains what happens in a single cycle of node's event loop. Some of it may have changed slightly since node v0.9, but it gets you the general idea:

enter image description here

mscdex
  • 104,356
  • 15
  • 192
  • 153
0

node.js has a single threaded model which eliminates the need for locks and semaphores (used in the traditional multithreaded model). Locks and semaphores can add some costs in terms of performance and, more importantly, can provide a lot of rope to hang yourself with (in other words, many pitfalls). IO operations happen in parallel and because work between IOs is typically very small, this single threaded model usually works quite nicely.

(side note: if you have an app that does a lot of work between IO operations, i.e. CPU intense apps, that is a case where node doesn't not scale well)

I like to think of the argument for why node's model scales well is the same as why people think NoSQL scales better than SQL databases. Obviously Java (multi-threaded) and SQL scale; big companies like Facebook and Twitter have proven that. However, like in SQL, there are a lot of things you could do incorrectly to slow down your performance. Node.js doesn't eliminate all potential problems, it just does a good job of restricting many of the common causes.

mattr
  • 5,458
  • 2
  • 27
  • 22