If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
Concurrency vs Parallelism by Favour Usifo on Unsplash
अगर आप backend developer हो या किसी DSA-based Node.js course का हिस्सा हो, तो एक सवाल आपको बार-बार सुनने को मिलेगा:
"Node.js तो single-threaded है. फिर ये concurrent और parallel कैसे हो सकता है?"
बहुत सही question है — और इसका जवाब समझना senior-level interviews के लिए must-have knowledge है।
इस blog में हम -
Concurrency और Parallelism के बीच का clear difference समझेंगे,
देखेंगे Node.js में दोनो कैसे काम करते हैं,
समझेंगे libuv, Worker Threads, और Child Processes का role,
और real examples के साथ topic को master करेंगे।
●●●
Concurrency का मतलब है एक ही thread पर multiple tasks को manage करना, बिना किसी को block किये। Tasks interleaved तरीके से चलते हैं।
Node.js में concurrency का magic होता है event loop + non-blocking I/O के through.
जब एक task I/O के लिए wait करता है (e.g., file read, DB call), तब दूसरा task उसका slot ले लेता है।
इससे CPU idle नहीं होता, Performance better होती है।
Parallelism का मतलब है multiple tasks को अलग-अलग threads पर एक साथ run करना — specially useful for CPU-bound tasks.
Node.js में -
JavaScript single-threaded होता है,
But backend में libuv होता है, जो multi-threaded operations handle करता है,
और आप use कर सकते हो -
Worker Threads (same process में background threads),
Child Processes (अलग-अलग Node.js processes).
●●●
Node.js का core engine V8 है, और उसके नीचे होता है libuv, जो event loop, async I/O और thread pool manage करता है।
आप code लिखते हो JavaScript में (readFile(), API request, etc).
Node.js event loop request को handle करता है।
अगर task blocking है (I/O), तो libuv उससे thread pool में send देता है।
Callback complete hone के बाद result JavaScript thread पर return होता है।
●●●
const fs = require('fs');
console.log("Start reading file...");
fs.readFile('demo.txt', 'utf8', (err, data) => {
if (err) throw err;
console.log("File content:", data);
});
console.log("Do something else...");Output
Start reading file... Do something else... File content: Hello world!
File reading background में गया, और process ने दूसरे काम continue किये. ये है concurrency.
// heavy-task.js
const { parentPort } = require('worker_threads');
function computeHeavyTask() {
let sum = 0;
for (let i = 0; i < 1e9; i++) sum += i;
return sum;
}
parentPort.postMessage(computeHeavyTask());// main.js
const { Worker } = require('worker_threads');
console.log("Main thread started");
const worker = new Worker('./heavy-task.js');
worker.on('message', (result) => {
console.log("Worker result:", result);
});
console.log("Main thread doing other work...");Output
Main thread started Main thread doing other work... Worker result: 499999999500000000
Heavy computation को background thread में send दिया using Worker Thread. ये है parallelism.
●●●
| Scenario | Use Concurrency | Use Parallelism |
File I/O | ✅ | ❌ |
Database queries | ✅ | ❌ |
Encrypting large data chunks | ❌ | ✅ |
Real-time chat system | ✅ | ❌ |
Video compression | ❌ | ✅ |
I hope, आपको ये blog पसंद आया होगा।
●●●
Loading ...