Non-Blocking event loop in Node.js
Last Updated :
18 Jun, 2024
Node.js operates on a single-threaded, event-driven architecture that relies heavily on non-blocking I/O operations to handle concurrent requests efficiently. This approach is enabled by its event loop mechanism, which allows Node.js to handle multiple requests concurrently without creating additional threads for each request. Let's explore how the non-blocking event loop works in Node.js.
Think of a waiter working in a restaurant. He goes around the restaurant taking orders from customers and serving them when their respective food is ready. What happens when the waiter takes an order from a customer and waits until the food is prepared, serves it and then proceeds to the next customer. This way the waiting time for each customer increases and the restaurant would be a huge flop. The latter represents synchronous programming and the earlier one represents asynchronous programming.
Event-Driven Architecture
Node.js uses an event-driven, asynchronous model to handle requests and I/O operations. Here’s how it typically works:
- Event Loop: Node.js includes an event loop that continuously listens for events and executes callback functions when an event occurs. It's the heart of Node.js architecture, managing asynchronous operations.
- Non-Blocking I/O: Node.js performs I/O operations asynchronously, meaning it doesn’t wait for these operations to complete before moving on to the next task. Instead, it delegates I/O tasks to the operating system and registers callback functions to be executed once the operation completes.
- Callback Queue: When an asynchronous operation completes, its callback function is pushed into the callback queue.
- Event-Driven Execution: The event loop picks up callback functions from the callback queue and executes them one by one in the order they were queued, when the stack is empty.
Benefits of Non-Blocking Event Loop
- Scalability: Node.js can handle large numbers of concurrent connections efficiently because it doesn’t create threads for each connection. Instead, it uses a single-threaded event loop to manage all connections asynchronously.
- Performance: By leveraging non-blocking I/O, Node.js can perform multiple tasks concurrently without being blocked by slow I/O operations. This improves overall application performance and responsiveness.
- Resource Efficiency: Node.js consumes less memory compared to traditional multi-threaded servers because it doesn’t allocate resources for each individual thread. Instead, it manages concurrent requests with a single thread and an event loop.
Non-Blocking
Non-Blocking nature of node.js simply means that node.js proceeds with the execution of  the program instead of waiting for long I/O operations or HTTP requests. i.e the non-JavaScript related code is processed in the background by different threads or by the browser which gets executed when node.js completes execution of the main program and when the data required is successfully fetched. This way, the long time taking operations do not block the execution of the remaining part of the program. Hence the name Non-Blocking.
Example: Have a look at the below code-snippet for getting a better understanding of the non-blocking nature of Node.js.
JavaScript
console.log("First one to start");
setTimeout(() => {
console.log("I should wait for 3 seconds before execution");
}, 3000);
setTimeout(() => {
console.log("I should wait for 0 seconds before execution");
}, 0);
console.log("It's time for me to end");
Output:

As you can see, the rest of the program is unaffected by the long delay. But why did "I should wait for 0 seconds before execution" print after "It's time for me to end". Well, let's get deeper into the internal working of Node.js.
How the code is Executed
Step 1: The main function is pushed to the call stack.
Step 2: The first console.log statement is pushed to the call stack.
Step 3: "First one to start" is printed and the console.log function is removed from the call stack.
Step 4: The setTimeOut(3000) function is given to the browser for processing.
Step 5: The setTimeOut(0) function is given to the browser for processing.
Step 6: The setTimeOut(0) function has been processed and given to the CallBack Queue.
Step 7: The last console.log statement is pushed to the call stack.
Step 8: "It's time for me to end" is printed and the console.log function is removed from the call stack.
Step 9: The main function is removed from the call stack and the event loop starts running.
Step 10: The console.log function present in setTimeOut(0) is pushed to the call stack.
Step 11: "I should wait for 0 seconds before execution" is printed and the console.log function is removed from the call stack.
Step 12: After waiting for 3 seconds, the browser gives the setTimeOut(3000) function to the CallBack Queue.
Step 13: The console.log function present in setTimeOut(3000) is pushed to the call stack.
Step 14: "I should wait for 3 seconds before execution" is printed and the console.log function is removed from the call stack.
Diagram:
Non-Blocking Event LoopConclusion
Node.js’ non-blocking event loop and asynchronous I/O model enable it to efficiently handle high concurrency and I/O-bound operations. By leveraging a single-threaded event loop and non-blocking operations, Node.js achieves high performance and scalability, making it well-suited for building fast and responsive applications, particularly those requiring real-time interactions or handling numerous concurrent connections. Understanding and leveraging these principles is crucial for maximizing the benefits of Node.js in modern web development.
Similar Reads
Blocking and Non-Blocking in NodeJS
In NodeJS, blocking and non-blocking are two ways of writing code. Blocking code stops everything else until it's finished, while non-blocking code lets other things happen while it's waiting. This difference is key to understanding how NodeJS works and writing fast, efficient applications.What is B
6 min read
What is Poll Phase in Node.js Event Loop ?
The Node.js event loop is the heart of Node.js, responsible for executing non-blocking I/O operations and enabling the asynchronous behavior that defines the platform. Among the various phases of the event loop, the poll phase plays a crucial role in handling I/O events. In this article, we'll delve
5 min read
Explain the concept of non-blocking I/O in Node
In traditional synchronous programming models, I/O operations such as reading from a file or making network requests block the execution of the program until the operation completes. This means that if there are multiple I/O operations, they are processed sequentially, leading to potential bottlenec
3 min read
NodeJS Event Loop
The event loop in Node.js is a mechanism that allows asynchronous tasks to be handled efficiently without blocking the execution of other operations. It:Executes JavaScript synchronously first and then processes asynchronous operations.Delegates heavy tasks like I/O operations, timers, and network r
5 min read
Event Demultiplexer in Node.js
Node.js is designed to handle multiple tasks efficiently using asynchronous, non-blocking I/O operations. But how does it manage multiple operations without slowing down or blocking execution? The answer lies in the Event Demultiplexer.The Event Demultiplexer is a key component of Node.js's event-dr
3 min read
Backbone.js listenToOnce Event
Backbone.js listenTo Event notifies an object to listen to a particular event on another object. The benefit of using this form is that listenTo permits the object to keep the track of the events, & later, they are removed all at once. When an event occurs, the callback function will be called w
2 min read
Node.js Process beforeExit Event
The 'beforeExit' is an event of class Process within the process module which is emitted when Node.js empties its event loop and has no additional work to schedule. Syntax: Event: 'beforeExit'Parameters: This event does not accept any argument as the parameter. Return Value: This event returns nothi
2 min read
Backbone.js stopListening Event
Backbone.js stopListening Event is used to stop an object to listen to the event on the other object. The subsequent arguments will be passed to identify the object. Either stopListening will have no arguments to have the object remove all of the registered callbacks. Sometimes it takes an argument
3 min read
Explain the Mechanism of event loop in Node.js
JavaScript is a single-threaded, non-blocking, asynchronous language that uses the concept of event loop to make it work asynchronously even if it is single-threaded. Feature: The Event Loop is responsible for sending functions from the event queue to the Stack for processing when it becomes empty.
2 min read
Node.js Process exit Event
The process is the global object in Node.js that keeps track of and contains all the information of the particular node.js process that is executing at a particular time on the machine. The process.exit() method is the method that is used to end the Node.js process. Every process action on the mach
2 min read