---
source: https://iinventedlight.com/library/notes/node-cluster.md
title: "Node.js cluster - fork workers and share a server port"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "MIT (Node.js API docs); MIT (nodejs/node (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Node.js API docs, Cluster; nodejs/node (GitHub), doc/api/cluster.md. MIT (https://raw.githubusercontent.com/nodejs/node/main/LICENSE). Copyright Node.js contributors."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-web: node cluster"
tags: [node, cluster, fork, workers, primary]
client: Librarian
sources:
  - id: S1
    title: "Cluster"
    publisher: Node.js API docs
    url: https://nodejs.org/api/cluster.html
    accessed: 2026-11-18
    license: "MIT"
  - id: S2
    title: "doc/api/cluster.md"
    publisher: nodejs/node (GitHub)
    url: https://github.com/nodejs/node/blob/main/doc/api/cluster.md
    accessed: 2026-11-18
    license: "MIT"
---
# Node.js cluster - fork workers and share a server port
**Answer:** The cluster module can start child processes that all share server ports. Call `cluster.fork()` from the primary process (`cluster.isPrimary`). Workers can then listen on the same port.

## Examples

```js
import cluster from 'node:cluster';
import http from 'node:http';
import { availableParallelism } from 'node:os';

if (cluster.isPrimary) {
  const n = availableParallelism();
  for (let i = 0; i < n; i++) cluster.fork();
  cluster.on('exit', (worker) => {
    console.error('worker', worker.process.pid, 'exited');
    cluster.fork();
  });
} else {
  http.createServer((req, res) => res.end('ok')).listen(3000);
}
```

## Rules

- On most platforms except Windows, the default policy is round-robin: the primary process listens on a port and distributes connections.
- Worker processes are spawned using `child_process.fork()`, so they can communicate with the parent via IPC.

## See also

- [node-process-and-console](https://iinventedlight.com/library/notes/node-process-and-console/)
- [node-http-and-url](https://iinventedlight.com/library/notes/node-http-and-url/)

## Claims

- id: C1
  claim: Cluster workers can share server ports.
  source: S1
  evidence: "child processes that all share server ports"

- id: C2
  claim: The primary process can be detected with isPrimary.
  source: S2
  evidence: "if (cluster.isPrimary)"

- id: C3
  claim: The primary logs that it is running before it forks workers.
  source: S2
  evidence: "Primary ${process.pid} is running"

## Sources

- [S1] Cluster. Node.js API docs. https://nodejs.org/api/cluster.html . License: MIT. Accessed 2026-11-18.

- [S2] doc/api/cluster.md. nodejs/node (GitHub). https://github.com/nodejs/node/blob/main/doc/api/cluster.md . License: MIT. Accessed 2026-11-18.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Node.js API docs, Cluster; nodejs/node (GitHub), doc/api/cluster.md. MIT (https://raw.githubusercontent.com/nodejs/node/main/LICENSE). Copyright Node.js contributors. Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
