19

I am trying to understand how concurrency works in a single-threaded environment like nodejs.

Let's say I have this code:

var fs = require('fs');

fs.readFile('file1', function one(err, data) {
  // some code...
});

fs.readFile('/file2', function two(err, data) {
  // some code...
});

Now each fs.readFile call is async. So, they are running concurrently. But if all this is happening in a single thread, then how is the concurrency achieved? Are function one and function two running on the same or different thread?

Basically, how does node.js handle concurrency?

2

3 Answers 3

20

As far as Node is concerned, there is only one thread. Asynchronous calls are put into the event queue and only run on the next tick.

For code that Node passes off to C++, all bets are off. There might or might not be threads there. But it doesn't really affect you.

The explanation that has been the most effective for me is "What the heck is the event loop anyway?" by Philip Roberts because the slow-motion visualization tool he shows makes everything pretty clear (to me, anyway).

You can experiment with Philip's tool Loupe online.

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

3 Comments

That is one nice video!
+1 on the Philip Roberts video. It's one of the best visualizations of the call stack/event loop collaboration for JS I've seen.
This is one great resource to look at - jsv9000.app
10

The thing with node.js is that everything runs concurrently, except for your code.

So, what that means is that there are actually lots of threads running inside node.js virtual machine (or a thread pool if you wish), and those threads are utilized whenever you call an async function like performing i/o operations on files, accessing databases, requesting urls, etc.

However, for your code, there is only a single thread, and it processes events from an event queue. So, when you register a callback it's reference is actually passed to the background worker-thread, and once the async operation is done, new event is added to the event-queue with that callback.

2 Comments

So, essentially, in the code I provided file1 and file2 are being read on separate threads?
While it's possible that the same worker-thread will be assigned with both tasks, it should be considered rather unlikely. But it's definitely a worker-thread who reads those files, and not your event-loop-thread.
8

Node.js does use multiple threads to handle io user the covers, but this is hidden from the user. The application environment (ie. your code) only has access to a single thread, but Node.js transparently hands off the io to a separate thread without the user needing to deal with it.

In your case, function one and two are both running in the same thread, but the actual io operations to read the file data were performed in separate threads.

A more detailed description of Node.js's threading/io model can be found here and a nice set of analogous examples can be read here.

Comments

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.