All About Node.js Architecture – Working, Components & Advantages

All About Node.js Architecture – Working, Components & Advantages

Hello, readers! If you’re a backend developer or aspire to be one then you most probably use node js frequently on the backend while developing a web application. But do you know how Node.js works internally? Well in this blog we will learn about Node js Architecture, we have covered it all, starting from basic architecture to answering most of the frequently asked questions so let’s get started.  

The architecture of Node.js

Node js is written in JavaScript and its core part is written in C and C++. 

Node js architecture is built on “Single Threaded Event Loop” to handle multiple concurrent incoming clients requests. Its processing model is based on a JavaScript event-based model with a JavaScript callback mechanism alongside. 

It uses two concepts in its architecture, the Asynchronous model which is similar to that in JavaScript’s event-based model, and Non-blocking of I/O operations. 

Let’s deep dive into the architecture of Node js and its working

Different components of Node.js architecture:

  • Requests– Requests are of two types, incoming and outgoing. Incoming requests can either be complex which means blocking or it can be simple which corresponds to non-blocking. It relies on the task which is to be performed by the users in the web application or software.
  •  Node js Server– It is a server-side platform that takes requests from users after which it processes those requests and sends those responses to corresponding users.
  • Event Queue– This is where the incoming client requests get stored and are then passed onto the Event loop one by one.
  • Event loop– It is an indefinite loop where it receives requests, processes them, and returns the corresponding response to the clients.
  • Thread pool– It is a pool of all the threads available in node js for carrying out tasks required to fulfill clients’ requests.
  • External resources– These are resources used for blocking clients’ requests, computation, data storage, etc.

Working:

Here are the following steps of Node js architecture workflow 

  • Requests are of two types, incoming and outgoing. Incoming requests can either be complex which means blocking or it can be simple which corresponds to non-blocking. It relies on the task which is to be performed by the users in the web application or software.
    • Querying for particular data refers to searching for particular data in a database
    • Deleting data which refers to sending a request to delete a particular data or performing a deleting query.
    • Updating data means sending a request to change/update a particular set of data or performing an updating query on a particular row of tables to update a specific entry in the database.
  • Node js retrieves the incoming clients’ requests and adds those requests to the Event Queue of node js.
  • The incoming requests are then passed onto Event Loop one by one after which it checks if that request requires external resources or not. If it does then those requests get allocated with external resources else it moves onto the next step.
  • In Event Loop, non-blocking (simple) requests are processed like I/O polling and return those requests (response) to respective clients.
Node js architecture

After the above steps, a single thread from the thread pool is assigned to a complex request, one at a time. This thread is the one that is responsible for completing specific blocking requests by allocating external resources like databases, file systems, etc.

Once the task is finished, the response is sent back to the Event Loop which in turn sends the response back to the client making it a single-threaded event loop model. 

How does single-thread work?

Node.js is a javascript runtime built on Chrome’s V8 javascript engine which is single-threaded. Whenever there’s a client’s request, it is handled by a single main thread. The primary component “event loop” allows Node.js to run I/O operation from blocking to non-blocking manner. It continuously monitors and keeps a track of all the asynchronous tasks, for instance, the code in the callback function, after completion of the task it is sent back to the execution queue. Everything is being operated in a single main thread. 

Fun fact, even though there’s just one main thread for all the executions, there are tons of auxiliary threads in the kernel system that node js uses for its operations. All these groups of threads working together are known as the worker pool which is implemented in Libuv and manages multiple threads according to the requirements.  

The event loop takes care of primary processing but since it uses async I/O operations, it includes modules like fs (file system) which is I/O heavy, and CPU-heavy. These threads synchronously run the operation and return output to the corresponding event loop. The Event Loop continues to work as usual and concurrently cater to any requests which come in queue.  The threads return their output to the event loop which the task is completed after which is either returned to the client or the event queue for other tasks. 

The main reason for using asynchronous I/O processing is to never block the program. In synchronous I/O processing, the request waits in a queue until the requests before it gets finished which means that it has to wait for its turn to come. On the other hand, in an asynchronous process, the thread does not wait for other operations to finish, instead, it runs in the background and is called when finished executing. This asynchronous I/O operation helps more CPU time available for other operations. 

Libuv, an open-source library gives Node js access to the operating system, networking system, and file system. It is written in C++ and holds two important features of node js architecture; those are event loop and thread pool.  The event loop is mainly used for smaller tasks while the thread pool is used for handling larger/heavy tasks. 

Now that we’ve got a glimpse of Node js architecture, let’s deep dive into these two main features, event loop, and thread pool. 

Event Loop:  The thread, which is a sequence of instructions, is being executed on the event loop. For instance, if we have 10 different tasks then all of them will use just a single thread instead of a different thread for different tasks, which means no matter how many users are using your platform/application, it will use only a single thread for accessing your platform. This is the reason why it is also known as the heart of Node js. It mainly performs less complex tasks. 

Thread pool: The thread pool consists of a bunch of threads up to 128 but only 4 of them are being used. These 4 threads along with the main one form a thread pool. The event loop offloads main tasks like password, chacing, deleting files, network DNS tasks, etc. This offloading of tasks is being done automatically by node js. The Node js decides which thread is to be assigned to which task. 

Note: Thread pool is only used when such heavy tasks are encountered by Node js. If not, then the event loop still keeps functioning the same way. 

Libraries used in Node js architecture: The most important libraries used in node js are Libuv and V8 but there are many more libraries that are being used in its architecture, to name a few, HTTP parser for parsing HTTP URLs, Open SSL for cryptography, c-ares for DNS queries, Z-lib for file compression. All of these libraries combined make Node js architecture perfect for being used at the backend of our application.  

Why does Node.js use “Single-threaded event loop model” architecture?

Fun fact, Node js initially used to use asynchronous processing on a single thread as this could provide more scalability as it wasn’t CPU intensive. But this single threading asynchronous process made things complex in Node js, threading took a lot of time in processing, designing of web applications and deadlock became complicated. As a result of all these issues, it was then converted to only a “single-threaded event loop” which resolved all the issues making it scalable. It was inspired by JavaScript’s event-based model with JavaScript callback mechanism which implies that network calls, DNS all these events asynchronously take place.

Event-driven architecture:  You might be amazed to know that some of Node js core modules like HTTP and file systems are also based around event-driven architecture. To make it simple remember the two terms, emitter and listener. The emitter emits events’ names as soon as some important task comes up like any request coming to the server of a task related to the file system. These events are then picked by the event listener and the callback function is being executed which is written by a developer. 

Advantages of Node.js Architecture

Node js server-side language gives the upper hand when compared to other server-side languages. The feature of the asynchronous model and that of Non-blocking I/O operation enhance the scalability and performance of Node js when integrated for other frameworks while building a web application

  1. Handling multiple client requests with ease.
  2. No multiple threads are needed. 
  3. Lesser memory and resources are required.
  4. Easily scalable 
  5. High Performance 
  6. Flexible with multiple frameworks 
  7. Easier development process

Conclusion

Node js is popular because of its architecture which is “Single-Threaded-Event-Loop” and is been used by almost all the applications as it provides flexible backend support and features.  Although it might be tedious to learn at the start it’s very easy once you get the hang of it. Do check out this free course by codedamn on Node js.

 

Sharing is caring

Did you like what mansi wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far