Login

Sign Up

Architecture and  Blocking vs Non-Blocking in Node.js
Ujjwal Paliwal

Posted on Apr 29, 2025 | Backend

Architecture and Blocking vs Non-Blocking in Node.js

Node.js is built on a non-blocking, event-driven architecture, making it highly efficient and scalable for I/O-heavy applications.

1. Single-Threaded Event Loop

Node.js uses a single-threaded architecture with an event loop for handling multiple concurrent clients.

  • Powered by libuv, which manages background tasks (like file I/O and network operations).
  • Enables non-blocking I/O so Node.js can continue executing other tasks without waiting for long-running operations to finish.

2. Components of Node.js Architecture

a. V8 JavaScript Engine

  • Developed by Google (used in Chrome).
  • Compiles JavaScript to machine code for fast execution.
  • Executes all JavaScript code within Node.js.

b. Event Loop

  • Core of Node.js’ asynchronous processing.
  • Continuously checks the callback queue and pushes callbacks to the call stack when it's free.

c. Libuv

A C++ library that provides:

  • Event loop
  • Thread pool
  • Async I/O operations
  • Handles file system, DNS, and network tasks in the background.

d. Callback Queue

  • Holds all the callbacks ready for execution.
  • The event loop picks them up when the call stack is empty.

e. Thread Pool

  • Node.js has a default thread pool of 4 threads (configurable).
  • Handles expensive or blocking operations (like file I/O) outside the main thread.

3. Node.js Execution Flow

  1. Client Request is received.
  2. Passed to the event loop.
  3. Blocking operations are delegated to the libuv thread pool.
  4. Once the operation is complete, its callback is added to the callback queue.
  5. The event loop sends the callback to the call stack for execution by the V8 engine.

4. Node.js Modules

  • Built-in Modules: fs, http, events, etc.
  • Third-party Modules: Available via npm.
  • Custom Modules: User-defined modules.

Node.js Architecture Diagram

image

Understanding Blocking vs Non-Blocking in Node.js Architecture

πŸ”’ Blocking Request (Synchronous)

  • The event loop waits for the task to complete.
  • No other code executes until the operation finishes.
  • Typically uses:
    • fs.readFileSync
    • fs.writeFileSync

⚑ Non-Blocking Request (Asynchronous)

  • The event loop doesn’t wait.
  • Task is sent to the background (handled by libuv and thread pool).
  • Node.js continues executing other code.
  • Uses:
    • Callbacks
    • Promises
    • async/await

Blocking Architectural Flow

Client Request
      ↓
  Event Loop
      ↓
Blocking Operation (e.g., fs.readFileSync)
      ↓
Wait until Done
      ↓
Execute Next

Non-Blocking Architectural Flow

Client Request
      ↓
  Event Loop
      ↓
Non-Blocking Operation (e.g., fs.readFile)
      ↓
Send to Thread Pool (libuv)
      ↓
Continue Execution
      ↓
Callback/Event Triggered Later

Code Examples

Blocking Example (Synchronous)

const fs = require('fs');

console.log('Start Reading (Blocking)');

const data = fs.readFileSync('example.txt', 'utf8'); // blocking call

console.log('File Content:', data);
console.log('End Reading (Blocking)');

Output

Start Reading (Blocking)
(File content here)
End Reading (Blocking)

During file reading, nothing else runs.

Non-Blocking example(Asynchronous)

const fs = require('fs');

console.log('Start Reading (Non-Blocking)');

fs.readFile('example.txt', 'utf8', (err, data) => {
  if (err) throw err;
  console.log('File Content:', data);
});

console.log('End Reading (Non-Blocking)');

Output

Start Reading (Non-Blocking)
End Reading (Non-Blocking)
File Content: (File content here)

Node.js continues to End Reading before the file is fully read.


Blocking vs Non-Blocking Summary Table

Feature Blocking Non-Blocking
Code Execution Pauses until done Continues immediately
Performance Slower for many requests Scales well with load
Method Example fs.readFileSync() fs.readFile()
Used In Startup scripts, configs Web servers, APIs

I hope it is usefull for you basic!
jay hanuman!

3 Reactions

0 Bookmarks

Read next

Ujjwal Paliwal

Ujjwal Paliwal

Dec 14, 24

4 min read

|

Building an Own AI Chatbot: Integrating Custom Knowledge Bases

Ujjwal Paliwal

Ujjwal Paliwal

Dec 15, 24

9 min read

|

Exploratory data analysis with Pandas:Part 1