Ad Code

Responsive Advertisement

Ticker

6/recent/ticker-posts

How does Node.js work?

How does Node.js work?

 NODE.JS ARCHITECTURE

Node.js is completely working on event-driven concept. Basically, the server consists of one thread processing one event after another.

A new request coming in is one kind of event. The server starts processing it and when there is a blocking IO operation, it does not wait until it completes and instead registers a callback function. The server then immediately starts to process another event ( maybe another request ).

 When the IO operation is finished, that is another kind of event, and the server will process it ( i.e. continue working on the request ) by executing the callback as soon as it has time.

Node.js Platform does not follow the 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.

Node Architecture

Single Threaded Event Loop Model Processing Steps:

  • Clients Send requests to the 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 an indefinite loop to receive requests and process them.
  • Event Loop uses Single Thread only. It is the main heart of the Node.js Platform Processing Model.
  • Event Loop checks any Client Request is placed in the Event Queue. If no, then wait for incoming requests indefinitely.
  • If yes, then pick up one Client Request from Event Queue
    • Starts to process that Client's 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 Systems, and External Services then it will follow a 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, processing it, performing Blocking IO operations, preparing a response, and sending it back to the Event Loop
      • Event Loop, in turn, sends that Response to the respective Client.

Post a Comment

0 Comments