NodeJs architecture explained!

Vivek Singh
5 min readJul 11, 2021

--

Node.js is a server-side platform built on Google Chrome’s JavaScript Engine (V8 Engine). It is an event-driven single threaded, non-blocking IO, asynchronous programming language!

Check this video to understand the above architecture :

The video is pretty good, or I wouldn’t have had suggested!

You can skip it, or come back later to this video once you have gone through the basics.

Event loop -

The event loop allows Node.js to perform non-blocking I/O operations despite the fact that JavaScript is single-threaded. It is done by assigning operations to the operating system whenever and wherever possible.

Event loop is an endless loop which picks tasks from the event queue to be processed. There can be multiple event loops, however there will be only 1 global thread pool library provided by libuv. Read through the article, to understand the concept completely.

Single Threaded -

When we talk about programming languages like Java or C# — a new thread is created for every request that is made to the server. For nodeJs, only a single thread is responsible to work on all the tasks that a node application has! However there’s a catch, stay tuned.
Please do not assume that nodeJs as it uses single thread can be a little slow when compared to Java or C#, because it is simply not true!

All that being said, nodeJs uses a thread pool as well with default capacity of thread being 4 which can be increased. This thread pool belongs to libuv library.
libuv : libuv is a C library originally written for Node.js to abstract non-blocking I/O operations. It provides mechanisms to handle file systems, DNS, network, child processes, pipes, signal handling, polling and streaming. It includes a thread pool for offloading work for some things that can’t be done asynchronously at the operating system level. The thread pool is global and shared across all event loops.

Use this environment variable ‘ UV_THREADPOOL_SIZE ’ to change the number of threads. The absolute maximum is 1024.

Take for an example, you have 4 different API/endpoint requests and a simple addition computation to be done. So we have 5 tasks in total. The endpoint takes time to return the response. Considering NodeJS uses a single thread, these tasks will take a relatively longer time, had it not been for libuv to help out! Any request which takes time to return the response (termed async operations), is sent to the libuv thread pool or to the OS. So all 4 endpoint calls are handled by libuv and the last task of ‘addition’ is executed by NodeJS without waiting for response from endpoint calls . Once the endpoints are completed, the callbacks are sent back to the event queue, for execution. So although nodeJS uses a single thread initially, the libuv library provides a thread pool for the tasks which needs time to be executed.

non blocking I/O -

Input-output (I/O) systems transfer information between computer’s main memory and the outside world. In our example above, endpoint requests takes time to compute completely, but that doesn’t stop NodeJS from executing other tasks. This is what non-blocking IO means, that computation of other parts of the application would not stop even if there’s an IO call.

Asynchronous programming -

This explains itself . If NodeJS didn’t have a libuv library, basically meaning a thread pool to work on time taking tasks(async tasks), then nodeJs would have been a synchronous language. However nodeJs is asynchronous, so multiple tasks can run at any time without any blocking operations. OS helps too in running multiple tasks simultaneously.

The above picture just shows dependencies of NodeJS, so that it can work easily the way it works : https://nodejs.org/en/docs/meta/topics/dependencies/

Let’s look at an example using screenshots and 2 line explanations for every screenshot :

In the below picture, we have :

Left side on top : A simple program
Console : Tracks the statements which is printed
Stack : memory in RAM, which keeps track of all function calls in LIFO order
Task queue : Keeps track of all tasks to be done by the server.
Event loop : The infinite loop which is single threaded and core of nodeJS architecture

Webapis : the async tasks are done by libuv or OS or node itself, it depends on the task.

main() in the stack is like the starting point in nodeJs.

console.log(‘Hi’) — simple statement which can be executed instantaneously. It goes to a stack and computes, which prints Hi on the console.

After that setTimeout(ch) is called, with ‘ch’ as the callback.

Node realizes that it is a time taking async operation, so it can’t be processed and waiting for it would make the system slow, because nodeJS is single threaded. Hence it sends the task to libuv, and internally creates one more event loop so that the completion can be tracked, and once completed would return the callback to event/task queue.

Until then, because of async non blocking IO nature of nodeJs, console.log(‘JSConfEU’) is pushed to stack and computed, which results in printing ‘‘JSConfEU’ to the console.

Now the timer task is complete, it had an internal event loop which kept track of it’s completion. So, once it is complete, the event loop sends the callback to the task queue, where our main event loop is working.

Now, as the stack is empty , so the callback task ‘cb’ is pushed to stack for execution, and it executes.

Final output/screenshot :

We are done with the article on nodeJs architecture and it’s working. The architecture is a bit confusing for sure! I know the more articles one reads, the deeper one dives — the more questions we have about our previous understandings. It is difficult to fully understand a system, however that is what will keep you on the top. That being said, you can go through this video as well on nodeJs, I myself found it interesting :

Please reach out to me in case you are interested to connect and develop something exciting!

--

--

Vivek Singh
Vivek Singh

Written by Vivek Singh

Software Developer. I write about Full Stack, NLP and Blockchain. Buy me a coffee - buymeacoffee.com/viveksinless

No responses yet