Node.js Architecture

Level : Beginner
Mentor: Shailendra Chauhan
Duration : 00:04:00

Node.js Architecture

The Node.js architecture uses an event-driven, non-blocking I/O model, which allows for fast handling of concurrent operations via its event loop and single-threaded runtime environment.

Node.js API

These are written in JavaScript and are immediately accessible to the outside world, allowing users to interact with Node.js internal components.

Node.js Binding

Node.js Binding is a Core API that links JavaScript to C/C++ libraries.

C/C++ Add-ons

You may create Node.js Add-ons in C/C++ to operate with Node.js.

V8

V8 is Google's open-source JavaScript engine, written in C++. It is a JavaScript virtual machine (VM) that compiles JavaScript code into native machine code rather than interpretation. It is the quickest JIT (Just-In-Time) compiler for JavaScript.

Libuv

Libuv is a multi-platform C++ library that handles Node.js thread pools, event loops, and async I/O operations. In Node.js, blocking I/O operations are delegated to LibUV modules, which use a fixed-size C++ thread pool to handle them. When these operations are complete, they are reported to the Event loop.

C-ares

C-ares is a C library that can handle async DNS requests, name resolutions, and multiple DNS searches in parallel.

http_parser

This is a C library that parses HTTP requests and responses.

OpenSSL

OpenSSL is a C library that implements Secure Sockets Layer (SSL v2/v3) and Transport Layer Security (TLS v1) protocols. It also includes all of the necessary cryptographic procedures such as hashing, ciphering, deciphering, signing, and verifying.

Zlib

Zlib is a C library that handles data compression and decompression.

Components of Node.js Architecture

Requests

Incoming requests might be either blocking (complicated) or non-blocking (simple) based on the actions that a user wants to accomplish in a web application.

Node.js Server

The Node.js server is a server-side platform that accepts user requests, processes those requests, and returns results to the users.

Event Queue

The Event Queue in a Node.js server stores incoming client requests and passes them one by one into the Event Loop. The event loop contains six phases, which are:

  • Timers
  • I/O Callbacks
  • Waiting / Preparation
  • I/O Polling
  • setImmediate() callbacks
  • Close events

Thread pool

The thread pool comprises all the threads available for carrying out some tasks that may be necessary to fulfill client requests.

Event loop

The Event Loop receives and processes requests indefinitely before returning responses to the relevant clients.

External Resources

To deal with blocked client requests, additional resources are required. These resources may be used for processing, data storage, or other purposes.

Workflow of Node.js Architecture

  • Clients submit requests to the web server.
  • These inquiries may be blocking (complicated) or non-blocking (simple).
  • The queries could be to query for data, delete data, or update it.
  • The requests are retrieved and routed to the event queue.
  • The requests are then sent from the event queue to the event loop one at a time.
  • The main thread handles simple requests and returns the response to the client.
  • A complex request is sent to a thread in the thread pool.

Advantages of Node.js Architecture

  • Scalability: Ability to handle several concurrent connections efficiently.
  • Non-blocking I/O: You can handle several requests without waiting for the preceding ones to finish.
  • Single language: JavaScript can be used to build the entire stack, which reduces context switching.
  • Rich ecosystem: Huge library support and an active community.
  • Real-time applications: Useful for developing real-time online applications such as chats and games.

Disadvantages of Node.js Architecture

  • Callback hell: The asynchronous nature might result in complicated nested callbacks.
  • Limited CPU-intensive tasks: Because it is single-threaded, it is unsuitable for CPU-bound processes.
  • Learning curve: Comprehend event-driven programming principles.
  • Dependency management: Excessive use of third-party modules might lead to dependency hell.
  • Debugging: Debugging asynchronous code can be difficult.
Self-paced Membership
  • 24+ Video Courses
  • 850+ Hands-On Labs
  • 500+ Quick Notes
  • 225+ Skill Tests
  • 120+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this