Node ~ Architecture - rohit120582sharma/Documentation GitHub Wiki

Node JS applications uses "Single Threaded Event Loop Model" architecture to handle multiple concurrent clients.

There are many web application technologies like JSP, Spring MVC, ASP.NET, HTML, Ajax, jQuery etc. But all these technologies follow "Multi-Threaded Request-Response" architecture to handle multiple concurrent clients.

Before discussing "Single Threaded Event Loop" architecture, first we will go through famous "Multi-Threaded Request-Response" architecture.


Traditional Web Application Processing Model

Any web application developed without Node JS, typically follows “Multi-Threaded Request-Response” model. Simply we can call this model as Request/Response model.

A client sends a request to the server, then the server does some processing based on clients request, prepare a response and send it back to the client.

This model uses HTTP protocol. As HTTP is a stateless protocol, this Request/Response model is also stateless model. So we can call this as Request/Response Stateless Model.

Processing Steps:

  • Clients send request to Web Server.
  • Web Server internally maintains a limited thread pool to provide services to the client requests.
  • Web Server is infinite loop and waiting for client incoming requests
  • Web Server receives those requests:
    • Server pickup one client request
    • Pick up one thread from thread pool
    • Assign this thread to client request
    • This thread will take care of reading client request, processing client request, performing any Blocking IO Operations (if required) and preparing response
    • This thread sends prepared response back to the server
    • Server in-turn sends this response to the respective client

This model creates one Thread per Client request. If more clients requests require Blocking IO Operations, then almost all threads are busy in preparing their responses. Then remaining clients requests should wait for longer time.


Single Threaded Event Loop

Node JS follows "Single Threaded with Event Loop" model. Node JS processing model mainly based on Javascript Event based model with Javascript callback mechanism.

Processing Steps:

  • Clients send request to Web Server.
  • Server internally maintains a limited thread pool to provide services to the client requests.
  • Server receives those requests and places them into a queue. It is known as "Event Queue".
  • Server internally has a component, known as "Event Loop". It uses an indefinite loop to receive requests and process them. Event Loop uses Single Thread only. It is main heart of Node JS Platform Processing Model.
  • Even Loop checks any client request is placed in event-queue. If no, then wait for incoming requests for indefinitely.
  • If yes, then pick up one client request from event queue:
    • Starts to process that client request
    • If that client request does not require any Blocking IO Operations, then process everything, prepare a response and send it back to the client.
    • If that client request requires some Blocking IO Operations like interacting with Database, File System, External Services then it will follow different approach
      • Checks threads availability from Internal Thread Pool
      • Picks up one thread and assign this client request to that thread.
      • That thread is responsible for taking that request, process it, perform Blocking IO operations, prepare response and send it back to the "Event Loop"
      • Event Loop, in turn, sends that response to the respective client

Even though our Node JS application receives more and more concurrent client requests, there is no need of creating more and more threads, because of the "Event Loop".

Node JS application uses fewer threads so that it can utilize only fewer resources or memory.

⚠️ **GitHub.com Fallback** ⚠️