Skip to main content

Command Palette

Search for a command to run...

Blocking vs Non-Blocking Code in Node.js

Updated
3 min read
Blocking vs Non-Blocking Code in Node.js

When working with Node.js, one of the most important concepts is understanding blocking and non-blocking code.

This concept directly affects how fast and scalable your application is.

  • Blocking = waiting for a task to finish

  • Non-blocking = continuing other tasks while waiting


What is Blocking Code?

Blocking code means:

One task must finish before the next task starts.

In blocking behavior, the program stops and waits.

Example:

If a file is being read, the server will wait until the file is fully read before doing anything else.

Blocking Code Example (File Read)

import fs from 'node:fs';

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

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

Here:

  • The server waits for file reading

  • Only then next line executes

Blocking Execution Timeline

Task 1 (file read - 5s) 
      ↓ 
Server waits 
      ↓ 
Task 2 (API request) waits 
      ↓ 
Task 3 waits

Everything stops until previous task is done

Problem with Blocking Code

Blocking code causes:

  • Slow server response

  • Poor performance under load

  • Users waiting unnecessarily

  • One task delaying all others

In real applications, this is very inefficient.


What is Non-Blocking Code?

Non-blocking code means:

The program does not wait for a task to finish. It continues executing other tasks.

Asynchronous or time taken operations are handled in the background.

Non-Blocking Code Example (File Read)

import fs from 'node:fs';

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

console.log("This runs immediately");

Here:

  • File reading happens in background

  • Program continues execution

  • Callback runs when file is ready

Non-Blocking Execution Timeline

Task 1 starts (file read) 
        ↓ 
Server continues Task 2 
        ↓ 
Server continues Task 3 
        ↓ 
Task 1 completes → callback runs

No waiting, everything flows smoothly


Why Blocking Slows Servers

In a server environment:

  • Many users send requests at the same time

  • If one request blocks the system

  • All other requests must wait

Result:

  • Slow website

  • Delayed responses

  • Poor user experience


Blocking vs Non-Blocking

Feature Blocking Code Non-Blocking Code
Execution One by one Parallel-like flow
Waiting Yes No
Performance Slow under load Fast and scalable
Server behavior Stops for each task Continues execution

Async Operations in Node.js

Node.js uses non-blocking asynchronous operations for tasks like:

  • File reading/writing

  • Database queries

  • API calls

  • Network requests

These operations do not stop the main thread.


Why Node.js Uses Non-Blocking Model

Node.js is designed to:

  • Handle many users efficiently

  • Avoid waiting for slow operations

  • Improve performance using async callbacks and event loop

That is why Node.js is popular for real-time applications.

Conclusion

Blocking and non-blocking code are fundamental concepts in Node.js. Blocking code waits for tasks to complete and slows down the server, while non-blocking code continues execution and handles tasks in the background, making applications fast and scalable.

Non-blocking behavior is the key reason Node.js can handle thousands of requests efficiently without freezing or slowing down.