how node js works

How Node Js works – Reactor Pattern and Event Loop

Featured, Javascript, Node JS

In this article we will going to describe that how node js works internally., How it handles I/O operations what is event driven and non blocking mean. What is Event Loop and how it works. Lets Start,

Reactor Pattern

Node js works on event driven architecture and it has a event demultiplexer and event queue. All I/O request generates a event and these events are processed as mentioned in below steps.

  1. Event demultiplexer collects I/O request from and send there request to its hardware (data from file , process image etc)
  2. Once I/O request is processed event demultiplexer add callback handler to the request and send it to event queue.
  3. When events are available in event queue, Event Loop processed them in sequence until event queue gets empty.
  4. Once event queue gets empty and there is no more I/O operation registered in event demultiplexer, the event loop stops working and program will complete.

The Event Loop is the real hero in the node js architecture which makes node js non blocking and asynchronous. Lets understand more about Event Loop.

Event Loop is single threaded and semi infinite loop . Its called semi infinite because event loop existed at any point when there is no more work to do or we can say there is no more event in event queue or demultiplexer.

Credits : http://katieleonard.ca

Event Demultiplexer

Event Demultiplexer exist in diffrent system with diffrent names like epoll on Linux, kqueue in BSD systems, IOCP (Input Output Completion Port) in windows. Node js use these low level non blocking asynchronous hardware for I/O functionalities provided by these systems.

But not all I/O can be performed using these hardwares. File operations can not be performed asynchronous using these systems. To resolve these complex I/O functionalities thread pool is introduced.

Thread Pool

There are some complexities with some I/O operations which requires more time to get processed, modules like fs, crypto, database operations. To avoid the blocking of event loop, Thread Pool (aka Worker Pool) concept is added to node js.

Thread Pool is maintained by the libuv library to perform long running operations in background and stops blocking the event loop. The default size of libuv thread pool is 4 and can be increased by setting UV_THREADPOOL_SIZE in environment variable.

Source: http://docs.libuv.org/en/v1.x/_images/architecture.png

Libuv Docs

Its a misconception that all the I/O runs through Thread Pool.

Event Queue

Event queue is a data structure where all events with its handler gets registered and processed by event loop sequentially until event queue gets empty.

Lets deep dive in event queue and find out what actually happens.

Basically there are more than one queue and each queue represent as phase. Event Loop goes through all these phases and check for the registered event.

Before moving from one phase to another, event loop checks 2 intermediate queues until intermediate queue gets empty.

There are 4 major event queues processed by event loop.

  1. Expired Time and Interval Queues : It contains expired time callbacks like setTimeout(), setInterval()
  2. IO Event Queue : It contains completed IO events.
  3. Immediate Queue : Callbacks added using setImmediate()
  4. Close Handler Queue : Any close event handler.

Other than this there are 2 more intermediate queue which event loop check after moving from each queue to another queue ( also called phases)

  1. NextTick Queue : Process callback added using process.nextTick()
  2. Other Microtask Queue: Other Microtask like promises callback.

Below is the diagram to show how event loop process through all the queues

Source : https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/

I will describe all the mentioned queues in upcoming tutorials.

Lets summarize:

When a request comes to node it gets registered in event demultiplexer . Event demultiplexer sends the request to its hardware to resolve and queues it with its context and a callback with it.

Event demultiplexer sends the event to event queue with the context and callback handler.

Event Loop checks the event queue, picks the event and provides result to its callback handler.

The event loop send the response to the application by callback handler or stores another event in the event demultiplexer.

Event loop will run continuously and check the event queue until event queue get empty. Once there is no more event to execute, event loop completes the program and stops running.

12 comments

  • Here are my queries:
    1. Is Event Demultiplexer the common name for event-notification-system like e-poll, k-queue etc.?
    2. After recieving a TCP-request via e-poll(Linux), what is the next step that event loop does(like we performed a get request from a remote client)?
    3. Is it the poll phase in which event-loop decides whether to use e-poll or use a thread-pool.? How does it decides?
    4. Do we have only a single socket-descriptor for say e-poll, why can’t we create and use a simple socket descriptor(like we do in c) instead of e-poll?
    5. Is databse call also included in e-poll?
    6. Say I did an async file read, Till the time kernel updates file descriptor and event-loop reaches poll phase and place the callbcak of that I/O in it’s event-queue, where is the callback placed for that call?
    7. It’s said event loop executes callbacks in event-callback queue iteratively , the genral diagram shows that the callback to be executed is pushed onto the call stack, Is it correct? If it’s the case then the single thread we are talking about has to pop that callbcak which means the event loop breaks somewhere in between and then starts again?

  • Thank you for the well-written blog post. I greatly treasured your generosity and help you offer through the totally free tips on your blog, specially the ones provided through this article. I know Britta would love to read more of your blog post. I’ve sent your website url to her. We love your thoughtfulness during this difficult time.

  • What I couldnt give to learn how you got your design to be so crazy! I mean it. Besides the blog just being awesome, this page is too sweet! Its not too flashy. It doesnt do too much with colours and things and the videos you use are perfect for this topic! Really, awesome blog.

  • After study many of the websites on the web site now, we genuinely much like your means of blogging. I bookmarked it to my bookmark internet site list and will also be checking back soon. Pls consider my web site at the same time and inform me what you consider.

Leave a Reply