Skip to main content

Blocking vs Non-Blocking Code in Node.js

Updated 4 min readMarkdown

What is Blocking Code?

Blocking code stops execution until a task is completed.

Imagine ordering food and the waiter stands beside the kitchen doing absolutely nothing until your order is ready.

That’s blocking behavior.

Example:

const fs = require("fs");

console.log("Start");

const data = fs.readFileSync("data.txt", "utf8");
console.log(data);

console.log("End");

Output:

Start
[file content]
End

Here:

  • Node.js waits for file reading to finish

  • Execution pauses

  • Other tasks cannot continue meanwhile

This blocks the thread.


Why Blocking Slows Servers

Servers handle many users simultaneously.

If one request blocks execution:

User 1 → File Read → Waiting
User 2 → Forced to wait
User 3 → Forced to wait

One slow task delays everything behind it.

This reduces:

  • Performance

  • Scalability

  • Response speed

Blocking operations become dangerous in large applications.

Especially when dealing with:

  • Database queries

  • API calls

  • File handling


What is Non-Blocking Code?

Non-blocking code allows Node.js to continue executing other tasks while waiting for an operation to complete.

Instead of waiting beside the kitchen, the waiter takes more orders while food cooks.

That’s non-blocking behavior.

Example:

const fs = require("fs");

console.log("Start");

fs.readFile("data.txt", "utf8", (err, data) => {
    console.log(data);
});

console.log("End");

Output:

Start
End
[file content appears later]

Node.js continues execution instead of waiting.

This is asynchronous behavior.


Async Operations in Node.js

Node.js handles many operations asynchronously.

Examples:

  • File reading

  • Database calls

  • API requests

  • Timers

  • Network operations

These operations are delegated and completed later.

Meanwhile, Node.js keeps handling new requests.

This is one reason Node.js performs well in real-time applications and APIs.


File Handling Example


Blocking File Read

const data = fs.readFileSync("data.txt", "utf8");
console.log(data);

Execution stops until file reading finishes.


Non-Blocking File Read

fs.readFile("data.txt", "utf8", (err, data) => {
    console.log(data);
});

Execution continues immediately.

File content appears later through a callback.


Database Call Example

Imagine fetching user data from a database.

Blocking behavior:

Request waits until DB responds

Non-blocking behavior:

DB query starts
↓
Node.js handles other requests
↓
DB response returns later

This makes applications more scalable.


Why Non-Blocking is Important in Node.js

Node.js uses a single-threaded event loop.

If blocking operations happen frequently, the entire server slows down.

Non-blocking code allows Node.js to efficiently manage:

  • Thousands of requests

  • Real-time communication

  • Streaming

  • APIs

  • Chat applications

Without freezing execution.


Final Thoughts

Blocking code waits. Non-blocking code keeps moving.

That difference is one of the biggest reasons Node.js became popular for backend development.

Node.js performs best when applications rely heavily on asynchronous operations like:

  • Database queries

  • File handling

  • Network requests

Understanding blocking vs non-blocking behavior helps developers write faster and more scalable applications.

Because one accidental blocking operation inside a busy server can turn your backend into a digital traffic jam faster than college WiFi during placement season.


Quick Recap

Concept Meaning
Blocking Code Stops execution until task finishes
Non-Blocking Code Continues execution while task runs
Async Operations Tasks completed later using callbacks
Blocking Problem Slows server performance
Best Practice Prefer non-blocking operations in Node.js

Suggested Diagram Ideas

1. Blocking Execution Timeline

Task 1 → Waiting → Finish
                  ↓
Task 2 Starts

2. Non-Blocking Execution Timeline

Task 1 → Processing in background
Task 2 → Executes immediately
Task 3 → Executes immediately
Task 1 → Finishes later

3. File Read Comparison

Blocking:
Read File → Wait → Continue

Non-Blocking:
Read File → Continue Execution → Callback Lat

1 view

No comments yet. Be the first to comment.