r/javascript • • 3d ago

Node.js' Event Loop Delay monitor has been lying to you (for years)

https://jomi-se.com/posts/nodejs-event-loop-delay-monitor/

In Node.js, if your event loop blocks for 30 seconds with a monitor that has a 60-second window and 50 milliseconds resolution, you'll get a p99 event loop delay of 52 milliseconds. And if you're lucky, the 30-second delay might show up in the max of the event loop delay monitor.

I wrote a blog post about it. The twist at the end was a bit funny (the fix has been sitting in the codebase since 2019). It almost features no AI.

It turns out this problem was given a name more than a decade ago by Gil Tene: Coordinated Omission. Where latency in your system skews your latency measurements and makes your percentile metrics lie to you. (awesome talk by Gil Tene on youtube "How NOT to measure Latency". Can't link video or post gets autoremoved)

In simpler terms: If your measurement system has this issue, when your system stalls for 10s, your p99 metrics happily report something like 5ms. If you measure latency from inside your code, you have this issue.

7 Upvotes

10 comments sorted by

3

u/Discordbothost 3d ago

yeah, this is coordinated omission. the real issue isn't window size or resolution, it's that anything measured from inside the event loop misses the stall it's part of. perf_hooks' monitorEventLoopDelay helps some but still can't see a synchronous block that freezes its own sampler.

1

u/BarelyAirborne 2d ago

If you ask it no questions it will tell you no lies.

1

u/kimberly-34 1d ago

The monitor's alibi: it was busy being blocked during questioning.

1

u/TheOfficeWM 2d ago

The bit that stuck with me is that a stalled loop cannot report its own stall, so the numbers look better than reality. Worth remembering before trusting latency figures from inside the process.

-3

u/Ronin-s_Spirit 2d ago

If you want to accurately measure the duration of any work you have to get a timestamp before and after it, it's that simple. Idk why you're faffing about with monitors and resolutions.

4

u/jomi-se 2d ago

If that's your perspective, go watch Gil Tene's talk and learn a thing.

Either you're smart enough to understand why this is valuable when measuring latency of systems as scale, or you're not and you'll stay convinced that all you need is before and after in code.

🤷

Quick very basic example. If you measure before/after in code, you'll never catch when a request was blocked at the OS level because the nodejs loop was blocked by some other synchronous stall.

-3

u/Ronin-s_Spirit 2d ago

Are you trolling me? Timestamps come from the OS, it doesn't matter if the event loop was frozen as long as you get a timestamp before a task and at the end of task.

1

u/jomi-se 2d ago

Lol, you're telling on yourself.

If you're request is at the network level, not yet taken into account by nodejs by example, an your timestamp is only taken once in your http listener callback at it's start, your timestamp can come from the os and be as accurate as you like. It'll still be late from the perspective of when it reached your server. And you'll be blind to that delay.

A real solution is to take the start timestamp in a header from the load balancer. Or measure it all from outside the server itself.

Go watch the talk lol

2

u/prawnsalad 2d ago

Better explained: if you have a function with some awaits in there then some other unrelated code (maybe another concurrent http request) can block the entire event loop and your function stalls at the await because of it. The function took longer to finish overall but it wasn't because of the function itself.

Knowing the difference means you can properly benchmark what your function actually does itself vs whatever else your app is doing that's interfering with it.

-2

u/Ronin-s_Spirit 2d ago

I don't care where it happens, it's not like like I can fix the OS, just bring the total number down.