Introduction to Node.js Architecture
Node.js architecture is based on the Single-Threaded Event Loop Model will provide us a better understanding of the advantages that Node.js provide, the way it can handle concurrent client’s requests without creating multiple threads and how Node.js uses fewer threads; only to utilize fewer resources. We will look after the Single-threaded nature and how Node.js handles a particular request? How the concepts of event queue and thread pool help in better processing of a request in Node.js with high scalability and performance.
Benefits of Single-Threaded nature
- We can easily handle concurrent client’s requests.
- JS uses fewer threads so that it can use fewer resources. When a Node.JS web server receives concurrent requests, it need not create multiple threads because of the Event loop.
Architecture of Nodejs
Let us go through the following two scenarios which will help us understand the architecture of Node.js:
- The client sends a request to the server.
- The request is registered in the Event Loop.
- The Single Thread Event Loop is what allows the node to execute non-blocking input-output operations.
- The incoming request is examined to determine whether it includes any interaction with a database or heavy computations.
- As the above request does not contain any complex operations, the request is prepared and the response is then sent back to the client.
Diagram Explanation for Scenario #1
- In Scenario 1, the client sends the request(a) to the server.
- Request(a) is registered in the Event Loop.
- The Event Loop then checks if the request(a) contains any complex operations or any interaction with the database.
- As the incoming request(a) does not contain any heavy computations, the Node js starts processing the request(a), prepares the response and sends it back to the client.
- As seen above, the Client sends the request to the server.
- The incoming request is registered in the Event Loop.
- The Event Loop then checks if the incoming request contains any complex operations, database queries or any kind of interaction with the file system.
- If the incoming request does contain interaction with the database, the Event Loop checks with the Internal Thread Pool which is present in Nodejs Server.
- The Internal thread pool of Nodejs Server consists of threads which is responsible to execute the kind of request that contains interaction with the database or file system.
- A thread is a path of execution within a process and also known as a lightweight process.
- The Event Loop checks if the thread is available to execute the request in the Internal Thread Pool.
- If the thread is available, the request is assigned to that thread.
- The assigned thread is then responsible to execute the request, prepare its response and give it to the Event Loop.
- The Event Loop then finally returns the response back to the client.
Diagram Explanation for Scenario #2
- In Scenario 2, the client sends the request(b) to the server.
- Request(b) is registered in the Event Loop.
- The Event Loop then checks if the request(b) contains any interaction with a database or any complex operations to be performed.
- As the incoming request(b) does contain heavy computations, that is interaction with the database; the Node js checks for the availability of threads in the Internal thread Pool.
- As the thread is found available to process the request(b), the request(b) is assigned to the thread.
- The thread is then responsible to process that request, prepare its response and send it back to the client.
Node.js gives us the privilege to build highly scalable web applications with the help of frameworks. The popular frameworks available for Node.js are Express.js, Sails.js, Meteor.js, Total.js and many more. The frameworks provide us a way to build the applications in an effortless way. The non-blocking I/O model (asynchronous programming) and the event-driven nature make Node.js efficient and lightweight.
Advantages of Node.js
- Single-threaded nature
- Asynchronous programming (non-blocking I/O operations)
- No buffering
- Node Package Manager (NPM)
- Advantage of Caching
- Open Source
- Highly Extensible
Conclusion – Nodejs Architecture
Thus we studied Node.js architecture which is based on the Single-Threaded Event Loop Model. It helped us understand how a particular request is handled through the concept of Event Queue and Thread Pool; how a request that contains any blocking input-output operations is placed in the event queue and a thread is assigned to carry the request forward and send back the response to the client once the response is prepared.
We also looked through the concept of Node.js Platform, the popular frameworks available to build data-intensive, highly scalable Node.js applications, and its advantages.
This is a guide to Nodejs Architecture. Here we discuss the Single-threaded nature and how Node.js handles a particular request. You may also have a look at the following articles to learn more –