Node JS- Single Threaded Event Loop Model

All major server side languages support concurrency (concurrent requests) using multi thread or multi process programming like Java, .Net, Ruby & PHP, etc, until Event driven model becomes popular.

Node JS uses single thread event loop model architecture to support concurrency. All incoming request handled by single thread & shared resources in node js application. In detail, The main event loop is run on single thread with all I/O operation runs on separate threads because of its non-blocking design in order to utilize the event loop.

How it work:

  1. Considering Node JS Rest Application, while creates server on specific port, the main thread created and associated with server connection.
  2. Node JS allocates a thread pool. when application receive the request, request are recievied by main thread and pushed into queue which is nothing but event queue.
  3. Now Event loop (indefinite loop) comes in picture which always runs on main thread. Even loop looks the event queue for any request and picks for process
Single Thread Event Loop Model

4. Check the request if it is Block Operation then assign a thread to complete blocking operation and return to event loop.

5. For Non Block operation, each call consider as event and handled by event loop. once the event completed, it goes to event queue to continue further.

Event Loop

Event loop has different phases to process each event which is depicted. It uses FIFO Queue

┌───────────────────────────┐
┌─>│ timers │
│ └─────────────┬─────────────┘
│ ┌─────────────┴─────────────┐
│ │ pending callbacks │
│ └─────────────┬─────────────┘
│ ┌─────────────┴─────────────┐
│ │ idle, prepare │
│ └─────────────┬─────────────┘ ┌───────────────┐
│ ┌─────────────┴─────────────┐ │ incoming: │
│ │ poll │<─────┤ connections, │
│ └─────────────┬─────────────┘ │ data, etc. │
│ ┌─────────────┴─────────────┐ └───────────────┘
│ │ check │
│ └─────────────┬─────────────┘
│ ┌─────────────┴─────────────┐
└──┤ close callbacks │
└───────────────────────────┘
  • timers: this phase executes callbacks scheduled by setTimeout() and setInterval().
  • pending callbacks: executes I/O callbacks deferred to the next loop iteration.
  • idle, prepare: only used internally.
  • poll: retrieve new I/O events; execute I/O related callbacks (almost all with the exception of close callbacks, the ones scheduled by timers, and setImmediate()); node will block here when appropriate.
  • check: setImmediate() callbacks are invoked here.
  • close callbacks: some close callbacks, e.g. socket.on('close', ...)

Programming isn’t about what you know; it’s about what you can figure out.