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

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

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.

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

Hello, my name is Hoai - a developer who tells stories through writing ✍️ and creating products 🚀. With many years of programming experience, I have contributed to various products that bring value to users at my workplace as well as to myself. My hobbies include reading, writing, and researching... I created this blog with the mission of delivering quality articles to the readers of 2coffee.dev.Follow me through these channels LinkedIn, Facebook, Instagram, Telegram.

Did you find this article helpful?
NoYes

Comments (0)