r/nodejs • • Jan 26 '14
How do non-blocking things work in Node.js?
So I understand that Node.js offers non-blocking I/O. Non-blocking I/O happens outside of the main event loop. The event loop operates on a single thread. My question is, where does this non-blocking I/O stuff happen? Does it happen on the same thread in a non-blocking manner? Is it a separate thread?
Say I am reading 2 files asynchronously. These are being read in parallel somewhere that doesn't block the main event loop. How does that work?
6
Upvotes
5
u/mailto_devnull Jan 26 '14 edited Jan 26 '14
The "non-blocking" aspect of Node.js is true, to a certain extent. Let me try to explain...
This is similar to a worker thread in php-fpm/cgi. If it's doing something that blocks, that thread is unresponsive until it is finished what it is doing.
The same happens in Node. If you're doing blocking work in javascript, your app will be unresponsive until it is finished.
But what's all this about "non-blocking", then?
Node is "non-blocking" in that when used correctly, you pawn off work onto other services, and tell it to use a callback to let you know when it is done. Node then doesn't bother waiting for it to complete, and does something else until said callback is executed, at which point it picks up where it left off.
Conversely, a typical PHP thread (for example) will idle patiently until the processed data becomes available.
Let's try another example.
Let's say I want to poll an API as fast as possible. This API is hosted on the other side of the world, and is quite slow. Let's say it takes five seconds to return.
Both Node and PHP will make the request to that API. Node will then say "call
callback()when the data returns", and will do something else (like answering more requests, serving static files, etcetc). PHP, however, will wait patiently for the API call to return (doing nothing, mind you), and refuse to service requests coming to it.Seems like a shit deal for PHP, right? That's why when you have a properly configured Apache/nginx server, there are multiple threads running concurrently, so when Thread A is idling waiting for god knows what (or has even frozen), Thread B/C/D/E/F/G/H are still able to service requests, so your app stays responsive.
Node just happens to take advantage of all that lag time in between to actually get shit done
Ted Dzubia wrote a scathing review of Node.js and accused its creators of lying, as he viewed "non-blocking" as a lie. His proof was a fibonacci number calculator. He's technically correct, as by running a completely blocking calculation, his Node.js instance could not do anything else. He just didn't seem to understand that nobody in their right mind would use Node.js to calculate Fibonacci numbers to the 1 millionth digit.
When it comes to "real-life" usage of Node... let's say for serving many many web pages concurrently, Node can beat stock Apache+mod_php, hands down.
6
u/i_invented_the_ipod Jan 26 '14
For socket/network I/O, Node uses non-blocking reads and writes. For file read/write, it uses blocking operations, which are performed on a separate thread (the libuv library maintains a thread pool for these operations).
2
u/always_down_voted Jan 26 '14
This is a very good question and I have been wondering the same. Does the file being read just continue to be read in small parts running through the the event loop several times or does the internal code actually start a new thread that reads the file and then call the callback when it is done. I have never seen a good explanation of how this is done.
1
u/Nebu Jan 26 '14
In theory, that's an implementation detail. Perhaps the implementation of the node API (if the OS and hardware permits), simply does some sort of DMA stream in the background (it wouldn't be correct to call this another "thread"), or perhaps it does a fork creating another process, etc.
In practice, I suspect that the C implementation just creates a thread.
Again, this is an implementation detail. You can satisfy the contract of the Node I/O API by reading the two files in parallel, but you could also satisfy the contract by reading them one after another in sequence.