DETAILED READ SECTION

Understand Node JS Single Thread Event Loop Work Flow



Node JS Single Threaded Event Loop Model


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.


In this article we focus on Single Threaded Event Loop Model which is latest request and response model to process client request and generate response utilizing lesser memory resources compare to traditional request and response model which is based on multi threaded.


Node JS Platform does not follow Request/Response Multi-Threaded Stateless Model. It follows Single Threaded with Event Loop Model. Node JS Processing model mainly based on Javascript Event based model with Javascript callback mechanism.


As Node JS follows this architecture, it can handle more and more concurrent client requests very easily.


The main heart of Node JS Processing model is “Event Loop”.


Here are Single Threaded Event Loop Model Processing Steps:

  • Clients Send request to Web Server.

  • Node JS Web Server internally maintains a Limited Thread pool to provide services to the Client Requests.

  • Node JS Web Server receives those requests and places them into a Queue. It is known as “Event Queue”.

  • Node JS Web Server internally has a Component, known as “Event Loop”. Why it got this name is that it uses indefinite loop to receive requests and process them. (See some Java Pseudo code to understand this below).

  • 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

  1. Starts process that Client Request

  2. If that Client Request Does Not requires any Blocking IO Operations, then process everything, prepare response and send it back to client.

  3. 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.

Diagram Description

  • Here “n” number of Clients Send request to Web Server. Let us assume they are accessing our Web Application concurrently.

  • Let us assume, our Clients are Client-1, Client-2… and Client-n.

  • Web Server internally maintains a Limited Thread pool. Let us assume “m” number of Threads in Thread pool.

  • Node JS Web Server receives Client-1, Client-2… and Client-n Requests and places them in the Event Queue.

  • Node JS Even Loop Picks up those requests one by one.

1. Even Loop pickups Client-1 Request-1

  • Checks whether Client-1 Request-1 does require any Blocking IO Operations or takes more time for complex computation tasks.

  • As this request is simple computation and Non-Blocking IO task, it does not require separate Thread to process it.

  • Event Loop process all steps provided in that Client-1 Request-1 Operation (Here Operations means Java Script’s functions) and prepares Response-1

  • Event Loop sends Response-1 to Client-1

2. Even Loop pickups Client-2 Request-2

  • Checks whether Client-2 Request-2does require any Blocking IO Operations or takes more time for complex computation tasks.

  • As this request is simple computation and Non-Blocking IO task, it does not require separate Thread to process it.

  • Event Loop process all steps provided in that Client-2 Request-2 Operation and prepares Response-2

  • Event Loop sends Response-2 to Client-2

3. Even Loop pickups Client-n Request-n

  • Checks whether Client-n Request-n does require any Blocking IO Operations or takes more time for complex computation tasks.

  • As this request is very complex computation or Blocking IO task, Even Loop does not process this request.

  • Event Loop picks up Thread T-1 from Internal Thread pool and assigns this Client-n Request-n to Thread T-1

  • Thread T-1 reads and process Request-n, perform necessary Blocking IO or Computation task, and finally prepares Response-n

  • Thread T-1 sends this Response-n to Event Loop

  • Event Loop in turn, sends this Response-n to Client-n


Here Client Request is a call to one or more Java Script Functions. Java Script Functions may call other functions or may utilize its Callback functions nature.


So Each Client Request looks like as shown below:

function(other-function call, callback-function)


Node JS Architecture – Single Threaded Event Loop Advantages

  1. Handling more and more concurrent client’s request is very easy.

  2. 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 Event loop.

  3. Node JS application uses less Threads so that it can utilize only less resources or memory

Event Loop Pseudo code:


public class EventLoop {

while(true){

if(Event Queue receives a JavaScript Function Call){

ClientRequest request = EventQueue.getClientRequest();

If(request requires BlokingIO or takes more computation time)

Assign request to Thread T1

Else

Process and Prepare response

}

}

}


#NGAutomation

Building better QA for tomorrow