Understanding the event loop as "waiting work" and "hands-on work"
Here is what this post covers:
- Introduction
- The event loop is a ramen shop with one cook
- “Waiting work” and “hands-on work”
- For waiting work, one cook is fast enough
- When hands-on work piles up, the shop slows down
- Go hires several cooks
- Where this helps in practice
- Wrapping up
Introduction
A web server takes requests from many people at the same time. A JavaScript (Node.js) server runs your program in a single line of execution (a single thread), yet it can still handle a lot of traffic. What makes that possible is the event loop.
This post explains the event loop by comparing it to a ramen shop with only one cook. From there, it sorts out which kinds of work the event loop is good and bad at, and how Go differs, without any code. At the end, it touches on where this distinction helps in day-to-day work.
I wrote about choosing between Go and Node.js for an anonymous chat service in an earlier post. This one covers the mechanism behind that decision.
The event loop is a ramen shop with one cook
Picture a ramen shop run by a single cook. When an order comes in, the cook drops the noodles into the boiler. If the cook stood in front of the boiler until the noodles were done, nobody else in the shop would get served in the meantime.
So this cook moves on to the next job as soon as the noodles are in. When they are done, a timer goes off, and the next step, “plate the bowl,” is written on a note and added to the end of a to-do list. Whenever the cook’s hands are free, they take the note at the top of the list and work through them in order.
That is the event loop. The cook is your program, the boiler is the operating system that takes care of network traffic and file reads and writes, and the to-do list is the queue of work waiting to run next. The cook keeps repeating “when my hands are free, take the next job from the list,” which is why it is called a loop.
“Waiting work” and “hands-on work”
Whether the event loop is a good fit depends on what the work is. Work in a program falls roughly into two kinds.
| Waiting work (I/O-bound) | Hands-on work (CPU-bound) | |
|---|---|---|
| Where the time goes | Mostly waiting for a reply from someone else | Mostly computing on its own |
| Examples | Calling a website, querying a database, reading a file | Editing images, heavy calculations, compressing files |
| In the ramen shop | Boiling noodles | Chopping vegetables |
While the noodles boil, the cook’s hands are free. Vegetables, on the other hand, only get chopped if the cook chops them. That difference is what the next two sections are about.
For waiting work, one cook is fast enough
Say you send 100 requests, and each one takes 1 second to get a reply. If you wait for each reply before sending the next request, the whole thing takes about 100 seconds, because the waits stack up one after another.
With an event loop, you send one request and move straight on to the next without waiting for the reply. It is the same as dropping noodles in the boiler and taking the next order without waiting for them to finish. All 100 waits run at roughly the same time, so in theory every reply is back in about 1 second. Replies are then handled in the order they arrive.
Waiting does not tie up the cook’s hands, so it does not matter how many things are waiting at once. This is why Node.js can handle a lot of simultaneous traffic on a single thread.
When hands-on work piles up, the shop slows down
The trouble is work that only the cook can do, like chopping vegetables. While the cook is busy with something slow, such as finely mincing carrots, no other vegetables get cut. Any order that comes in during that time has to wait, however simple it is.
A single request that does heavy computation is enough to slow down responses for everyone else. If you see “whenever a certain heavy request comes in, every other request suddenly gets slow” in production, this is the first thing to suspect.
Go hires several cooks
Go does not use an event loop. It hires many lightweight cooks (goroutines), and a manager (the scheduler) assigns them to kitchens, one kitchen per CPU. Because there are several kitchens, hands-on work can genuinely move forward at the same time. Even if one cook keeps working on something for a long time, the manager swaps them out partway, so other customers are less likely to be kept waiting.
The catch is that you have to watch out for cooks fighting over the ingredients they are handling, each acting as if to say “that’s my job!” In Go, if you do not set rules for passing ingredients around (locks or channels), you get a kind of bug that cannot happen in JavaScript.
Side by side, JavaScript and Go look like this:
| JavaScript (Node.js) | Go | |
|---|---|---|
| Cooks | One | Several |
| Waiting work | Good at it | Good at it |
| Hands-on work | Bad at it | Can run at the same time across several kitchens |
| What to watch for | Heavy work slows the shop down | Cooks fighting over ingredients |
Where this helps in practice
This distinction helps less when choosing a language and more in the day-to-day building and running of whatever you chose.
The first is tracking down incidents. If “one heavy operation makes everything slow” in Node.js, you know to check first whether the cook’s hands are tied up.
The second is server configuration. Go counts how many kitchens (CPUs) it can use automatically, but when it ran in a container it could count more than it had actually been given. For example, with only 5 cores allotted, it would work as if the whole machine’s 48 cores were available, and the work over the limit would get paused from time to time. Since Go 1.25 it reads the container’s limit, but knowing how this works makes it easier to find the cause.
Wrapping up
The event loop handles a lot of requests by having one cook “do other work while waiting.” It is very strong at waiting work, but when a lot of hands-on work comes in, the shop slows down. Go can move work forward in parallel with more cooks and more kitchens, at the cost of having to watch out for cooks fighting over ingredients.
In Node.js, the runtime for JavaScript, the event loop has one cook working hard on their own, while Go hires several cooks. How does that picture work for you? Next time something heavy is slowing you down, start by asking whether it is waiting work or hands-on work. If it is waiting work, one cook can handle it, so you know the event loop is not the problem. If it is hands-on work, you can guess that it is tying up the cook’s hands and slowing the shop down. Once you know that, you can think about next steps, such as handing the heavy work to another cook (worker threads, in Node.js) or choosing something like Go that can hire several cooks.