Skip to main content

Command Palette

Search for a command to run...

Blocking vs Non-Blocking Code in Node.Js

Why It Matters for Performance

Updated
Blocking vs Non-Blocking Code in Node.Js

In the previous blog, you learned how Node.js handles multiple requests using a single thread.

Now we zoom into the core behavior that makes that possible.

Blocking vs non-blocking code.

This is not just a technical detail. This is the difference between a slow server and a scalable system.


What is Blocking Code

Blocking code is code that stops everything until it finishes.

When a blocking operation runs, the system cannot do anything else until that task is complete.

const data = fs.readFileSync("file.txt", "utf-8");
console.log(data);
console.log("Next task");

Here, the program waits for the file to be read completely before moving to the next line.

Nothing else happens during that time.


Why Blocking Slows Down Servers

In a server environment, multiple users are sending requests at the same time.

If one request uses blocking code:

  • the server waits

  • all other requests are delayed

This creates a bottleneck.

One slow operation can affect many users.


What is Non-Blocking Code

Non-blocking code does not wait.

It starts a task and moves forward immediately.

fs.readFile("file.txt", "utf-8", (err, data) => {
  console.log(data);
});

console.log("Next task");

Here:

  • the file read starts

  • the program continues executing

  • when the file is ready, the callback runs

The system keeps moving.


Waiting vs Continuing (Simple Analogy)

Imagine you ordered food.

Blocking behavior:

  • you stand at the counter

  • wait until food is ready

  • then leave

Non-blocking behavior:

  • you place the order

  • go sit down

  • collect food when it is ready

In the second case, you are not wasting time.

That is exactly what Node.js does.


Async Operations in Node.js

Node.js is built around non-blocking operations.

Common async operations include:

  • reading files

  • database queries

  • API calls

These tasks are delegated and handled in the background.

The main thread stays free to handle new requests.


File Handling Example (Clear Difference)

Blocking Version

const data = fs.readFileSync("largeFile.txt", "utf-8");
console.log(data);
console.log("Done");

Execution:

Read File → Wait → Print → Done

Non-Blocking Version

fs.readFile("largeFile.txt", "utf-8", (err, data) => {
  console.log(data);
});

console.log("Done");

Execution:

Start Read → Continue → Done → Print when ready

The second approach keeps the system responsive.


Impact on Server Performance

Blocking code:

  • delays all requests

  • reduces scalability

  • increases response time

Non-blocking code:

  • handles multiple requests efficiently

  • improves responsiveness

  • scales better under load

This is why Node.js prefers non-blocking behavior.


Visual Understanding

Blocking Execution

Request 1 → Wait → Done  
Request 2 → Wait → Done  
Request 3 → Wait → Done

Non-Blocking Execution

Request 1 → Start → Continue  
Request 2 → Start → Continue  
Request 3 → Start → Continue  

Results handled when ready

The Core Idea

Node.js does not try to finish one task before starting another.

It makes sure no time is wasted waiting.

This is the foundation of:

  • event loop

  • concurrency

  • high performance


Final Thought

Blocking code makes your server slow and unresponsive.

Non-blocking code keeps your system moving.

The difference is not just technical. It directly impacts how your application behaves under real-world load.

Understanding this is essential before moving deeper into Node.js internals.

Node.js and Backend Development

Part 4 of 5

Node.js & Backend Development is a practical series designed to take you from understanding the basics of Node.js to building real-world, production-ready backend systems. You’ll learn how to create APIs, work with Express.js, handle requests, manage databases, implement authentication, and build scalable applications step by step.

Up next

The Node.js Event Loop Explained

How JavaScript Handles Everything Behind the Scenes