Node.js Architecture : simple and clear explanation

Node.js Architecture : simple and clear explanation

What is Node.js ?

  • Node.js is a powerful, open-source, cross-platform runtime environment that allows you to run JavaScript on the server side.

  • It is built on the V8 JavaScript engine (the same engine that powers Google Chrome).

  • V8 compiles JavaScript to machine code, and is designed to be lightweight and efficient, especially for building fast, scalable network applications.

    Node.js is used widely by the developers around the globe from a startup to a big established companies.

Things you should know before understanding node.js architecture :

  1. Client : User interacts with the frontend part of application to perform requests.

  2. Server : Computation machine which serves the requests by performing the desired processes and give response.

  3. Database : Stores the data recieved or created during interaction with the user durinf request/response cycle.

  4. Blocking operations(Synchronous operations) : that cause the program or thread to halt its execution until the operation is complete. During this time, no other task can proceed or execute, as the program is "blocked" waiting for the operation to finish.

    Types :

    1. I/O operations

    2. User input

    3. Long-running calculations

    4. Synchronous System calls

  5. Non-blocking operations(Asynchronous operations) : that do not halt/block the execution of other tasks while they are running.

    when a non-blocking operation is initiated, the program doesn't wait for the task to finish. Instead, it can continue executing other operations or tasks, improving concurrency and responsiveness.

Components of Node.js Architecture :

  1. Nodejs Server : It is a server-side platforms that respond to the users requests by processing the respective operations.

  2. Requests : These are the requests made by the client, it can be of two types blocking(Synchronous) or Non-blocking(Asynchronous).

  3. Event queue : It is the queue in which the incoming requests come and gets store and then wait for their turn to execute in the event loop.

  4. Thread pool : The thread pool is part of libuv, the underlying C library Node.js uses to handle asynchronous I/O. This thread pool consists of a set of worker threads (by default 4 worker threads) that handle blocking operations in the background, allowing the main thread (the event loop) to remain unblocked.

    It performs various tasks :

    1. Handles blocking operations(such as file system I/O, DNS lookups, and some other system-level tasks) and after they gets completes,

    2. It creates a callback that goes back into the event queue. Then when the event loop is ready, it picks up this callback from the event queue and executes it.

  5. Event loop : this is the loop that has two main responsibility :

    1. Check if the current process is blocking or Non-blocking, if it is blocking then it will give this process to the Thread Pool to assign a worker thread to this task.

    2. Indefinitely recieves the requests and processes them and then returns the responses to corresponding clients.

  6. A very short intro to libuv :

    libuv is a multi-platform C library that provides asynchronous I/O operations through an event loop pattern. It was originally developed for Node.js but has since become a standalone project used in many other applications.

    Here are the key aspects of libuv:

    1. Event Loop: At its core, libuv implements an event-driven asynchronous I/O model. The event loop handles various types of events like file operations, network operations, and timers.

    2. Key Features:

  • Full-featured event loop backed by epoll/kqueue/IOCP/event ports

  • Asynchronous TCP and UDP sockets

  • Asynchronous DNS resolution

  • Asynchronous file and file system operations

  • File system events

  • Thread pool

  • Child processes handling

  • Thread synchronization primitives

  • inter process communication

Execution flow of Node.js :

This diagram explains the whole exection flow from request to response.

Explanation :

When a client sends requests to a Node.js server, the request first enters through a gateway and is immediately queued in the Event Queue. From there, the Event Loop takes over, analyzing each request to determine its processing requirements.

If the request involves non-blocking operations, the Event Loop handles it directly in the main thread, processes it, and promptly returns the response to the client. However, for blocking operations, the process becomes more intricate. The Event Loop delegates these requests to the Thread Pool, where an available thread picks up the task for processing.

Once a blocking operation completes, the system creates a callback containing the result. This callback is then placed in the Event Queue while the thread that processed it returns to the Thread Pool for future tasks. When the Event Loop cycles back to these callbacks in the queue, it executes them and sends the corresponding responses back to the clients.

This sophisticated architecture allows Node.js to efficiently manage both blocking and non-blocking operations, ensuring optimal performance and resource utilization while handling multiple concurrent requests.

Explaining with simple analogy :

  1. Request Arrives

    1. Think of it like a customer arriving at a restaurant

    2. Goes into a waiting line (Event Queue)

  2. Event Loop Checks

    1. Like a waiter who looks at each order

    2. Decides if it's a quick task or a long task

  3. Quick Tasks (Non-blocking)

    1. Waiter handles it right away

    2. Like serving water or taking an order

    3. Customer gets quick response

  4. Long Tasks (Blocking)

    1. Waiter sends it to the kitchen (Thread Pool)

    2. Like cooking a meal that takes time

    3. A cook (thread) takes the task and works on it

  5. Task Completion

    1. When cook finishes, writes down "order ready" note (callback)

    2. Note goes back to waiting line (Event Queue)

    3. Cook is free to take new orders

  6. Final Step

    1. Waiter picks up the "order ready" note

    2. Serves the completed order to customer

    3. Task complete!

That's Node.js in a nutshell - one waiter (Event Loop) managing everything, sending big tasks to the kitchen (Thread Pool), and making sure everyone gets served efficiently!

This article tells about the whole article in simple words. for deep understanding and under the hood operations like Node I/O, Network I/O and so on , Stay tuned… for next article.