Cluster Module

Back

Loading concept...

🚀 Node.js Cluster Module: Making Your Server Super Strong!

The Restaurant Kitchen Story 🍳

Imagine you own a tiny restaurant with just ONE chef. When 100 hungry customers show up, your single chef works so hard but can only cook one dish at a time. Customers wait forever. Chaos!

Now imagine you hire 8 chefs who all work together in the same kitchen. Each chef handles different orders at the same time. Customers get food fast. Everyone is happy!

That’s exactly what the Node.js Cluster Module does for your server!


🎯 What is the Cluster Module?

Node.js runs on a single thread (one chef). But your computer has multiple CPU cores (multiple kitchens). Without clustering, you’re wasting power!

The Cluster Module lets you create copies of your server (called workers) that share the work. If you have 8 CPU cores, you can have 8 workers handling requests together.

graph TD A["😊 Users"] --> B["Primary Process"] B --> C["Worker 1"] B --> D["Worker 2"] B --> E["Worker 3"] B --> F["Worker 4"] C --> G["🖥️ CPU Core 1"] D --> H["🖥️ CPU Core 2"] E --> I["🖥️ CPU Core 3"] F --> J["🖥️ CPU Core 4"]

Simple Truth:

  • Without Cluster: 1 worker = uses 1 CPU core
  • With Cluster: Many workers = uses ALL CPU cores

🔧 Cluster Module Overview

Think of the cluster module as a factory manager. The manager (primary) doesn’t make products—they organize the workers who do!

How to Start Using It

const cluster = require('cluster');
const os = require('os');

// How many workers can we create?
const numCPUs = os.cpus().length;
console.log(`We have ${numCPUs} CPU cores!`);

Key Properties You’ll Use

Property What It Does
cluster.isPrimary Am I the boss?
cluster.isWorker Am I a helper?
cluster.workers List of all helpers
cluster.worker Current helper’s info

👨‍👩‍👧‍👦 Primary and Worker Detection

Here’s the magic question: How does your code know if it’s the boss or a helper?

The Simple Answer

const cluster = require('cluster');

if (cluster.isPrimary) {
    console.log('🎩 I am the PRIMARY!');
    console.log('My job: create workers');
} else {
    console.log('👷 I am a WORKER!');
    console.log('My job: handle requests');
}

Why This Matters

When you run your Node.js file, it starts as the primary. When the primary creates workers using fork(), each worker runs the SAME file but enters the else branch!

graph TD A["Run server.js"] --> B{cluster.isPrimary?} B -->|Yes| C["Create Workers"] B -->|No| D["Handle HTTP Requests"] C --> E["Fork Worker 1"] C --> F["Fork Worker 2"] E --> D F --> D

Real Example

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

