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
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
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.
License: CC-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.
Cite this
iinventedlight. 2026. "Node.js cluster - fork workers and share a server port." iinventedlight Library, iil:note:fwdj1x6v, version of 2026-11-18. https://iinventedlight.com/library/notes/node-cluster/. License: CC-BY-4.0.
@misc{iil_note_fwdj1x6v,
author = {{iinventedlight}},
title = {{Node.js cluster - fork workers and share a server port}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/node-cluster/},
note = {Permanent ID iil:note:fwdj1x6v. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/node-cluster.md.