Building Performant Server-Side Applications using Node.js Clusters

Building Performant Server-Side Applications using Node.js Clusters

Node.js is a single-threaded runtime environment. This means that the applications can only utilize one core of the CPU. Even though modern CPUs have multiple cores, applications can not fully utilize the available CPU resources, leading to slower response times and reduced performance.

Node.js Clusters

Node.js clusters help to take full advantage of multiple CPU cores. It creates multiple instances of the application that run on different CPU cores concurrently sharing the same server port.

More Benefits

Before diving into code for creating clusters in Node.js, let us see what more benefits other than better CPU utilization and performance, Node.js clusters have to offer.

Fault Tolerance

Node.js clusters let us run multiple instances of the application. So, even if there is an unhandled exception in one of the instances, others will keep on running.

Scalability

We can add or remove extra instances of the application depending on the workload without making significant changes in our code.

Isolation

Each instance runs independently, preventing conflicts and ensuring problem in one instance does not affect the other.

Implementation

import os from 'node:os';
import http from "node:http";
import process from "node:process";
import cluster from "node:cluster";

const numCPUs = os.cpus().length;

if(cluster.isPrimary) {
  console.log(`Primary ${process.pid} is running`);

  // fork to create multiple instances of the same process
  for(let i = 0; i < numCPUs; i++) {
    cluster.fork();
  }

  cluster.on("exit", (worker, _code, _signal) => {
    console.log(`Worker ${worker.process.pid} died`);
  });
} else {
  // instances can share any TCP connection
  // in this case it is an HTTP server
  http.createServer((_req, res) => {
    res.writeHead(200);
    res.end("Hello World\n");
  }).listen(8000);

  console.log(`Worker ${process.pid} started`);
}

Here, we get the total number of CPUs that we have available on our hardware, and fork those many instances(or, worker processes), then create an HTTP server on each of those instances sharing the same port 8000.

You can refer to the cover photo of this blog post to visualize the actual working process.

Running this code on a CPU with 4 cores will produce the result in the console as,

Node.js Clusters

Fig: Node.js Clusters

Conclusion

Node.js clusters are a powerful tool for building performant, scalable and fault-tolerant server-side applications by improving CPU utilization. It helps to distribute the workload evenly and improve your application’s overall performance harnessing the power of the underlying hardware.