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
  • Well, I have officially launched the Store page on the blog 2coffee.dev everyone 🥳

    This is simply a collection of products that I have purchased and found to be good, suitable for their price, and aligned with my usage needs, and I want to share them with you readers. Feel free to check it out for fun. Initially, I don't have much time to edit the content. I will update it gradually. Thank you, everyone.

    » Read more
  • During the weekend, I'm sitting down to set up the store for everyone's relaxation. I did it once before, but it was just for the sake of doing it, and I actually sold a book 😆

    Now I'm doing it again, and there will be more diverse products. I plan to post some products I've bought and used along with a few lines of reviews for everyone to refer to 🤓

    » Read more
  • A short article on how to write blog posts that developers read.

    How to Write Blog Posts that Developers Read

    In summary, get straight to the point and envision the audience you are targeting. Another thing is that the author has over 9 years of writing experience; in the beginning, no one read his work, but persistence helped him reach 300K - 500K readers each year. That's quite an impressive number, isn't it? 🔥

    » 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

5 profound lessons

Every product comes with stories. The success of others is an inspiration for many to follow. 5 lessons learned have changed me forever. How about you? Click now!

Every product comes with stories. The success of others is an inspiration for many to follow. 5 lessons learned have changed me forever. How about you? 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...