Node.js Architecture (Part 3)

Node.js Architecture (Part 3)

Node.js utilizes the Event Loop to handle asynchronous I/O tasks. Do you truly understand how the Event Loop operates?

Phases of the Event Loop

Previously, it was mentioned that the Event Loop monitors whether the Call Stack is empty and if the Event Queue contains any functions waiting to be executed, it puts them back into the Call Stack. This process is carried out through phases, as depicted in the following diagram:

Các pha (Phases) của Event Loop

  • Timers: executes scheduled callback functions with setTimeout and setInterval.
  • I/O callbacks: performs most callback operations except close callbacks, timers callbacks, and setImmediate().
  • Idle, prepare: used for internal node.js processing.
  • Poll: retrieves new I/O events, accepts incoming connections, and processes data.
  • Check: handles setImmediate callback functions.
  • Close callbacks: executes callback functions for "close" events. For example, socket.on("close").

This proves that the phases of the Event Loop are executed by the main thread. Each phase represents how the Event Loop determines which pending callback functions to put into the Call Stack for execution.

The phases of the Event Loop also explain the execution order of certain functions in node.js, such as setTimeout(cb, 0), setImmediate(), and process.nextTick(cb), which we will delve deeper into in a future article.

Recap

In summary, node.js has two crucial building components:

Libuv - Cross-platform Asynchronous I/O

libuv is a cross-platform library that focuses on asynchronous I/O. It is primarily developed for use by node.js.

Libuv includes an Event Loop, asynchronous TCP/UDP, asynchronous file handling, thread pooling, child processes, and their independent handling.

V8 JavaScript Engine

V8 is an open-source project from Google, written in C++. It is used in Chrome and Node.js.

It implements ECMAScript and WebAssembly, while running on Windows 7 and above, macOS 10.12+, and Linux.

V8 contains heap memory allocation capabilities, Call Stack execution, garbage collector, optimizing compiler, and JavaScript interpreter.

Node.js utilizes the V8 JavaScript Engine and is therefore called a V8 embedder. As per the requirements of the V8 Engine, the embedder must implement an event loop. Node.js has chosen libuv to implement the loop. This is where V8 and libuv are connected through C++ bindings.

As a single-threaded environment, node.js has only one event loop. Between each run of the event loop, node.js checks for any pending asynchronous I/O tasks and exits if there are no events. Essentially, the event loop does not create an instantaneous response until there is a waiting callback to be processed. Ultimately, a node.js application ends when there are no events in either the Call Stack or Event Queue.

Single-Threaded Concurrent Model

To summarize the concurrency model in a single thread, the Event Loop and Call Stack are used to build the asynchronous I/O model as follows:

  • The node.js application is executed by node.js.
  • CPU-bound functions (high CPU usage) are synchronously executed on the main thread.
  • If the function is an asynchronous I/O, it is pushed to the thread pool for asynchronous execution while the main thread continues execution.
  • After the asynchronous function completes, the thread pool pushes the callback function and event into the Event Queue.
  • The event loop utilizes the main thread to monitor any callback functions waiting to be processed in the Event Queue if the Call Stack is empty. It puts the callback functions back into the Call Stack for execution if the Call Stack is empty.

When to Use Node.js?

Node.js simply provides an asynchronous I/O model, unblocked even with a single thread. This makes it more suitable for deep I/O applications. CPU-intensive applications will not be suitable for node.js as it would block the execution on that single thread.

Due to the usage of the JavaScript programming language in node.js, developers can create full Application Stacks solely using JavaScript.

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)