Cluster
A single instance of Node.js runs in a single tread. To take advantage of multi-core systems the user will sometimes want to launch a cluster of Node.js processes to handle the load.
The cluster module allows you to easily create child processes that all share server port.
1 | const cluster = require('cluster') |
Running Node.js will now share port 8000 between the workers.
How it works
The workder processes are spawned using the child_process.fort()
method, so that they can communicate with the parent via IPC and pass server handles back and forth.
The cluster module supports two methods of distributing incoming connections.
The first one (and the default one on all platform except Windows), is the round-robin approch, where the master process listens on a port, accepts new connections and distributes them across the workers in a round-robin fashion, with some built-in smarts to avoid overloading a worker process.
The second approach is where the master process creates the listen socket and sends it to interested workers. The workers then accept incoming connection directly.
The second approach should, in theory, give the best performance. In practice however, distribution tends to be very unbalanced due to operating system scheduler vagaries. Loads have been observed where over 70% of all connections ended up in just two process, out of a total of eight.
Because server.listen()
hands off most of the worker to the master process, there are three case where the behavior between a normal Node.js process and a cluster worker differs:
server.listen({fd: 7})
Because the message is passed to the master, file descriptor 7 in the parent will be listened on, and the handle passed to the worker, rather than listening to the worker’s idea of what the number 7 file descriptor reference. (the fd will be used up sooner)
server.listen(handle)
Listening on handles explicitly wil cause the worker to use the supplied handle, rather than talk to the master process. If the worker already has the handle, then it’s presumed that you know what you are doing.
server.listen(0)
Normally, this will cause servers to listen on a random port. However, in a cluster, each worker will receive the same ‘random’ port each time they dolisten(0)
. In essence, the port is random the first time, but predicatable thereafter. If you want to listen on a unique port, generate a port number based on the cluster worker ID.
There is no routing logic in Node.js, or in your program, and no shared state between the workers. Therefore, it is important to design your program such that it does not rely on heavily on in-memory data object for things like sessions and login.
Because workers are all separate processes, they can be killed or re-spawned depending on your program’s needs, without affecting other workers. As long as there are some workers still alive, the server will continue to accept connections. If no workers are alive, existing connections will be dropped and new connections will be refused. Node.js does not automatically manage the number of worker for you, however. It is yoru responsibility to manage the worker pool for your application’s needs.
Class: worker
A Worder Object contains all public information and method about a worker. In the master it can be obtained using cluster.workers
. In a worker it can be obtained using cluster.worker
Event: ‘disconnect’
Similar to the cluster.on('disconnect')
event, but specific to this worker.
1 | cluster.fork().on('disconnect', () => { |
Event: ‘error’
This event is the same as the one provided by child_process.fork()
In a worker you can alse use process.on('error')
Event: ‘exit’
code
the exit code, if it exited Normally
signal
the name of the signal(e.g. SIGHUP) that caused the process to be killed.
Similar to the cluster.on('exit')
event, but specific to this worker
1 | const worker = cluster.fork() |
Event: ‘listening’
address
cluster.fork().on('listening', (address) => {
// Worker is listening
})
It is not emitted in the worker
Event: ‘message’
message
handle
| if (cluster.isMaster) {
let numReqs = 0
setInterval(() => {
console.log(numReqs = ${numReqs}
)
}, 1000)// Count requests
function messageHandler(msg) {
if (msg.cmd && msg.cmd === ‘notifyRequest’) {
numReqs++
}
}// Start workers and listen for messages containing notifyRequest
const numCPUs = require(‘os’).cpus().length
for (let i = 0; i < numCPUs; i++) {
cluster.fork()
}for (const id in cluster.workers) {
cluster.workers[id].on(‘message’, messageHandler)
}} else {
// Worker processes have a http server
http.Server((req, res) => {
res.writeHead(200)
res.end(‘hello world’)
process.send({ cmd: ‘notifyRequest’ })
}).listen(8000)
}
Event: ‘online’
Similar to the
cluster.on('online')
event, but specific to this worker
cluster.fork().on('online', () => {
// Worker is online
})
It is not emitted in the worker
worker.disconnect()
Return:
A reference to worker
In a worker, this function will close all server, wait for the
close
event on those servers, and disconnect the IPC channel.
In the master, an internal message is sent to the worker causing it to call
.disconnect()
on itself
Cause
.exitedAfterDisconnect
to be set.
Note that after a server is closed, it will no longer accept new connections, but connections may be accepted by any other listening worker. Existing connections will be allowed to close as usual. When no more connections exist, see
server.close()
, the IPC channel to the worker will close allowing it to die gracefully.
Note that in a worker,
process.disconnect
exists, but it is not this function.
Because long living server connections may block workers from disconnecting, it may be useful to send a message, so application specific actions may be taken to close them. It also may be useful to implement a timeout, killing a worker if the
disconnect
event has not been emiited after some time.
if (cluster.isMaster) {
const worker = cluster.fork()
let timeoutworker.on(‘listening’, (address) => {
worker.send(‘shutdown’)
worker.disconnect()
timeout = setTimeout(() => {
worker.kill()
}, 2000);
})worker.on(‘disconnect’, () => {
clearTimeout(timeout)
})
} else if (cluster.isWorker){
const net = require(‘net’)
const server = net.createServer((socket) => {
// connections never end
})
server.listen(8000)process.on(‘messaage’, (msg) => {
if (msg === ‘shutdown’) {
// initiate graceful close of any connections to server
}
})
}
worker.exitedAfterDisconnect
Set by calling
.kill()
or.disconnect()
. Until then, it isundefined
The boolean
worker.exitedAfterDisconnect
lets you distinguish between voluntary and accidental exit, the master may choose not to respawn a worker based on this value.
cluster.on('exit', (worker, code, signal) => {
if (worker.exitedAfterDisconnect === true) {
console.log('Oh, it was just voluntary - no need to worry')
}
})// kill worker
worker.kill()
worker.id
Each new worker is given its own unique id, this id is stored in the
id
While a worker is alive, this is the key that indixes it in cluster.workers.
worker.isConnected()
This function returns
true
if the worker is connected to its master via its IPC channel.false
otherwise. A worker is connected to its master after it’s been created. It is disconnected after thedisconnect
event is emitted.
worker.isDead()
This function returns
true
if the worker’s process has terminated (either because of exiting or being signaled). Otherwise, it returnsfalse
worker.kill([signal=’SIGTERM’])
signal
Name of the kill signal to send to the worker process.
This function will kill the worker. In the master it does this by disconnecting the
worker.process
and once disconnected, killing withsignal
. In the worker, it does it by disconnecting the channel and exiting with code0
Causes
.exitedAfterDisconnect
ot be set
This method is aliased as
worker.destroy()
for backwords compatibility.
Note that ina worker,
process.kill()
exists, but it is not the function.
worker.process
All Workers are created using
child_process.fork()
, the returned object from this function is stored as.process
. In a worker, the globalprocess
is stored
Note that the workers will call
process.exit(0)
if thedisconnect
event occurs onprocess
and.exitedAfterDisconnect
is nottrue
. This protects against accidental disconnection.
worker.send(message[, sendHandle, [callback]])
message
sendHandle
callback
Send a message to a worker or master, optinally with a handle.
In the master this sends a message to a specific worker. It is identical to
ChildProcess.send()
In a worker this sends a message to the master. It is identical to
process.send()
This example will echo back all messages from the master
if (cluster.isMaster) {
const worker = cluster.fork()
worker.send('hi there')
} else if (cluster.isWorker) {
process.on('message', (msg) => {
process.send(msg)
})
}
Event: ‘disconnect’
worker
<cluster.worker>
</cluster.worker>Emitted after the worker IPC channel has disconnected. This can occur when a worker exits gracefully, is killed, or is disconnected manually(such as with worker.disconnected())
There may be a delay between the
disconnect
andexit
events. These events can be used to detect if the process is stuck in a cleanup or if there are long-living connections.
cluster.on('disconnect', (worker) => {
console.log(The worker #${worker.id} has disconnected
)
})
Event: ‘exit’
worker
<cluster.worker>
</cluster.worker>
code
the exit code, if it exited normally
signal
the name of the signal (e.g. ‘SIGHUP’) that caused the process to be killed. When any of the workers die the cluster module will emit the 'exit' events
This can be used to restart the worker by calling
.fork()
again.
cluster.on('exit', (worker, code, signal) => {
console.log('worker %d died (%s), restarting...', worker.process.pid, signal || code)
cluster.fork()
})
Event: ‘fork’
worker
<cluster.worker>
</cluster.worker>When a new worker is forked the cluster module will emit a
fork
event. This can be used to log worker activity, and create your own timeout.
const timeouts = []
function errorMsg () {
console.error('Something must be wrong with the connection...')
}cluster.on(‘fork’, (worker) => {
timeouts[worker.id] = setTimeout(errorMsg, 2000)
})cluster.on(‘listening’, (worker, address) => {
clearTimeout(timeouts[worker.id])
})cluster.on(‘exit’, (worker, code, signal) => {
clearTimeout(timeouts[worker.id])
})
Event: ‘listening’
worker
<cluster.worker>
</cluster.worker>
address
After calling
listen()
from a worker. when thelistening
event it emitted on the server, alistening
event will also be emitted oncluster
in the masterThe event handler is executed with two arguments, the
worker
contains the worker object and theaddress
object contains the following connection properties:address
,port
andaddressType
. This is very useful if the worker is listening on more than one address.
cluster.on('listening', (worker, address) => {
console.log(A worker is now connected to ${address.address}: ${address.port}
)
})
The
addressType
is one of:
4
: (TCPv4)
6
: (TCPv6)
-1
: (unix domain socket)
"udp4"
or"udp6"
(UDP v4 or v6)Event: 'message'
worker
message
socket.on('data', (id) => {
const worker = cluster.workers[id]
})