Node.js Event Loop: Not So Single-Threaded

Node.js, Event Loop and Multi-ThreadingYou all know this or a similar sentence: Node.js is a single-threaded non-blocking asynchronous concurrent runtime environment.Photo by Joshua Aragon on UnsplashBut what if I tell you, actually it’s not?Surprised?…


This content originally appeared on Bits and Pieces - Medium and was authored by Kevin Vogel

Node.js, Event Loop and Multi-Threading

You all know this or a similar sentence: Node.js is a single-threaded non-blocking asynchronous concurrent runtime environment.

Photo by Joshua Aragon on Unsplash

But what if I tell you, actually it’s not?

Surprised? Well, that’s where people might argue about, and that’s great. But in fact, it’s possible to run your JavaScript/TypeScript code in multiple threads, and actually, it does by itself because of Node.js - or being more accurate - because of Libuv.

What is Libuv?

Experienced Node.js developers most likely know what Libuv is, but let me explain it anyway.

Libuv is a library completely written in C. It focuses on asynchronous I/O operations such as file system, network, crypto, etc. It brings the Event Pool, Thread Pool, and Child Processes into Node.js. Libuv creates a pool with 4 threads (Thread Pool) that are only used if no async API is available.

So, as you can see, Libuv makes multi-threading already possible out of the box. But in order to fully understand the workflow of Node.js, let me tell you more about what happens in the background of your Node.js applications.

What actually is Node.js?

Node.js is an event-driven JavaScript runtime, based on Google’s V8 JavaScript engine, which is also running in the background of Google Chrome. V8 compiles JavaScript code into machine code that a computer can actually understand. As we know now, Libuv is written in C while the V8 JavaScript engine is mostly written in C++ with a little bit of JavaScript.

Because Node.js is built on top of the V8 engine, it’s possible to use JavaScript outside the browser. That wasn’t really possible before Node.js has arrived. In earlier days, JavaScript was only used inside a browser to make websites a little bit more dynamic back then. In other words, JavaScript was a frontend-only programming language.

Event Loop

As we know the Event Loop is shipped by the Libuv and it’s the heart of the Node.js system architecture. Because the Event Loop is running after the top-level code, it’s possible to run a Node.js application w/o the Event Loop by not having any callbacks, but mostly that’s not the case.

Event Loop Phases

The Event Loop is, as the term says, a loop of events that handles callbacks in JavaScript. The concept is very simple. There are four primary phases responsible for macrotasks and two secondary phases responsible for microtasks.

Macrotasks

  1. Expired timers callbacks (setTimeout, setInterval)
  2. I/O polling and its callbacks (file system, HTTP requests)
  3. Immediate callbacks (setImmediate)
  4. Close callbacks (files, server listeners, processes)

The Event Loop will jump from phase 4 to phase 1 if your Node.js application is waiting for callbacks or if you listening to an HTTP server.

Microtasks

Microtasks are getting executed between each primary phase of the Event Loop in the following order:

  1. process.nextTick() (not recommended to use)
  2. Promises

How to use multithreading on purpose?

There are three ways of using multiple threads in your Node.js application.

  • Child Processes (1 thread each process)
  • Clustering
  • Worker Threads (preferred) (same process, multiple threads)

Do you want to hear more about this? Check out this great video from Bryan Hughes (former Software Engineer at Microsoft) talking about Node.js and why it’s not as single-threaded as we might think.

Thank you for reading my second article on Medium. I hope I could help to refresh your knowledge about Node.js.

Cheers!

Unlock 10x development with independent components

Building monolithic apps means all your code is internal and is not useful anywhere else. It just serves this one project. And as you scale to more code and people, development becomes slow and painful as everyone works in one codebase and on the same version.

But what if you build independent components first, and then use them to build any number of projects? You could accelerate and scale modern development 10x.

OSS Tools like Bit offer a powerful developer experience for building independent components and composing modular applications. Many teams start by building their Design Systems or Micro Frontends, through independent components. Give it a try →

An independent product component: watch the auto-generated dependency graph

Learn more

Microservices are Dead — Long Live Miniservices


Node.js Event Loop: Not So Single-Threaded was originally published in Bits and Pieces on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Bits and Pieces - Medium and was authored by Kevin Vogel


Print Share Comment Cite Upload Translate Updates
APA

Kevin Vogel | Sciencx (2022-02-17T12:23:24+00:00) Node.js Event Loop: Not So Single-Threaded. Retrieved from https://www.scien.cx/2022/02/17/node-js-event-loop-not-so-single-threaded/

MLA
" » Node.js Event Loop: Not So Single-Threaded." Kevin Vogel | Sciencx - Thursday February 17, 2022, https://www.scien.cx/2022/02/17/node-js-event-loop-not-so-single-threaded/
HARVARD
Kevin Vogel | Sciencx Thursday February 17, 2022 » Node.js Event Loop: Not So Single-Threaded., viewed ,<https://www.scien.cx/2022/02/17/node-js-event-loop-not-so-single-threaded/>
VANCOUVER
Kevin Vogel | Sciencx - » Node.js Event Loop: Not So Single-Threaded. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/02/17/node-js-event-loop-not-so-single-threaded/
CHICAGO
" » Node.js Event Loop: Not So Single-Threaded." Kevin Vogel | Sciencx - Accessed . https://www.scien.cx/2022/02/17/node-js-event-loop-not-so-single-threaded/
IEEE
" » Node.js Event Loop: Not So Single-Threaded." Kevin Vogel | Sciencx [Online]. Available: https://www.scien.cx/2022/02/17/node-js-event-loop-not-so-single-threaded/. [Accessed: ]
rf:citation
» Node.js Event Loop: Not So Single-Threaded | Kevin Vogel | Sciencx | https://www.scien.cx/2022/02/17/node-js-event-loop-not-so-single-threaded/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.