Node.js Advance : Concurrency vs Parallelism in Hindi

Other Blogs

Blogs ❯❯ NodeJS

Image could not load

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 करेंगे।

What इस Concurrency?

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 होती है।

What is Parallelism?

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 Architecture : Power of Concurrency 

Node.js का core engine V8 है, और उसके नीचे होता है libuv, जो event loop, async I/O और thread pool manage करता है।

📌 Working flow -
  • आप 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 होता है।

Real Examples : Concurrency vs Parallelism in Node.js

Concurrency Example (Non-blocking I/O)

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.

Parallelism Example (CPU-bound Task with Worker Threads)

// 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.

💡 When to Use What?

ScenarioUse ConcurrencyUse Parallelism

File I/O

Database queries

Encrypting large data chunks

Real-time chat system

Video compression

I hope, आपको ये blog पसंद आया होगा।

Hey ! I'm Rahul founder of learnhindituts.com. Working in IT industry more than 5.5 years. I love to talk about programming as well as writing technical tutorials and blogs that can help to others .... keep learning :)

Get connected with me - LinkedIn Twitter Instagram Facebook

Your Thought ?

Please wait . . .

    Recent Blogs

    Loading ...

    0 Comment(s) found !