Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does concurrency work in nodejs?

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?

like image 773
Jatin Avatar asked Apr 30 '15 20:04

Jatin


People also ask

How does node JS concurrency work?

Node js uses an event loop to maintain concurrency and perform non-blocking I/O operations. As soon as Node js starts, it initializes an event loop. The event loop works on a queue (which is called an event queue) and performs tasks in FIFO(First In First Out) order.

Is NodeJS parallel or concurrent?

At a high level, Node. js falls into the category of concurrent computation. This is a direct result of the single-threaded event loop being the backbone of a Node. js application.

How does node handle concurrent request?

How NodeJS handle multiple client requests? NodeJS receives multiple client requests and places them into EventQueue. NodeJS is built with the concept of event-driven architecture. NodeJS has its own EventLoop which is an infinite loop that receives requests and processes them.

Does NodeJS support parallelism?

Node can support "Parallelism" via either the Cluster or child_process modules packaged in the Nodejs Core API. Both of these modules create additional processes and not additional threads.


3 Answers

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.

like image 183
Trott Avatar answered Oct 12 '22 09:10

Trott


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.

like image 27
npe Avatar answered Oct 12 '22 10:10

npe


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.

like image 8
Steve U Avatar answered Oct 12 '22 10:10

Steve U