QA
Beginner LevelNode JS

How does the node:cluster module scale applications across multi-core CPUs?

❓ Interview Prompt: Explain `node:cluster` module, Primary master process vs Worker processes, `cluster.fork()`, round-robin IPC load balancing.

Because Node.js runs on a single thread, a single instance utilizes ONLY ONE CPU core. The 'node:cluster' module enables spawning worker processes sharing the same server port:

Master/Worker Architecture:

•
Primary (Master): Manages worker lifecycles and distributes incoming TCP connections via round-robin IPC.
•
Workers: Individual Node.js processes running on separate CPU cores.

Code Implementation Example

javascript
import cluster from 'node:cluster';
import http from 'node:http';
import os from 'node:os';

if (cluster.isPrimary) {
  const numCPUs = os.cpus().length;
  console.log('Primary ${process.pid} forking ${numCPUs} workers...');
  for (let i = 0; i < numCPUs; i++) cluster.fork();
} else {
  http.createServer((req, res) => {
    res.end('Handled by worker PID ${process.pid}');
  }).listen(8000);
}
💡Forking HTTP server workers across multi-core CPUs using node:cluster.
🎯 Interview Key Takeaway

Use node:cluster or PM2 to scale Node.js applications across all available CPU cores on bare-metal servers.

#cluster#Multi-Core#Scaling#IPC#Beginner
Back to all Node JS Questions→

More Node JS Interview Questions

Beginner LevelNode JS

How does the Node.js Event Loop work? Explain the 6 execution phases (Timers, Pending, Poll, Check, Close).

Beginner LevelNode JS

What does 'Single-Threaded, Event-Driven, Non-Blocking I/O' mean in Node.js?

Beginner LevelNode JS

What is the difference between process.nextTick() and setImmediate()?