Node.js Web Application Architecture
Node.js is a JavaScript-based platform that is mainly used to create I/O-intensive web applications such as chat apps, multimedia streaming sites, etc. It is built on Google Chrome’s V8 JavaScript engine. A web application is software that runs on a server and is rendered by a client browser that accesses all of the application’s resources through the internet.
A typical web application consists of the following components:
- Client: A client refers to the user who interacts with the server by sending out requests.
- Server: The server is in charge of receiving client requests, performing appropriate tasks, and returning results to the clients. It serves as a bridge between the front-end and the stored data, allowing clients to perform operations on the data.
- Database: A database is where a web application’s data is stored. Depending on the client’s request, the data can be created, modified, and deleted.
Node.js Server Architecture: To manage several concurrent clients, Node.js employs a “Single Threaded Event Loop” design. The JavaScript event-based model and the JavaScript callback mechanism are employed in the Node.js Processing Model. It employs two fundamental concepts:
- Asynchronous model
- Non-blocking of I/O operations
These features enhance the scalability, performance, and throughput of Node.js web applications.
Components of the Node.js Architecture:
- Requests: Depending on the actions that a user needs to perform, the requests to the server can be either blocking (complex) or non-blocking (simple).
- Node.js Server: The Node.js server accepts user requests, processes them, and returns results to the users.
- Event Queue: The main use of Event Queue is to store the incoming client requests and pass them sequentially to the Event Loop.
- Thread Pool: The Thread pool in a Node.js server contains the threads that are available for performing operations required to process requests.
- Event Loop: Event Loop receives requests from the Event Queue and sends out the responses to the clients.
- External Resources: In order to handle blocking client requests, external resources are used. They can be of any type ( computation, storage, etc).
Workflow of Nodejs Server:
- Users send requests (blocking or non-blocking) to the server for performing operations.
- The requests enter the Event Queue first at the server-side.
- The Event queue passes the requests sequentially to the event loop. The event loop checks the nature of the request (blocking or non-blocking).
- Event Loop processes the non-blocking requests which do not require external resources and returns the responses to the corresponding clients
- For blocking requests, a single thread is assigned to the process for completing the task by using external resources.
- After the completion of the operation, the request is redirected to the Event Loop which delivers the response back to the client.
Advantages:
- The Node.js server can efficiently handle a high number of requests by employing the use of Event Queue and Thread Pool.
- There is no need to establish multiple threads because Event Loop processes all requests one at a time, therefore a single thread is sufficient.
- The entire process of serving requests to a Node.js server consumes less memory and server resources since the requests are handled one at a time.