Skip to main content

Command Palette

Search for a command to run...

Blocking vs Non-Blocking Code in Node.js ⚑

Understanding How Node.js Handles Work Efficiently

Updated
β€’5 min read
Blocking vs Non-Blocking Code in Node.js ⚑

πŸ“Œ Introduction

When building backend applications, performance matters a lot.

πŸ‘‰ How fast your server responds πŸ‘‰ How many users it can handle πŸ‘‰ How efficiently it processes requests

A big reason Node.js performs well is because it uses:

Non-blocking execution

But what does that actually mean?

Let’s break it down simply.


β›” What is Blocking Code?

Blocking code means:

One task must finish before the next one starts.


🧩 Simple Meaning:

πŸ‘‰ β€œWait for this task to complete before moving on.”


πŸ“¦ Example of Blocking Code

const fs = require("fs");

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

console.log("This runs after file is read");

🧠 What happens here?

Read file (WAIT)
   ↓
Print file content
   ↓
Print next line

⚠️ Problem:

  • Everything waits for file reading

  • Server becomes slow

  • Other requests are stuck


πŸš€ What is Non-Blocking Code?

Non-blocking code means:

The server does NOT wait for a task to finish.


🧩 Simple Meaning:

πŸ‘‰ β€œStart task, move on, come back later.”


πŸ“¦ Example of Non-Blocking Code

const fs = require("fs");

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

console.log("This runs immediately");

🧠 What happens here?

Start reading file
   ↓
Move to next line immediately
   ↓
File finishes β†’ callback runs

βš–οΈ Blocking vs Non-Blocking (Core Difference)

Feature Blocking Non-Blocking
Execution Sequential Concurrent
Waiting Yes No
Performance Slower Faster
Scalability Low High

🍽️ Real-Life Analogy

πŸ” Restaurant Example:


β›” Blocking System:

  • One waiter serves one customer completely

  • Next customer waits

πŸ‘‰ Slow service 😡


⚑ Non-Blocking System:

  • Waiter takes all orders quickly

  • Kitchen prepares in background

  • Orders served when ready

πŸ‘‰ Fast service πŸš€


πŸ”„ Why Blocking Slows Servers

Imagine 100 users hitting your server:


β›” Blocking:

User 1 β†’ wait
User 2 β†’ wait
User 3 β†’ wait

πŸ‘‰ Each request delays the next one


⚑ Result:

  • Poor performance

  • High response time

  • Server bottleneck


πŸš€ How Node.js Handles Non-Blocking

Node.js uses:

πŸ‘‰ Event loop πŸ”„ πŸ‘‰ Async operations βš™οΈ πŸ‘‰ Callback system πŸ“ž


🧠 Key Idea:

πŸ‘‰ Node.js delegates heavy tasks to background workers


πŸ”„ Async Operations in Node.js

Common non-blocking tasks:


πŸ“‚ 1. File System Operations

fs.readFile()
fs.writeFile()

🌐 2. API Calls

fetch("https://api.example.com")

πŸ—„οΈ 3. Database Queries

db.find()

πŸ“Š Blocking vs Non-Blocking Execution Timeline


β›” Blocking Timeline:

Request 1 β†’ WAIT β†’ Finish
Request 2 β†’ WAIT
Request 3 β†’ WAIT

⚑ Non-Blocking Timeline:

Request 1 β†’ Start
Request 2 β†’ Start
Request 3 β†’ Start
   ↓
Responses come back later

🌍 Real-World Example

πŸ“‚ File Reading Server


β›” Blocking Version:

const data = fs.readFileSync("data.txt", "utf8");
res.send(data);

πŸ‘‰ Server cannot handle other requests until file is read


⚑ Non-Blocking Version:

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

πŸ‘‰ Server stays free to handle more requests


🧠 Why Node.js Uses Non-Blocking Model

Because Node.js is designed for:

  • High traffic apps πŸš€

  • Real-time systems πŸ’¬

  • Fast APIs 🌐


⚠️ Important Concept

Non-blocking does NOT mean:

πŸ‘‰ β€œEverything runs at the same time”

It means:

πŸ‘‰ β€œWork is delegated so the main thread is never stuck”


🎯 Key Takeaways

  • Blocking code waits for tasks to finish

  • Non-blocking code continues execution

  • Node.js uses async operations to avoid waiting

  • Event loop enables concurrency

  • Non-blocking improves scalability and performance


✍️ Practice Challenge

Try this:

πŸ‘‰ Write a program that:

  • Reads a file synchronously

  • Then rewrite it asynchronously

  • Observe the difference in output order


πŸ”₯ Conclusion

Blocking vs non-blocking is one of the most important concepts in Node.js.

πŸ‘‰ Blocking = waiting and slowing down πŸ‘‰ Non-blocking = fast and scalable execution

This design choice is a big reason why Node.js powers modern high-performance web applications πŸš€


Happy Coding ⚑✨

How the Web Works πŸ•ΈοΈπŸ•ΈοΈ

Part 3 of 50

A practical web development series explaining how the web worksβ€”from DNS and browsers to servers, HTTP, APIs, and deploymentβ€”while clearly connecting these fundamentals to real-world website programming using frontend and backend examples.

Up next

REST API Design Made Simple with Express.js πŸš€

Building Clean and Scalable Backend APIs