The Node.js Event Loop: Concurrency on a Single Thread
The Node.js event loop lets a single thread handle high concurrency by offloading I/O. It's ideal for web servers and APIs, but the footgun is that any long-running synchronous code will block the entire application, freezing all other requests.
Why it exists
Traditional web servers often used a thread-per-request model, which consumes significant memory and struggles under high concurrency. Node.js was designed to solve this with an event-driven, non-blocking I/O model, allowing a single process to handle thousands of concurrent connections efficiently.
The mental model
Imagine a restaurant with only one, very fast waiter. The waiter takes an order (initiates an I/O operation like a database query), gives it to the kitchen (the underlying OS/libuv), and immediately moves to the next table without waiting for the food to be cooked. When a dish is ready (the I/O completes), the kitchen signals the waiter, who then delivers it. The waiter is the event loop; your code is the waiter, and the kitchen is the background worker pool.
How it works
The event loop is a process that continuously checks for tasks to execute. When your JavaScript code initiates an asynchronous operation (like reading a file with fs.readFile), Node.js hands that operation off to a worker thread in its underlying C++ library, libuv. The main JavaScript thread is not blocked and can continue executing other code. When the I/O operation finishes, its associated callback function is placed into a queue. The event loop picks up this callback from the queue and executes it on the main thread.
When to use it
Node.js and its event loop excel in I/O-bound applications. This includes web servers, APIs, real-time applications like chat servers, and microservices that spend most of their time waiting for network or database responses. It's built for high concurrency with low resource usage.
When not to use it
Avoid Node.js for CPU-intensive tasks. A long-running, synchronous computation (like complex math or data processing) will monopolize the single main thread, blocking the event loop entirely. This makes the application unresponsive to all other requests. For CPU-bound work, consider using Node.js Worker Threads or a different language better suited for multi-threaded computation.
One canonical example
A simple Express.js web server handling API requests. When a request comes in to fetch user data, the server makes a non-blocking call to a database. While waiting for the database response, the event loop is free to handle hundreds or thousands of other incoming requests. Once the database returns the data, the corresponding callback is executed to send the HTTP response.
Interview question
What is the primary consequence of running a long-running synchronous task in a Node.js application?
- a.The application's event loop will become blocked, making the server unresponsive.Correct
- b.Node.js will prioritize other I/O operations, delaying the synchronous task's execution.
- c.The task will be automatically moved to a background thread, preventing blocking.
- d.The task will cause a memory overflow due to its single-threaded nature.
Why? this is the answer
The card states that a long-running synchronous computation will 'monopolize the single main thread, blocking the event loop entirely,' leading to an unresponsive application. Synchronous tasks are not automatically offloaded to background threads; they execute directly on the main thread, unlike asynchronous I/O operations.
Just read this? Test yourself on what you have been reading.
Read the original → en.wikipedia.org
- #nodejs
- #event-loop
- #async
- #concurrency
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on nodejs — each one lists the topics its interview covers.
See open roles