🔍 Why Do We Need These Features?

Node.js is single-threaded, which means it handles tasks one at a time in the event loop. This works well for many applications but can become a problem when:

To solve this, Node.js provides child processes, worker threads, and clustering.

👶 Child Processes

In simple words:

When to use:

Example (Child Process):

// Using child_process
const { exec } = require('child_process');

exec('node -v', (error, stdout, stderr) => {
  if (error) {
    console.error(`Error: ${error.message}`);
    return;
  }
  console.log(`Node.js version: ${stdout}`);
});

This code runs the command node -v in a separate process and prints the version of Node.js.

🧵 Worker Threads

In simple words:

When to use:

Example (Worker Threads):

// Using worker_threads
const { Worker } = require('worker_threads');

const worker = new Worker(`
  const { parentPort } = require('worker_threads');
  let sum = 0;
  for (let i = 0; i < 1e9; i++) {
    sum += i;
  }
  parentPort.postMessage(sum);
`, { eval: true });

worker.on('message', (result) => {
  console.log(`Sum: ${result}`);
});

This worker thread calculates a big sum without blocking the main thread.

⚡ Clustering

When to use:

Example (Clustering):

// Using cluster
const cluster = require('cluster');
const http = require('http');
const os = require('os');

if (cluster.isMaster) {
  const numCPUs = os.cpus().length;
  console.log(`Master process running on PID: ${process.pid}`);

  for (let i = 0; i < numCPUs; i++) {
    cluster.fork();
  }

  cluster.on('exit', (worker) => {
    console.log(`Worker ${worker.process.pid} died, starting a new one...`);
    cluster.fork();
  });
} else {
  http.createServer((req, res) => {
    res.writeHead(200);
    res.end(`Handled by worker: ${process.pid}`);
  }).listen(3000);

  console.log(`Worker started on PID: ${process.pid}`);
}

This code creates a clustered HTTP server where each worker runs on a separate CPU core.

📊 Key Differences

Feature Child Processes Worker Threads Clustering
Memory Sharing ❌ No ✅ Yes ❌ No
Best For Running scripts, system tasks CPU-heavy tasks, parallel computations Scaling web servers
Communication Messages Messages + Shared memory Messages
Example Use Case Run python script, file compression Image processing, encryption Handling thousands of HTTP requests

📝 Summary

In Node.js, you can improve performance and handle heavy tasks by using child processes, worker threads, and clustering. Child processes are best for running external programs, worker threads are perfect for CPU-heavy calculations with shared memory, and clustering helps scale applications across multiple CPU cores. By choosing the right approach, you can make your Node.js application faster, more reliable, and better at handling large workloads.