Home > Net >  How Node.js handles concurrency?
How Node.js handles concurrency?

Time:10-18

I was reading this book.

It says that the defining feature of Node.js is its single-threaded event-based concurrency enabled by an asynchronous-by-default API.

For me, single-threaded means that Node will execute only one thing at a time. So how exactly is concurrency achieved?

Also, if someone could explain the defining feature above, it would be a great help.

I also wanted a few resources explaining the inner workings of Node.js.

I am new to JavaScript.

CodePudding user response:

Node.js (and, generally speaking, JavaScript) uses an event loop to execute statements. Meaning that asynchronous code is pushed to the end of the event queue.

The event loop is what allows Node.js to perform non-blocking I/O operations — despite the fact that JavaScript is single-threaded — by offloading operations to the system kernel whenever possible.

Since most modern kernels are multi-threaded, they can handle multiple operations executing in the background. When one of these operations completes, the kernel tells Node.js so that the appropriate callback may be added to the poll queue to eventually be executed.

The event loop looks like this :

   ┌───────────────────────────┐
┌─>│           timers          │
│  └─────────────┬─────────────┘
│  ┌─────────────┴─────────────┐
│  │     pending callbacks     │
│  └─────────────┬─────────────┘
│  ┌─────────────┴─────────────┐
│  │       idle, prepare       │
│  └─────────────┬─────────────┘      ┌───────────────┐
│  ┌─────────────┴─────────────┐      │   incoming:   │
│  │           poll            │<─────┤  connections, │
│  └─────────────┬─────────────┘      │   data, etc.  │
│  ┌─────────────┴─────────────┐      └───────────────┘
│  │           check           │
│  └─────────────┬─────────────┘
│  ┌─────────────┴─────────────┐
└──┤      close callbacks      │
   └───────────────────────────┘

Where each phase is described as :

  • timers: this phase executes callbacks scheduled by setTimeout() and setInterval().
  • pending callbacks: executes I/O callbacks deferred to the next loop iteration.
  • idle, prepare: only used internally.
  • poll: retrieve new I/O events; execute I/O related callbacks (almost all with the exception of close callbacks, the ones scheduled by timers, and setImmediate()); node will block here when appropriate.
  • check: setImmediate() callbacks are invoked here.
  • close callbacks: some close callbacks, e.g. socket.on('close', ...).

More details here.

In short, while Node's event loop is single-threaded, it loops as fast as it can through these phases, and leaves other processes (e.g. services, external programs, etc., typically accessed through sockets, pipes, etc.) and I/O operations to execute and provide responses through the pull and callback phases.

  • Related