01 / 17

How does the Node.js Cluster module work internally?

The Cluster module works by having a master process fork multiple worker processes using child_process.fork() under the hood. Workers share the same server port via IPC, and the master distributes incoming connections using either a round-robin or OS-handled scheduling policy.

Internally, when cluster.fork() is called, the master process spawns a new child process using child_process.fork(), which creates a full copy of the Node.js process. The master process binds to the TCP port and passes connection handles to workers via IPC (Inter-Process Communication) sockets. Workers never directly bind to the port — they receive connection handles from the master.

Internal Working Steps
  1. 1

    Master process starts and calls cluster.fork() N times

  2. 2

    Each fork creates a new OS-level process using child_process.fork()

  3. 3

    Master process binds to the configured port

  4. 4

    Incoming connections are received by the master

  5. 5

    Master distributes connections to workers via IPC using a scheduling policy

  6. 6

    Workers process the request and send responses directly to the client

Scheduling Policies
  1. 1

    SCHED_RR (Round-Robin): Default on Linux/macOS — master distributes connections evenly across workers

  2. 2

    SCHED_NONE: Default on Windows — OS decides which worker gets the connection