Source Code: lib/cluster.js
Clusters of Node.js processes can be used to run multiple instances of Node.js\nthat can distribute workloads among their application threads. When process\nisolation is not needed, use the worker_threads module instead, which\nallows running multiple application threads within a single Node.js instance.
worker_threads
The cluster module allows easy creation of child processes that all share\nserver ports.
import cluster from 'node:cluster';\nimport http from 'node:http';\nimport { cpus } from 'node:os';\nimport process from 'node:process';\n\nconst numCPUs = cpus().length;\n\nif (cluster.isPrimary) {\n console.log(`Primary ${process.pid} is running`);\n\n // Fork workers.\n for (let i = 0; i < numCPUs; i++) {\n cluster.fork();\n }\n\n cluster.on('exit', (worker, code, signal) => {\n console.log(`worker ${worker.process.pid} died`);\n });\n} else {\n // Workers can share any TCP connection\n // In this case it is an HTTP server\n http.createServer((req, res) => {\n res.writeHead(200);\n res.end('hello world\\n');\n }).listen(8000);\n\n console.log(`Worker ${process.pid} started`);\n}\n
const cluster = require('node:cluster');\nconst http = require('node:http');\nconst numCPUs = require('node:os').cpus().length;\nconst process = require('node:process');\n\nif (cluster.isPrimary) {\n console.log(`Primary ${process.pid} is running`);\n\n // Fork workers.\n for (let i = 0; i < numCPUs; i++) {\n cluster.fork();\n }\n\n cluster.on('exit', (worker, code, signal) => {\n console.log(`worker ${worker.process.pid} died`);\n });\n} else {\n // Workers can share any TCP connection\n // In this case it is an HTTP server\n http.createServer((req, res) => {\n res.writeHead(200);\n res.end('hello world\\n');\n }).listen(8000);\n\n console.log(`Worker ${process.pid} started`);\n}\n
Running Node.js will now share port 8000 between the workers:
$ node server.js\nPrimary 3596 is running\nWorker 4324 started\nWorker 4520 started\nWorker 6056 started\nWorker 5644 started\n
On Windows, it is not yet possible to set up a named pipe server in a worker.
The worker processes are spawned using the child_process.fork() method,\nso that they can communicate with the parent via IPC and pass server\nhandles back and forth.
child_process.fork()
The cluster module supports two methods of distributing incoming\nconnections.
The first one (and the default one on all platforms except Windows)\nis the round-robin approach, where the primary process listens on a\nport, accepts new connections and distributes them across the workers\nin a round-robin fashion, with some built-in smarts to avoid\noverloading a worker process.
The second approach is where the primary process creates the listen\nsocket and sends it to interested workers. The workers then accept\nincoming connections directly.
The second approach should, in theory, give the best performance.\nIn practice however, distribution tends to be very unbalanced due\nto operating system scheduler vagaries. Loads have been observed\nwhere over 70% of all connections ended up in just two processes,\nout of a total of eight.
Because server.listen() hands off most of the work to the primary\nprocess, there are three cases where the behavior between a normal\nNode.js process and a cluster worker differs:
server.listen()
server.listen({fd: 7})
server.listen(handle)
server.listen(0)
listen(0)
Node.js does not provide routing logic. It is therefore important to design an\napplication such that it does not rely too heavily on in-memory data objects for\nthings like sessions and login.
Because workers are all separate processes, they can be killed or\nre-spawned depending on a program's needs, without affecting other\nworkers. As long as there are some workers still alive, the server will\ncontinue to accept connections. If no workers are alive, existing connections\nwill be dropped and new connections will be refused. Node.js does not\nautomatically manage the number of workers, however. It is the application's\nresponsibility to manage the worker pool based on its own needs.
Although a primary use case for the node:cluster module is networking, it can\nalso be used for other use cases requiring worker processes.
node:cluster
A Worker object contains all public information and method about a worker.\nIn the primary it can be obtained using cluster.workers. In a worker\nit can be obtained using cluster.worker.
Worker
cluster.workers
cluster.worker
Similar to the cluster.on('disconnect') event, but specific to this worker.
cluster.on('disconnect')
cluster.fork().on('disconnect', () => {\n // Worker has disconnected\n});\n
This event is the same as the one provided by child_process.fork().
Within a worker, process.on('error') may also be used.
process.on('error')
Similar to the cluster.on('exit') event, but specific to this worker.
cluster.on('exit')
import cluster from 'node:cluster';\n\nif (cluster.isPrimary) {\n const worker = cluster.fork();\n worker.on('exit', (code, signal) => {\n if (signal) {\n console.log(`worker was killed by signal: ${signal}`);\n } else if (code !== 0) {\n console.log(`worker exited with error code: ${code}`);\n } else {\n console.log('worker success!');\n }\n });\n}\n
const cluster = require('node:cluster');\n\nif (cluster.isPrimary) {\n const worker = cluster.fork();\n worker.on('exit', (code, signal) => {\n if (signal) {\n console.log(`worker was killed by signal: ${signal}`);\n } else if (code !== 0) {\n console.log(`worker exited with error code: ${code}`);\n } else {\n console.log('worker success!');\n }\n });\n}\n
Similar to the cluster.on('listening') event, but specific to this worker.
cluster.on('listening')
cluster.fork().on('listening', (address) => {\n // Worker is listening\n});\n
It is not emitted in the worker.
Similar to the 'message' event of cluster, but specific to this worker.
'message'
cluster
Within a worker, process.on('message') may also be used.
process.on('message')
See process event: 'message'.
process
Here is an example using the message system. It keeps a count in the primary\nprocess of the number of HTTP requests received by the workers:
import cluster from 'node:cluster';\nimport http from 'node:http';\nimport { cpus } from 'node:os';\nimport process from 'node:process';\n\nif (cluster.isPrimary) {\n\n // Keep track of http requests\n let numReqs = 0;\n setInterval(() => {\n console.log(`numReqs = ${numReqs}`);\n }, 1000);\n\n // Count requests\n function messageHandler(msg) {\n if (msg.cmd && msg.cmd === 'notifyRequest') {\n numReqs += 1;\n }\n }\n\n // Start workers and listen for messages containing notifyRequest\n const numCPUs = cpus().length;\n for (let i = 0; i < numCPUs; i++) {\n cluster.fork();\n }\n\n for (const id in cluster.workers) {\n cluster.workers[id].on('message', messageHandler);\n }\n\n} else {\n\n // Worker processes have a http server.\n http.Server((req, res) => {\n res.writeHead(200);\n res.end('hello world\\n');\n\n // Notify primary about the request\n process.send({ cmd: 'notifyRequest' });\n }).listen(8000);\n}\n
const cluster = require('node:cluster');\nconst http = require('node:http');\nconst process = require('node:process');\n\nif (cluster.isPrimary) {\n\n // Keep track of http requests\n let numReqs = 0;\n setInterval(() => {\n console.log(`numReqs = ${numReqs}`);\n }, 1000);\n\n // Count requests\n function messageHandler(msg) {\n if (msg.cmd && msg.cmd === 'notifyRequest') {\n numReqs += 1;\n }\n }\n\n // Start workers and listen for messages containing notifyRequest\n const numCPUs = require('node:os').cpus().length;\n for (let i = 0; i < numCPUs; i++) {\n cluster.fork();\n }\n\n for (const id in cluster.workers) {\n cluster.workers[id].on('message', messageHandler);\n }\n\n} else {\n\n // Worker processes have a http server.\n http.Server((req, res) => {\n res.writeHead(200);\n res.end('hello world\\n');\n\n // Notify primary about the request\n process.send({ cmd: 'notifyRequest' });\n }).listen(8000);\n}\n
Similar to the cluster.on('online') event, but specific to this worker.
cluster.on('online')
cluster.fork().on('online', () => {\n // Worker is online\n});\n
In a worker, this function will close all servers, wait for the 'close' event\non those servers, and then disconnect the IPC channel.
'close'
In the primary, an internal message is sent to the worker causing it to call\n.disconnect() on itself.
.disconnect()
Causes .exitedAfterDisconnect to be set.
.exitedAfterDisconnect
After a server is closed, it will no longer accept new connections,\nbut connections may be accepted by any other listening worker. Existing\nconnections will be allowed to close as usual. When no more connections exist,\nsee server.close(), the IPC channel to the worker will close allowing it\nto die gracefully.
server.close()
The above applies only to server connections, client connections are not\nautomatically closed by workers, and disconnect does not wait for them to close\nbefore exiting.
In a worker, process.disconnect exists, but it is not this function;\nit is disconnect().
process.disconnect
disconnect()
Because long living server connections may block workers from disconnecting, it\nmay be useful to send a message, so application specific actions may be taken to\nclose them. It also may be useful to implement a timeout, killing a worker if\nthe 'disconnect' event has not been emitted after some time.
'disconnect'
if (cluster.isPrimary) {\n const worker = cluster.fork();\n let timeout;\n\n worker.on('listening', (address) => {\n worker.send('shutdown');\n worker.disconnect();\n timeout = setTimeout(() => {\n worker.kill();\n }, 2000);\n });\n\n worker.on('disconnect', () => {\n clearTimeout(timeout);\n });\n\n} else if (cluster.isWorker) {\n const net = require('node:net');\n const server = net.createServer((socket) => {\n // Connections never end\n });\n\n server.listen(8000);\n\n process.on('message', (msg) => {\n if (msg === 'shutdown') {\n // Initiate graceful close of any connections to server\n }\n });\n}\n
This function returns true if the worker is connected to its primary via its\nIPC channel, false otherwise. A worker is connected to its primary after it\nhas been created. It is disconnected after the 'disconnect' event is emitted.
true
false
This function returns true if the worker's process has terminated (either\nbecause of exiting or being signaled). Otherwise, it returns false.
import cluster from 'node:cluster';\nimport http from 'node:http';\nimport { cpus } from 'node:os';\nimport process from 'node:process';\n\nconst numCPUs = cpus().length;\n\nif (cluster.isPrimary) {\n console.log(`Primary ${process.pid} is running`);\n\n // Fork workers.\n for (let i = 0; i < numCPUs; i++) {\n cluster.fork();\n }\n\n cluster.on('fork', (worker) => {\n console.log('worker is dead:', worker.isDead());\n });\n\n cluster.on('exit', (worker, code, signal) => {\n console.log('worker is dead:', worker.isDead());\n });\n} else {\n // Workers can share any TCP connection. In this case, it is an HTTP server.\n http.createServer((req, res) => {\n res.writeHead(200);\n res.end(`Current process\\n ${process.pid}`);\n process.kill(process.pid);\n }).listen(8000);\n}\n
const cluster = require('node:cluster');\nconst http = require('node:http');\nconst numCPUs = require('node:os').cpus().length;\nconst process = require('node:process');\n\nif (cluster.isPrimary) {\n console.log(`Primary ${process.pid} is running`);\n\n // Fork workers.\n for (let i = 0; i < numCPUs; i++) {\n cluster.fork();\n }\n\n cluster.on('fork', (worker) => {\n console.log('worker is dead:', worker.isDead());\n });\n\n cluster.on('exit', (worker, code, signal) => {\n console.log('worker is dead:', worker.isDead());\n });\n} else {\n // Workers can share any TCP connection. In this case, it is an HTTP server.\n http.createServer((req, res) => {\n res.writeHead(200);\n res.end(`Current process\\n ${process.pid}`);\n process.kill(process.pid);\n }).listen(8000);\n}\n
This function will kill the worker. In the primary, it does this\nby disconnecting the worker.process, and once disconnected, killing\nwith signal. In the worker, it does it by disconnecting the channel,\nand then exiting with code 0.
worker.process
signal
0
Because kill() attempts to gracefully disconnect the worker process, it is\nsusceptible to waiting indefinitely for the disconnect to complete. For example,\nif the worker enters an infinite loop, a graceful disconnect will never occur.\nIf the graceful disconnect behavior is not needed, use worker.process.kill().
kill()
worker.process.kill()
This method is aliased as worker.destroy() for backward compatibility.
worker.destroy()
In a worker, process.kill() exists, but it is not this function;\nit is kill().
process.kill()
Send a message to a worker or primary, optionally with a handle.
In the primary, this sends a message to a specific worker. It is identical to\nChildProcess.send().
ChildProcess.send()
In a worker, this sends a message to the primary. It is identical to\nprocess.send().
process.send()
This example will echo back all messages from the primary:
if (cluster.isPrimary) {\n const worker = cluster.fork();\n worker.send('hi there');\n\n} else if (cluster.isWorker) {\n process.on('message', (msg) => {\n process.send(msg);\n });\n}\n
This property is true if the worker exited due to .kill() or\n.disconnect(). If the worker exited any other way, it is false. If the\nworker has not exited, it is undefined.
.kill()
undefined
The boolean worker.exitedAfterDisconnect allows distinguishing between\nvoluntary and accidental exit, the primary may choose not to respawn a worker\nbased on this value.
worker.exitedAfterDisconnect
cluster.on('exit', (worker, code, signal) => {\n if (worker.exitedAfterDisconnect === true) {\n console.log('Oh, it was just voluntary – no need to worry');\n }\n});\n\n// kill worker\nworker.kill();\n
Each new worker is given its own unique id, this id is stored in the\nid.
id
While a worker is alive, this is the key that indexes it in\ncluster.workers.
All workers are created using child_process.fork(), the returned object\nfrom this function is stored as .process. In a worker, the global process\nis stored.
.process
See: Child Process module.
Workers will call process.exit(0) if the 'disconnect' event occurs\non process and .exitedAfterDisconnect is not true. This protects against\naccidental disconnection.
process.exit(0)
Emitted after the worker IPC channel has disconnected. This can occur when a\nworker exits gracefully, is killed, or is disconnected manually (such as with\nworker.disconnect()).
worker.disconnect()
There may be a delay between the 'disconnect' and 'exit' events. These\nevents can be used to detect if the process is stuck in a cleanup or if there\nare long-living connections.
'exit'
cluster.on('disconnect', (worker) => {\n console.log(`The worker #${worker.id} has disconnected`);\n});\n
When any of the workers die the cluster module will emit the 'exit' event.
This can be used to restart the worker by calling .fork() again.
.fork()
cluster.on('exit', (worker, code, signal) => {\n console.log('worker %d died (%s). restarting...',\n worker.process.pid, signal || code);\n cluster.fork();\n});\n
See child_process event: 'exit'.
child_process
When a new worker is forked the cluster module will emit a 'fork' event.\nThis can be used to log worker activity, and create a custom timeout.
'fork'
const timeouts = [];\nfunction errorMsg() {\n console.error('Something must be wrong with the connection ...');\n}\n\ncluster.on('fork', (worker) => {\n timeouts[worker.id] = setTimeout(errorMsg, 2000);\n});\ncluster.on('listening', (worker, address) => {\n clearTimeout(timeouts[worker.id]);\n});\ncluster.on('exit', (worker, code, signal) => {\n clearTimeout(timeouts[worker.id]);\n errorMsg();\n});\n
After calling listen() from a worker, when the 'listening' event is emitted\non the server, a 'listening' event will also be emitted on cluster in the\nprimary.
listen()
'listening'
The event handler is executed with two arguments, the worker contains the\nworker object and the address object contains the following connection\nproperties: address, port, and addressType. This is very useful if the\nworker is listening on more than one address.
worker
address
port
addressType
cluster.on('listening', (worker, address) => {\n console.log(\n `A worker is now connected to ${address.address}:${address.port}`);\n});\n
The addressType is one of:
4
6
-1
'udp4'
'udp6'
Emitted when the cluster primary receives a message from any worker.
See child_process event: 'message'.
After forking a new worker, the worker should respond with an online message.\nWhen the primary receives an online message it will emit this event.\nThe difference between 'fork' and 'online' is that fork is emitted when the\nprimary forks a worker, and 'online' is emitted when the worker is running.
'online'
cluster.on('online', (worker) => {\n console.log('Yay, the worker responded after it was forked');\n});\n
Emitted every time .setupPrimary() is called.
.setupPrimary()
The settings object is the cluster.settings object at the time\n.setupPrimary() was called and is advisory only, since multiple calls to\n.setupPrimary() can be made in a single tick.
settings
cluster.settings
If accuracy is important, use cluster.settings.
Calls .disconnect() on each worker in cluster.workers.
When they are disconnected all internal handles will be closed, allowing the\nprimary process to die gracefully if no other event is waiting.
The method takes an optional callback argument which will be called when\nfinished.
This can only be called from the primary process.
Spawn a new worker process.
Deprecated alias for .setupPrimary().
setupPrimary is used to change the default 'fork' behavior. Once called,\nthe settings will be present in cluster.settings.
setupPrimary
Any settings changes only affect future calls to .fork() and have no\neffect on workers that are already running.
The only attribute of a worker that cannot be set via .setupPrimary() is\nthe env passed to .fork().
env
The defaults above apply to the first call only; the defaults for later\ncalls are the current values at the time of cluster.setupPrimary() is called.
cluster.setupPrimary()
import cluster from 'node:cluster';\n\ncluster.setupPrimary({\n exec: 'worker.js',\n args: ['--use', 'https'],\n silent: true\n});\ncluster.fork(); // https worker\ncluster.setupPrimary({\n exec: 'worker.js',\n args: ['--use', 'http']\n});\ncluster.fork(); // http worker\n
const cluster = require('node:cluster');\n\ncluster.setupPrimary({\n exec: 'worker.js',\n args: ['--use', 'https'],\n silent: true\n});\ncluster.fork(); // https worker\ncluster.setupPrimary({\n exec: 'worker.js',\n args: ['--use', 'http']\n});\ncluster.fork(); // http worker\n
Deprecated alias for cluster.isPrimary.
cluster.isPrimary
True if the process is a primary. This is determined\nby the process.env.NODE_UNIQUE_ID. If process.env.NODE_UNIQUE_ID is\nundefined, then isPrimary is true.
process.env.NODE_UNIQUE_ID
isPrimary
True if the process is not a primary (it is the negation of cluster.isPrimary).
The scheduling policy, either cluster.SCHED_RR for round-robin or\ncluster.SCHED_NONE to leave it to the operating system. This is a\nglobal setting and effectively frozen once either the first worker is spawned,\nor .setupPrimary() is called, whichever comes first.
cluster.SCHED_RR
cluster.SCHED_NONE
SCHED_RR is the default on all operating systems except Windows.\nWindows will change to SCHED_RR once libuv is able to effectively\ndistribute IOCP handles without incurring a large performance hit.
SCHED_RR
cluster.schedulingPolicy can also be set through the\nNODE_CLUSTER_SCHED_POLICY environment variable. Valid\nvalues are 'rr' and 'none'.
cluster.schedulingPolicy
NODE_CLUSTER_SCHED_POLICY
'rr'
'none'
After calling .setupPrimary() (or .fork()) this settings object will\ncontain the settings, including the default values.
This object is not intended to be changed or set manually.
A reference to the current worker object. Not available in the primary process.
import cluster from 'node:cluster';\n\nif (cluster.isPrimary) {\n console.log('I am primary');\n cluster.fork();\n cluster.fork();\n} else if (cluster.isWorker) {\n console.log(`I am worker #${cluster.worker.id}`);\n}\n
const cluster = require('node:cluster');\n\nif (cluster.isPrimary) {\n console.log('I am primary');\n cluster.fork();\n cluster.fork();\n} else if (cluster.isWorker) {\n console.log(`I am worker #${cluster.worker.id}`);\n}\n
A hash that stores the active worker objects, keyed by id field. This makes it\neasy to loop through all the workers. It is only available in the primary\nprocess.
A worker is removed from cluster.workers after the worker has disconnected\nand exited. The order between these two events cannot be determined in\nadvance. However, it is guaranteed that the removal from the cluster.workers\nlist happens before the last 'disconnect' or 'exit' event is emitted.
import cluster from 'node:cluster';\n\nfor (const worker of Object.values(cluster.workers)) {\n worker.send('big announcement to all workers');\n}\n
const cluster = require('node:cluster');\n\nfor (const worker of Object.values(cluster.workers)) {\n worker.send('big announcement to all workers');\n}\n