Nodejs Architecture

Udara Abeythilake
3 min readSep 1, 2022

--

Node.js is an open-source, JavaScript run-time environment designed to run scalable applications. Node.js allows developers to write code in JavaScript for server-side scripting. Also, Node.js has an event-driven architecture capable of asynchronous I/O. This environment was built on Google Chrome’s V8 Engine which is used to develop I/O intensive web applications like video streaming, single page applications, online chat applications, and other web pages. In this article, I’m going to discuss Node.js architecture and go down to the more minor component inside Node.js.

We can categorize Node.js architecture into two main components as V8 engine and LIBUV. Let’s see each component's functionalities separately.

V8 Engine

The V8 engine is the fundamental part of Node.js architecture. Without V8 engine there is no way to identify JavaScript. V8 engine helps to convert JavaScript code to machine code which is C++ that can be understood by machines.

LIBUV

LIBUV is an open-source library that is strongly focused on asynchronous I/O. This library provides access to Node.js as a computer operating system, file system, and Networking. Below are some features of LIBUV

  • Asynchronous TCP (net module) and UDP (dgram module)
  • Asynchronous DNS resolution (used partly for the DNS module)
  • Asynchronous file, file system operations & events (fs module)
  • ANSI escape code-controlled TTY
  • Thread pool and Signal handling
  • Child processes
  • High-resolution clock
  • Threading and synchronization primitives.
  • Inter-Process Communication using sockets and Unix domain sockets (Windows)

Event Queue, Event Loop, and Thread Pool are the most important components in the LIBUV.

Event Queue

Event queue to the incoming client request and pass them to the event loop in sequentially

Event Loop

The event loop is responsible for handling small tasks such as executing callback functions or network I/O. Those are non-blocking tasks that do not block the main thread. It takes care of all the incoming events and performs the balancing part by offloading heavier tasks into the thread pool and doing the simpler tasks by itself. Below are some features of the event loop

  • Event loop is an endless loop, which waits for tasks, executes them, and then sleeps until it receives more tasks.
  • The event loop executes tasks from the event queue only when the call stack is empty i.e. there is no ongoing task.
  • The event loop allows us to use callbacks and promises.
  • The event loop executes the tasks starting from the oldest first.

Thread Pool

The thread pool gives us 4 separate threads. The event loop will offload the heavy task to the thread pool, which happens automatically. The thread pool is responsible for handling heavy tasks such as

  • File access
  • Cryptography related things
  • Caching password
  • File Compression
  • DNS lookups

Other Libraries

Apart from the above main components following libraries are also used in the Node.js architecture for some other purposes

  • HTTP Parser — parsing HTTP
  • C-ARES — DNS queries
  • OpenSSL — cryptography
  • Zlib — file compression

The flow of Node.js Architecture

  1. The client sends requests to the server. Request can be blocking or non-blocking.
  2. Node.js retrieves the incoming request and adds it to the event queue.
  3. From the event queue, it passes each request to the event loop one by one.
  4. Event loop checks if the request is small enough to execute in itself, otherwise passes the request to the thread loop
  5. When a request is received to the thread pool it executes the request and passes the response again to the event loop.

Advantages of Node.js Architecture

  • Handling multiple concurrent client requests is fast and easy
  • No need to create multiple threads
  • Require fewer resources and memory

--

--