How is Node.js inherently faster when it still relies on Threads internally?

There are actually a few different things being conflated here. But it starts with the meme that threads are just really hard. So if they’re hard, you are more likely, when using threads to 1) break due to bugs and 2) not use them as efficiently as possible. (2) is the one you’re asking about.

Think about one of the examples he gives, where a request comes in and you run some query, and then do something with the results of that. If you write it in a standard procedural way, the code might look like this:

result = query( "select smurfs from some_mushroom" );
// twiddle fingers
go_do_something_with_result( result );

If the request coming in caused you to create a new thread that ran the above code, you’ll have a thread sitting there, doing nothing at all while while query() is running. (Apache, according to Ryan, is using a single thread to satisfy the original request whereas nginx is outperforming it in the cases he’s talking about because it’s not.)

Now, if you were really clever, you would express the code above in a way where the environment could go off and do something else while you’re running the query:

query( statement: "select smurfs from some_mushroom", callback: go_do_something_with_result() );

This is basically what node.js is doing. You’re basically decorating — in a way that is convenient because of the language and environment, hence the points about closures — your code in such a way that the environment can be clever about what runs, and when. In that way, node.js isn’t new in the sense that it invented asynchronous I/O (not that anyone claimed anything like this), but it’s new in that the way it’s expressed is a little different.

Note: when I say that the environment can be clever about what runs and when, specifically what I mean is that the thread it used to start some I/O can now be used to handle some other request, or some computation that can be done in parallel, or start some other parallel I/O. (I’m not certain node is sophisticated enough to start more work for the same request, but you get the idea.)

Leave a Comment