15 / 17

What is the difference between cluster.fork() and child_process.fork()?

cluster.fork() is built on top of child_process.fork() but adds load balancing, shared port binding, and cluster-specific lifecycle management. child_process.fork() is a general-purpose tool for spawning child processes without these networking features.

Both methods create new Node.js child processes with IPC channels, but they serve different purposes. cluster.fork() is specifically designed for building HTTP servers that can share a port and distribute load. child_process.fork() is a lower-level API used for offloading CPU-intensive tasks or running isolated scripts.

cluster.fork() Features
  1. 1

    Workers share the same server port automatically

  2. 2

    Built-in round-robin load balancing by the master

  3. 3

    Cluster events: fork, online, listening, exit, disconnect

  4. 4

    Workers are tracked in the cluster.workers map

  5. 5

    Designed specifically for HTTP server scaling

child_process.fork() Features
  1. 1

    General-purpose child process creation

  2. 2

    No shared port or built-in load balancing

  3. 3

    Full IPC channel for message passing

  4. 4

    Used for CPU-bound task offloading to a separate script

  5. 5

    Spawns an entirely separate Node.js script file

Comparison Example