Mind~G
NodeJs

Cluster

What is Cluster?

Cluster makes Node.js faster and stronger by using all parts of your computer's CPU cores.

  • One process is the boss (master) that starts many small workers
  • Each worker can handle requests
  • If one worker stops, the boss makes a new one

The Problem

By default Node.js runs on one single thread. That means it uses only one CPU core even if your computer has many cores.

So if your system has 4 cores, Node.js will still use only one. That can make your app slower when many users come together.

The Solution

To fix this we use the Cluster module. It allows Node.js to create multiple processes that all share the same server port. Each process runs a copy of your app and can handle requests separately.

So instead of one worker, you now have many workers handling requests.

How it Works

There are two main roles in a cluster:

Master process (the boss)

  • Creates worker processes and gives them jobs

Worker processes (the workers)

  • Each worker handles client requests independently but shares the same server port
  • If a worker crashes, the master can replace it

Why We Use Cluster

  • To use all CPU cores of the system
  • To handle more requests at the same time
  • To make the server more reliable (if one worker crashes, others keep running)
  • Improves performance and makes full use of the hardware

On this page