Nodejs Event Loop Lag

Sandeep
NodejsMadeEasy
Published in
4 min readFeb 6, 2022

--

Event loop lag is one of the important performance metrics but we don’t talk much about it. In this post, I will explain what it is, why does it matter, how to measure and minimize it.

To be able to understand the event loop lag we must understand how does the Nodejs event loop work. If you don’t I would recommend you to go through the below resources:

By far I believe you understand how the Nodejs event loop works. Just keep in mind that the event loop runs in a single thread.

Why do so many people say Nodejs is single-threaded(Yes and no)?, also if you want to tune Nodejs performance they say avoid long-running blocking code in the main thread. Let me explain why.

Note: However, Nodejs has a Libuv thread pool for asynchronous(file IO, DNS, Zlib, etc) tasks. Nodejs does not use the thread pool for networking IO.

Hey!

Courtesy:https://www.meme-arsenal.com/

Ok fine! Let me show you a basic but powerful example as to why Nodejs is called single-threaded.

You can run the above code here: https://replit.com/@sandeepp2016/IsNodeSingleThreaded#index.js

Now I believe you got a gist of how long-running synchronous(blocking) code could block the event loop from running. In the above example due to the infinite loop, it could not even be entered in the event loop phases, in our case timer phase, despite the fact that the timer was expired. This is because the event loop also runs in the same thread(Main thread) but synchronous code blocks event loop execution.

This raises an interesting question what if we have long-running blocking code started in the poll phase of the event loop, for example, API request to a server, or any other phase right? Wouldn’t it will degrade the event loop(Nodejs) performance? and how do we measure it? Well, that is what we will talk about here:

The EVENT LOOP LAG

What is event loop lag?

One good example says a lot than the word so let me twist the previous example a little bit.

Courtesy:makememe.org

Run it here: https://replit.com/@sandeepp2016/nodejs-event-loop-lag

Note: You might get different outputs for the execution based on your system capability.

According to the above example, it should print “eventlooplag ” roughly in 100ms or a little bit more but it got printed after 2.495s, approximately lagging by 2.395 seconds.

Note: By default, there will be some lag in timers.

So

Event loop lag is an estimation of the time span between the scheduling of a callback and its execution.

What does event loop lag or delay tell us about?

The longer the average lag is the more synchronous code is being executed, It’s time for you to tweak your APIs code for better performance. Average Lag more than 100 ms should be an alarm for the investigations and lags over 300 ms are usually a serious problem. But it all depends on the use cases you have.

Avoid long running Blocking code.

How do we measure it?

In this post, I have shown you a basic example that roughly measures lag but you should consider using advanced APM tools such as New Relic, Appdynamics, Dynatrace, Nodejs-dashboard(open-source) that provide measurement with accuracy and graphical representations.

A sample Nodejs-dashboard:

Example code using Nodejs-dashboard: https://github.com/sandeep-cs-dev/event-loop-lag-example

Why does it Matter?

It is not just a matter of performance but it also has something to do with business.

Poor performance of your systems(APIs) could cost you business revenue.

53% of visits are abandoned if a mobile site takes longer than 3 seconds to load — Google

A 100ms increase in load time led to 1% decrease in revenue — Amazon

source:

How do you minimize event loop delay?

If your Apis are suffering from high lag you probably are running blocking code. You need to make synchronous code asynchronous. There are many options like using Nodejs worker_threads APIs, child_process, c++ addon to leverage Libuv thread pool, or even separate API(server). You should consider options per your need basis.

Summary:

  • Event loop lag is an important but often overlooked performance metric.
  • Event loop lag is the estimation of the time span between the scheduling of a callback and its execution.
  • Avoid long-running blocking code to keep event loop delay below the threshold.
  • Many studies have shown that poor performance can negatively impact business revenue.

Useful resources:

--

--