Tuesday, July 27, 2021

Introduction to Event Loop Utilization in Node.js

Leave a Comment


JavaScript is a well-known single-threaded technology, but it may limit the use of the modern architecture of Node.js. When it comes to multiple threads, there comes some complexity and this is because of the inherent complexity. Therefore, managing context switching and spinning up new threads becomes expensive. And it also requires a lot of work from both the programmers and the operating system to deliver a solution. Besides, Node.js offers various other functionalities and security aspects. 


This is the reason why the event loop is used with Node.js. To more about this concept, let us go through this blog, and learn how everything in the Node.js event loop works and how it helps in solving various problems.

 

What is Event Loop Utilization?

The event loop is a very simple non-blocking, single-threaded, and asynchronously concurrent. When you request anything on a web page, it lookups in the database. A single thread has the capability to do one thing at a time. So, instead of waiting for the response from the database, it continues working with other tasks that are in the queue.

 

Basically, when it comes to the event loop, the main loop untangles the call stack and does not wait for callbacks. And because this loop doesn’t block, it is clear to work on a lot of web requests at a time. If there are multiple requests at the same time, it gets queued and this makes it concurrent. The event loop doesn’t wait for everything to get completed from one request but continues to pick up callbacks.

 

The event loop is itself semi-infinite. This means that if the callback queue or call stack is empty it can exit the loop. Imagine the call stake as synchronous code that untangles just like console log, prior to the loop polls. This is because the "block the event loop" function isn't there.

 

How Does Event Loop Work?

The process of the event loop is very simple. It works by requesting some external or internal event provider. These providers generally block the requests until the events have appeared and then call the appropriate event handler to dispatch the events.

 

Event Loop in Node.js

The event loop comes with six phases. These stages are repeated for as long as the node.js applications have the code that needs to be performed -

 

Phase 1: Timers 

Node.js timers enable the execution of functions that can carry out the callbacks after a set period of time. This time is set up in the core timer module and this module offers two main global functions known as setTimeout(), and setInterval(). Each of these functions allows the developer to define code that will get executed after a period of time.

 

The timers process is carried out directly by the Event Loop. At the very beginning of this stage, the Event Loop updates its own duration. After that, it checks a pool or queue of timers. This pool has all timers that are recently set.

 

The fundamental difference between the two nodes is that setInterval() comes with a repeat flag that has the capability to place the timer back into the queue after its execution. This is how a server stays alive while waiting for the request to be fulfilled.

 

Phase 2: I/O Callbacks

This stage of the event loop is of non-blocking input or output. To make the concept of blocking vs. non-blocking input/output more clear, imagine that you are at a restaurant waiting for your food to arrive and while the chef is preparing it, you are unable to do anything else. You are all busy or blocked while waiting for food. As soon as the food arrives, you are unblocked.

 

Similarly, when your node app is waiting for a document to be read, this application doesn't wait for the system to get back with the required file. So basically, the app can continue the execution of the code and receive the content asynchronously. This is exactly what non-blocking I/O is all about.

 

Phase 3: Waiting & Preparation

This is a stage where the event loop performs various internal operations of any callbacks. Basically, there is no direct influence on this phase and no mechanism is present that has the capability to guarantee code execution in this stage. But, still, this phase is used by Node.js developers for gathering information and planning the execution process of the next even loop phase. This is how the process of the event loop works.


Phase 4: I/O Polling

In this stage, the Node.js developers execute the JavaScript code that they have written. Basically from the beginning of the file to its end, everything that is percent in the JavaScript code is executed here. This process completely depends upon the code it may immediately execute.

 

In this phase, the event loop is the one managing the I/O workload or calling all the functions from the queue until its employ, or calculating the time it must wait before the next phase starts. Here, all callbacks are called synchronously. This means that they are called in the same order they were put in the queue.

 

Event loop's I/O polling stage can potentially block the node application whenever the callbacks are not executed in an asynchronous manner or slow down.

 

Phase 5: setImmediate() Callbacks

Node.js is a technology that comes with a special timer called setImmediate(). In this stage, the timer and its callbacks are executed. The performance of this process starts as soon as the poll phase becomes idle. Therefore, the chances of setImmediate() executing before the timer are more but only if it is scheduled within the input/output cycle.

 

Phase 6: Close Events

Within the event loop, a closing event is the last and final stage. Here the callback of every close event is executed. For instance, a close event of process. exit () or web socket callback is called. This stage is processed when the event loop is completing one cycle and is ready to start the new one. It is one of the most important phases in JavaScipt as it enables the cleaning of the state of the app.

 

Conclusion

As seen in this blog post, the event loop is a very simple architecture and it is the ultimate sophistication. It can handle difficult issues like thread safety, concurrency, and asynchrony. It removes the things that aren't helpful and maximizes throughput in a very efficient way.



0 comments:

Post a Comment

If you have any question, please email me at durgtech@gmail.com