DEV Community

Patrick Shema Karamuka
Patrick Shema Karamuka

Posted on

My Understanding of Event Loop in NodeJS

My Understanding of Event Loop in NodeJS

By definition, the event loop in Node JS is what makes it possible for a Node JS to handle multiple requests at the same time without waiting/blocking other requests.

Almost all operating systems nowadays are multithreaded, which means they can handle a number of different tasks at the same time. For instance, a Linux system like fedora or ubuntu can perform an I/O operation such as writing to disk and at the same time fetching data from a remote server or streaming a video online, this ability of the operating system to handle multiple tasks at the same is what NodeJS event loop uses on to delegate tasks to the host OS.

So, how does NodeJS do it exactly

How Node JS Event loop makes use of the base operating system to serve multiple requests

NodeJS Event loop offloads tasks to the host operating system and in turn, the operating system provides a callback as an observer for when it has finished a certain task. This happens so fast that NodeJS thread is always available for new requests.

Sometimes, there are tasks that require intensive calculations and the base operating system can’t handle them or it takes longer to complete, in this case, it is when NodeJS’s thread pool comes into play. By default when you create a thread pool, it’ll have four threads assigned and you can use these to handle those tasks the OS can’t handle.

What is Node Js thread pool

Understanding the event loop(main loop) and thread pool (worker pool) in Node JS

In Node Js, there are two types of threads, the main thread also known as event thread and thread pool in which Node JS uses to handle CPU intensive tasks.

As mentioned earlier in this article there comes a time when the base OS can’t handle a specific task, and this is when the thread pool is used to handle those tasks.

After reading the concepts of the thread pool in Node Js, one might understand that Node Js is not really single-threaded, but instead, it is also multi-threaded under the hood thanks to the thread pool.

Summary

To summarize, Node Js is referred to as single-threaded in case of performing non-CPU intensive tasks like fetching some data from a database, and multi-threaded in case of CPU intensive tasks when using thread pool(also known as worker threads) such as complex scientific calculations or cryptographic operations. Example Node Js APIs that use the thread pool are Zlib, crypto, fs(filesystem) and DNS.

Node uses the event loop to handle those non-CPU intensive tasks and thread pool for tasks that take a long time to finish.

All incoming requests and outgoing responses pass through the event loop (main loop), so it’s advised not to block the event loop because if it spends long at any point, all other clients requests won’t get served. This means that every callback in your app should complete as quickly as possible to give other clients a turn.

Top comments (0)