
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
- Client Request is received.
- Passed to the event loop.
- Blocking operations are delegated to the libuv thread pool.
- Once the operation is complete, its callback is added to the callback queue.
- 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
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