DEV Community

Cover image for A Practical Guide to the Cluster Module in Node JS
Sriniwas
Sriniwas

Posted on

A Practical Guide to the Cluster Module in Node JS

Introduction

Scaling NodeJS is easier than you might think, but let's first understand how it works, you might've heard that NodeJS is an event-driven single threaded runtime for javascript, but trust me it's nothing to scoff at, thanks to the single-threaded nature of node it's exceptional at managing concurrent requests, while that may sound good on paper even that has it's limitation, and in this article we will discuss the cluster module.

Clusters

In order to fully utilize our CPU, we need to implement the cluster module, it's a module which allows us to create multiple 'workers' and each worker runs on one thread, if we have a CPI containing 4 cores we will 8 threads at our disposal, which means by implementing the cluster module our application can handle 8x more requests.

Now let's create a application!

// server.js
const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.send('Hello, World!');
});

module.exports = app
Enter fullscreen mode Exit fullscreen mode

We'll start by creating a basic express server with a route which sends 'Hello World!' as the response.

const cluster  = require('cluster')
const os = require('os')
const app = require('./server.js')
const port = 3000

if(cluster.isMaster){
  let cpus = os.cpus().length

  for(let i=0; i<cpus; i++){
    cluster.fork();
  }
} else {
  app.listen(port, () => {
    console.log(`Server is running on http://localhost:${port}`);
  });
}
Enter fullscreen mode Exit fullscreen mode

and now we're gonna add another file cluster.js, this will be the entry point for application

node ./cluster.js
Enter fullscreen mode Exit fullscreen mode

before ending this article there's one more thing we need to take care of, let's say one of our threads crash for some reason, in that case we need to fork another worker, which is actually quite simple to do.

// cluster.js
...
cluster.on('exit', (worker) => {
  console.log(`Worker ${worker.id} died`)
  cluster.fork()
})
...
Enter fullscreen mode Exit fullscreen mode

With this block, whenever a process dies, we'll fork a new one.

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Billboard image

Deploy and scale your apps on AWS and GCP with a world class developer experience

Coherence makes it easy to set up and maintain cloud infrastructure. Harness the extensibility, compliance and cost efficiency of the cloud.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay