As we all know Node is built upon a single threaded, non-blocking I/O model that is known for its speed due to its asynchronicity. However, that doesn’t mean it scales well right out of the box. A quick remedy that doesn’t involve incorporating load balancers, or technologies like Redis, is to make use of the additional cores embedded in your computer in order to achieve a greater degree of scalability. So, you can essentially delegate processes from parent to spawned child processes (better known as ‘workers’).
const cluster = require(‘cluster’);
const numCPUs = require(‘os’).cpus().length;We have now imported the cluster module natively built into Node.js, and we’ve also acquired the number of CPUs in your system in order to split up the work across the different cores.
if (cluster.isMaster) {
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
} cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`);
cluster.fork();
});
} else {
//code that the workers execute}
cluster.isMaster returns a boolean value that identifies the master process from amongst the workers, and then we can call fork() on the master process in order to actually spawn the workers.
Get Michael Laythe’s stories in your inbox
Join Medium for free to get updates from this writer.
Workers are usually used for things like: web servers, transmitting messages between master and worker with sockets, etc.
Check out this repo that deals with configuring clusters with Socket.io if interested.
indutny/sticky-session
sticky-session - Sticky session balancer based on a `cluster` module
github.com
For web servers workers aid quite a bit in terms of how many processed requests per second are recorded.
if (cluster.isMaster) {
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
} cluster.on(‘exit’, (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`);
cluster.fork();
});
} else {
const app = require(‘express’)(); app.get(‘/home’, (req, res) => {
console.log(‘handling get request to /home!’);
});
app.listen(8080);
}
With the implementation above each worker can handle the same get request, so there will be more processed requests per second, because we simply have more processing power on our side to handle additional traffic to this route.
As a developer who’s also looking for easy scaling options this seems to take the cake, so check it out, and let me know what you think!






