codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🚂

Express.js

42 / 76 topics
40Scaling Express.js Applications41Load Balancing with Nginx42Node.js Cluster Module
Tutorials/Express.js/Node.js Cluster Module
🚂Express.js

Node.js Cluster Module

Updated 2026-05-15
10 min read

Node.js Cluster Module

Introduction

In the world of web development, scalability is a critical factor that determines how well an application can handle increased load. One of the primary ways to achieve scalability in Node.js applications is by utilizing the built-in cluster module. This module allows you to create child processes (workers) that share the same server port and distribute incoming requests among them.

Concept

Node.js runs on a single-threaded event loop, which means it can only handle one request at a time. While this model is efficient for I/O-bound tasks, it becomes a bottleneck when dealing with CPU-intensive operations. The cluster module addresses this by enabling you to create multiple worker processes that share the same server port and distribute incoming requests among them.

Each worker process runs independently and shares the same memory space as the master process. This allows workers to communicate with each other using IPC (Inter-Process Communication) channels, which can be useful for sharing data or coordinating tasks.

Examples

Basic Cluster Setup

Let's start by setting up a basic cluster that creates a specified number of worker processes and distributes incoming requests among them.

JavaScript
1const cluster = require('cluster');
2const http = require('http');
3const numCPUs = require('os').cpus().length;
4
5if (cluster.isMaster) {
6console.log(`Master ${process.pid} is running`);
7
8// Fork workers.
9for (let i = 0; i < numCPUs; i++) {
10 cluster.fork();
11}
12
13cluster.on('exit', (worker, code, signal) => {
14 console.log(`worker ${worker.process.pid} died`);
15});
16} else {
17// Workers can share any TCP connection
18// In this case it is an HTTP server
19http.createServer((req, res) => {
20 res.writeHead(200);
21 res.end('hello world
22');
23}).listen(8000);
24
25console.log(`Worker ${process.pid} started`);
26}

In this example:

  1. We check if the current process is the master process using cluster.isMaster.
  2. If it is the master, we fork a number of worker processes equal to the number of CPU cores.
  3. Each worker process creates an HTTP server that listens on port 8000 and responds with "hello world".
  4. The master process logs when a worker exits.

Handling Worker Events

You can handle various events related to workers, such as exit, message, and listening. Here's an example of how to handle the exit event:

JavaScript
1cluster.on('exit', (worker, code, signal) => {
2console.log(`worker ${worker.process.pid} died`);
3// Optionally, you can restart the worker
4cluster.fork();
5});

Communicating Between Workers

Workers can communicate with each other using IPC channels. Here's an example of how to send a message from one worker to another:

JavaScript
1if (cluster.isMaster) {
2// Fork workers.
3for (let i = 0; i < numCPUs; i++) {
4 const worker = cluster.fork();
5 if (i === 0) {
6 // Send a message to the first worker
7 worker.send({ type: 'greeting', message: 'Hello from master!' });
8 }
9}
10} else {
11process.on('message', (msg) => {
12 console.log(`Message from master: ${msg.message}`);
13});
14
15// Send a message back to the master
16process.send({ type: 'response', message: 'Hello from worker!' });
17}

In this example:

  1. The master process sends a greeting message to the first worker.
  2. Each worker listens for messages and logs them.
  3. Workers can also send messages back to the master or other workers.

What's Next?

Now that you have a good understanding of how to use the Node.js cluster module, you can explore more advanced caching strategies to further improve the performance and scalability of your applications. Consider topics like shared memory, distributed caches, and load balancing techniques.

By leveraging the power of the cluster module and other best practices, you can build robust and scalable Node.js applications that can handle high traffic efficiently.


PreviousLoad Balancing with NginxNext Advanced Caching Strategies

Recommended Gear

Load Balancing with NginxAdvanced Caching Strategies