Node.js Event Loop Explained

    Dec 28, 2018       by Pankaj Kumar
nodejs-event-loop-explained.jpg

Node.js has quickly become most popular technology for web app development. It’s completely written in javascript, runs over Google chrome’s V8 engine, and uses  event loop with callback to maximize the CPU usage and increases the app performance. The event loop is what allows Node.js to perform non-blocking I/O operations — despite the fact that JavaScript is single-threaded — by offloading operations to the system kernel whenever possible.

Popularity of node.js is increasing very speedly, many developers starts using Node.js without knowing the basic architecture that makes node.js so popular. When Node.js starts, it initializes the event loop, processes the provided input script which may make asyncronous API calls, schedule timers,  call process.nextTick(), then starts processing the event loop. This blog gives a basic understanding of Node.js event loop, its supporting architecture and working flow.

 

Understanding Blocking I/O and Threads

When we make query with database, creating any REST API or any interaction with file system at server end a process runs that is normally works in blocking way. And in this approach our system is temporarily blocked and due to this our system don’t accept any further request untill the current request completes. So we wait to return the processed data from the server. If our program uses the previous result data in the next function, then we need to wait for the response first to execute the program further.

 

These type of blocking action run on threads. A thread or a task are similar and are often confused for programmers. Most computers can only execute one program instruction at a time, but because they operate so fast, they appear to run many programs and serve many users simultaneously.  It’s important to remember that while separate threads can run in parallel, they share entire CPU power and memory. This is not much concern with traditional synchronous programming language like PHP, .Net.

 

Single Threaded Approach of Node.js

Most of the traditional programming languages are multi-threaded. Means for every request an independent thread runs. While this has been proven in managing many number of  blocking operations simultaneously. It can result in unwanted waiting time and poor CPU allocation. Node takes a very different approach. It uses a single thread to process all IO with the help of event loop with callback. The main thread of this loop listens for events and then triggers callback function when any processing events are detected as complete. If any particular request is blocking, Node pulls from different thread pool to manage the request while it continues running the main thread. That’s way, blocking operations do not interfere with any other non-blocking operations.

The single threaded approach is the feature which makes it different from others and make it so popular in rapidly. After all additional threads are also used to process the blocking tasks. The main differences lies in how Node.js uses the additional threads. As traditional languages uses multi-threaded concept to allocate a separate thread for every request, Node.js partially uses the additional threads for processing of blocking task.So in Node.js non-blocking operations goes in single thread and only blocking processes are sent to the separate thread. This approach provides much more efficient memory allocation and CPU usage, making Node more performant.

 

Conclusion

After all, multi-threaded programming languages are still widely used at this time. In multi-threading languages, If one thread fails due to any reason, we still have other independent threads processing. With Node, if one process gets expensive, it can slow down the whole event loop which is also one of the drawback of node.js, but it is good for Node.js sample application.


Find other similar Articles here: