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
  • How I wish I had discovered this repository earlier. github/opensource.guide is a place that guides everyone on everything about Open Source. From how to contribute code, how to start your own open-source project, to the knowledge that anyone should know when stepping into this field 🤓

    Especially, this content is directly from Github.

    » Read more
  • Just the other day, I mentioned dokploy.com and today I came across coolify.io - another open-source project that can replace Heroku/Netlify/Vercel.

    From what I've read, Coolify operates based on Docker deployment, which allows it to run most applications.

    Coolify offers an interface and features that make application deployment simpler and easier.

    Could this be the trend for application deployment in the future? 🤔

    » Read more
  • One of the things I really like about command lines is their 'pipeline' nature. You can imagine each command as a pipe; when connected together, they create a flow of data. The output of one pipe becomes the input of another... and so on.

    In terms of application, there are many examples; you can refer to the article Practical Data Processing Using Commands on MTTQVN Statement File. By combining commands, we turn them into powerful data analysis tools.

    Recently, I combined the wrangler command with jq to make it easier to view logs from the worker. wrangler is Cloudflare's command line interface (CLI) that integrates many features. One of them helps us view logs from Worker using the command:

    $ wrangler tail --config /path/to/wrangler.toml --format json

    However, the logs from the above command contain a lot of extraneous information, spilling over the screen, while we only want to see a few important fields. So, what should we do?

    Let’s combine it with jq. jq is a very powerful JSON processing command. It makes working with JSON data in the terminal much easier. Therefore, to filter information from the logs, it’s quite simple:

    $ wrangler tail --config /path/to/wrangler.toml --format json | jq '{method: .event.request.method, url: .event.request.url, logs }'

    The above command returns structured JSON logs consisting of only 3 fields: method, url, and logs 🔥

    » 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...
Scroll or click to go to the next page