Node.JS Architecture

Photo by Joan Gamell on Unsplash

Node.JS Architecture

Node.js is an open-source, JavaScript run-time environment designed to run scalable applications. NodeJS utilizes both JavaScript and C/C++ to create its core components. NodeJS is a platform that utilizes JavaScript and is primarily employed for developing web applications that are highly focused on input/output operations, including (but not limited to) chat applications and multimedia streaming websites. The platform is constructed using Google Chrome's V8 JavaScript engine. Node.js allows developers to write code in JavaScript for server-side scripting. A web application is a type of software that executes on a server and is displayed by a client's browser that obtains all the application's resources over the internet.

The web application consists of the following components:

  • Client: User who interacts with the server by sending out requests.

  • Server: The server is in charge of receiving client requests, performing tasks and returning results. It serves as a bridge between the front end and the stored data, allowing clients to perform operations on the data.

  • Database: It is the place where a web application's data is stored. Depending on the client's request, the data can be created, modified and deleted.

To manage several concurrent clients, NodeJS employs a "Single Threaded Event Loop" design. The JavaScript event-based model and the JavaScript Callback Mechanism are employed in NodeJS. processing model. It employs two fundamental concepts:

  • Asynchronous model

  • Non-blocking I/O operations

Components

Node.js architecture consists of several components that work together to handle incoming client requests and process responses. These components are:

  1. Requests: Node.js receives two types of requests, incoming and outgoing. Incoming requests can be either complex (blocking) or simple (non-blocking), depending on the task that needs to be performed by the web application or software.

  2. Server: It is a server-side platform that receives client requests, processes them and sends the corresponding responses back to the clients.

  3. Event Queue: Incoming client requests are stored in the event queue and then passed on to the event loop one by one.

  4. Event Loop: It is an indefinite loop where requests are received, processed and responses are returned to the clients. The event loop is a critical component of Node.js architecture, as it allows for non-blocking I/O operations.

  5. Thread Pool: Node.js has a pool of threads available for carrying out tasks required to fulfil client requests. This ensures that multiple requests can be processed simultaneously, improving the performance of Node.js applications. The thread pool is responsible for handling heavy tasks such as

    • File access

    • Cryptography related things

    • Caching password

    • File Compression

    • DNS lookups

  6. External Resources: These are resources used by Node.js for blocking client requests, computation, data storage, etc. Examples of external resources include databases, file systems and network resources. 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

Overall, these components work together to create a scalable, efficient and non-blocking architecture that is ideal for building web applications and software.

V8 Engine

The V8 engine is the fundamental part of Node.js architecture. Without the 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 and UDP

  • Asynchronous file, file system operations & events (fs module)

  • Thread pool and Signal handling

  • 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.

Advantages of NodeJS

Compared to other server-side languages, Node Js has distinct advantages. Its asynchronous model and non-blocking I/O operation improve the scalability and performance of web applications built on other frameworks. Node.js can easily handle multiple client requests without requiring multiple threads, consuming less memory and resources. Additionally, it is highly scalable and provides high performance. Node Js is also flexible with multiple frameworks and makes the development process easier.

  • Handling multiple concurrent client requests is fast and easy

  • No need to create multiple threads

  • Require fewer resources and memory