Node.js Architecture - How does Node.js handle asynchronous tasks?

Node.js Architecture - How does Node.js handle asynchronous tasks?

Daily short news for you
  • Today, I accidentally came across the website notes.andymatuschak.org which has a very interesting note-taking method. Clicking on a link opens a new tab next to it. Each time you click, it continues to open. Just like filing cabinets.

    This presentation style is not only easy to follow but also aligns with the flow of thought. It's a pity that I can't find the author of this open-source project. I wonder if there's anything similar out there 🤔

    » Read more
  • Instagram has just introduced a video editing app for content creators called Edits. This is a direct competition with popular apps on the market. However, it might take some time because looking at the features, they are still quite basic, just enough to use.

    By the way, I've been "playing" on IG for over 10 years now. Anyone with IG, please leave your account for me to check out 🥳.

    My IG is hoaitx_

    » Read more
  • I signed up for Cursor for the second month now and just realized I haven't used all 500 premium requests per month 😳

    Specifically, just over 100 🙏

    » Read more

Problem

In the previous article, we learned about Node.js being single-threaded and the differences between synchronous and asynchronous I/O tasks. In this article, I will talk about how Node.js handles asynchronous tasks.

First, let me review some concepts to give you a clearer understanding before diving into how Node.js handles asynchronous tasks.

What is asynchronous?

If you have worked with other languages like C, PHP, or Java before learning JavaScript, you might be familiar with synchronous programming. Synchronous programming runs the code sequentially, meaning it executes one line of code after another. Let's take an example of reading a file in PHP:

<?php
$content = readfile("file.txt");
echo $content;
?>

In this PHP code, the readfile function reads the content of "file.txt" and immediately echoes it to the screen using the echo statement, because echo is called after readfile has the result. This is synchronous programming.

Now, in JavaScript, a similar file reading function looks like this:

const content = readFile("file.txt");
console.log(content);

The output will not be the content of "file.txt" because readFile is an asynchronous function. It does not get processed immediately. Instead, it delegates this task to another entity (specifically the Thread Pool) and continues executing the console.log function right away. At this point, the content does not have the result of reading the file, so the output will definitely be something else.

You might ask:

Why is asynchronous useful for Node.js?

How does Node.js handle asynchronous tasks?

Let's start by looking at the components in the Node.js architecture:

Thành phần Node.js

The Call Stack is responsible for running JavaScript code in a specific order. Since there is only one Call Stack, only one piece of JavaScript code can be executed at a time. This can cause a bottleneck if the code takes a long time to process.

The Node.js Standard Library contains components that interact with the system, such as files, HTTP requests, DNS resolution, provided by libuv. In other words, these components handle asynchronous I/O tasks.

The Event Loop is simply an infinite loop that always checks if the Call Stack is empty. If it is empty, it moves the callback functions from the Event Queue to the Call Stack in a First-In-First-Out (FIFO) order. The callback functions in the Event Queue are added by the Thread Pool after completing an I/O operation.

In summary, Here is how Node.js handles a JavaScript code snippet with asynchronous code:

When running a JavaScript file, the JavaScript code is pushed into the Call Stack for processing. If it encounters any asynchronous code, it delegates that code to the Thread Pool and continues with the processing. The Thread Pool takes the task from the Call Stack and processes it immediately. Once it has the result, it places the result into the callback function registered in the Event Queue for that I/O operation.

Meanwhile, the Event Loop continuously checks if the Call Stack has finished processing all the JavaScript code in the file. If it is empty, it moves the callback functions from the Event Queue to the Call Stack for further processing.

If there are more asynchronous code snippets in the callback functions, the process continues repeating the steps mentioned above.

We can see that even though Node.js only has one Call Stack for processing JavaScript code, there are multiple Thread Pools for handling asynchronous I/O tasks, which can take a longer time. Node.js leverages the power of V8 for handling JavaScript code execution, so the speed of JavaScript code execution depends entirely on V8, which is known for its "fast" performance.

By default, there are 4 Thread Pools created upon startup, but this number can be increased up to 1024 depending on the server configuration to tune it accordingly.

Premium
Hello

The secret stack of Blog

As a developer, are you curious about the technology secrets or the technical debts of this blog? All secrets will be revealed in the article below. What are you waiting for, click now!

As a developer, are you curious about the technology secrets or the technical debts of this blog? All secrets will be revealed in the article below. What are you waiting for, click now!

View all

Subscribe to receive new article notifications

or
* The summary newsletter is sent every 1-2 weeks, cancel anytime.

Comments (0)

Leave a comment...