if (cluster.isPrimary) {
    console.log(`Primary ${process.pid} running`);

    // Create one worker per CPU
    for (let i = 0; i < os.cpus().length; i++) {
        cluster.fork();
    }
} else {
    // Workers handle HTTP requests
    http.createServer((req, res) => {
        res.end(`Hello from worker ${process.pid}`);
    }).listen(3000);

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

🍴 cluster.fork() - Creating Workers

cluster.fork() is like saying: “Create a copy of me to help with work!”

Basic Fork

if (cluster.isPrimary) {
    // Create 4 workers
    cluster.fork();
    cluster.fork();
    cluster.fork();
    cluster.fork();
}

Smarter Fork (Match CPU Count)

if (cluster.isPrimary) {
    const numCPUs = os.cpus().length;

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

    console.log(`Created ${numCPUs} workers`);
}

What Happens When You Fork?

  1. Primary calls cluster.fork()
  2. Node.js creates a NEW process
  3. New process runs the SAME file
  4. But cluster.isPrimary is now false!
  5. New process becomes a worker

Passing Environment Variables

// Primary can send data to workers
cluster.fork({ WORKER_TYPE: 'api' });
cluster.fork({ WORKER_TYPE: 'background' });

👷 Worker Processes

Workers are the real heroes! They do the actual work while the primary just manages them.

Worker Properties

if (cluster.isWorker) {
    console.log(`Worker ID: ${cluster.worker.id}`);
    console.log(`Process ID: ${process.pid}`);
}

Tracking All Workers (Primary Side)

if (cluster.isPrimary) {
    cluster.fork();
    cluster.fork();

    // List all workers
    for (const id in cluster.workers) {
        console.log(`Worker ${id} is running`);
    }
}

Worker Events

cluster.on('online', (worker) => {
    console.log(`Worker ${worker.id} is online!`);
});

cluster.on('exit', (worker, code) => {
    console.log(`Worker ${worker.id} died`);
    // Create a new worker to replace it
    cluster.fork();
});

Communication Between Primary and Workers

// Primary sends message to worker
if (cluster.isPrimary) {
    const worker = cluster.fork();
    worker.send({ hello: 'worker!' });
}

// Worker receives message
if (cluster.isWorker) {
    process.on('message', (msg) => {
        console.log('Message from primary:', msg);
    });
}

⚖️ Load Balancing

When many requests come in, how do workers share the work fairly? That’s load balancing!

Node.js Does It Automatically!

Good news: Node.js cluster module has built-in load balancing. You don’t need to do anything special!

graph TD A["Incoming Requests"] --> B["Primary Process"] B -->|Request 1| C["Worker 1"] B -->|Request 2| D["Worker 2"] B -->|Request 3| E["Worker 3"] B -->|Request 4| F["Worker 4"] B -->|Request 5| C B -->|Request 6| D

Two Load Balancing Methods

1. Round-Robin (Default on Linux/Mac)

  • Requests go to workers in order: 1, 2, 3, 4, 1, 2, 3, 4…
  • Very fair!

2. OS-Scheduled (Windows)

  • Operating system decides which worker gets work
  • Less predictable but works well

See Load Balancing in Action

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

if (cluster.isPrimary) {
    for (let i = 0; i < os.cpus().length; i++) {
        cluster.fork();
    }
} else {
    let requestCount = 0;

    http.createServer((req, res) => {
        requestCount++;
        res.end(`Worker ${process.pid}: ${requestCount} requests`);
    }).listen(3000);
}

Now if you hit your server many times, you’ll see different workers handling different requests!


🎪 Complete Example: Production-Ready Cluster

Here’s everything working together:

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

const numCPUs = os.cpus().length;

if (cluster.isPrimary) {
    console.log(`🎩 Primary ${process.pid} starting`);
    console.log(`   Creating ${numCPUs} workers...`);

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

    // Handle worker death
    cluster.on('exit', (worker) => {
        console.log(`💀 Worker ${worker.id} died`);
        console.log('   Starting replacement...');
        cluster.fork();
    });

} else {
    // Worker code
    http.createServer((req, res) => {
        res.writeHead(200);
        res.end(`Worker ${cluster.worker.id} handled this!`);
    }).listen(3000);

    console.log(`👷 Worker ${cluster.worker.id} ready`);
}

🎓 Quick Summary

Concept What It Does Example
Cluster Module Lets you run multiple workers require('cluster')
Primary The manager that creates workers cluster.isPrimary
Worker Does the actual work cluster.isWorker
fork() Creates a new worker cluster.fork()
Load Balancing Shares work fairly among workers Automatic!

💡 Remember This!

One process can only use one CPU core.

The Cluster Module creates multiple processes.

Multiple processes = Multiple CPU cores = Much faster!

It’s like going from one chef to a whole kitchen team. Your server can now handle way more customers without breaking a sweat! 🚀


🎯 Key Takeaways

  1. ✅ Use cluster.isPrimary to check if you’re the boss
  2. ✅ Use cluster.fork() to create helpers
  3. ✅ Workers share the same port automatically
  4. ✅ Load balancing happens by itself
  5. ✅ If a worker dies, create a new one!

You’re now ready to make your Node.js server super powerful! 💪

Loading story...

Story - Premium Content

Please sign in to view this story and start learning.

Upgrade to Premium to unlock full access to all stories.

Stay Tuned!

Story is coming soon.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.