What is Cluster in Node.js? Using Cluster to increase the scalability of Node.js applications

What is Cluster in Node.js? Using Cluster to increase the scalability of Node.js applications

Daily short news for you
  • swapy is a library that helps you create drag-and-drop actions to easily swap the positions of components.

    The library supports a wide range of platforms, making it perfect for building something personalized like a user’s Dashboard.

    » Read more
  • After waking up, I saw the news feed flooded with articles about Microsoft rewriting the TypeScript compiler - tsc in Go, achieving performance that is 10 times faster than the current one. Wow!

    But when I saw this news, the question immediately popped into my head: "Why not Rust?" You know, the trend of rewriting everything in Rust is hotter than ever, and it’s not an exaggeration to say that it's sweeping through the rankings of previous legacy tools.

    What’s even more interesting is the choice of Go - which supposedly provides the best performance to date - as they say. Many people expressed disappointment about why it wasn’t C# instead 😆. When something is too famous, everything is scrutinized down to the smallest detail, and not everyone listens to what is said 🥶

    Why Go? #411

    » Read more
  • I read this article Migrating Off Oh-My-Zsh and other recent Yak Shavings - the author essentially states that he has been using Oh-My-Zsh (OMZ) for a long time, but now the game has changed, and there are many more powerful tools that have come out, so he realizes, oh, he doesn't need OMZ as much anymore.

    I also tried to follow along because I find this situation quite similar to the current state. It was surprising to discover something new. I hope to soon write a post about this next migration for my readers 😁

    » Read more

Problem

Node.js only has one thread to execute JavaScript code, which means it can only process one JS command at a time. If an application has multiple JS code that takes time to process, a "bottleneck" can occur. For example, your application may handle lightweight tasks but with a large number of concurrent requests, resulting in continuous connection timeouts due to not being able to handle all the requests.

It can be said that part of the application's power lies in the speed of the CPU. Nowadays, CPUs are becoming more powerful and integrated with more cores. If your Node.js application only runs on one core of the CPU, it is a waste. If there is a way to make the application utilize all the CPU cores, it would be great. For a simple calculation, let's say the application can handle 1000 req/s, but if it can utilize 8 CPU cores, that number can go up to ~8000 req/s. Furthermore, it increases fault tolerance, so if one instance malfunctions, there are still 7 instances ready to handle requests.

To harness the power of multi-core processors and increase processing capacity and fault tolerance, Cluster is one of the optimization methods. Let's learn more about Cluster in the following article.

What is Cluster?

Cluster in Node.js can be used to run multiple instances of Node.js to distribute the workload among them. The difference between Cluster and Worker Threads is that Cluster isolates worker processes, while Worker Threads run multiple threads within a single Node.js instance.

In essence, the workers created by Cluster use child_process.fork() to communicate with the process that creates them through IPC (Inter-Process Communication).

Cluster supports two methods of workload distribution:

  • The first method (default on all platforms except Windows) is a round-robin distribution, in which the main process (Primary) listens on a port, listens for new connections, and distributes them to workers in a round-robin fashion, with developers integrating "smart distribution methods" to avoid overloading worker processes.

  • The second approach is for the main process to create a socket to listen and send it to the workers. The workers then directly handle those requests.

In theory, the second approach provides the best performance. However, in practice, socket distribution tends to become imbalanced due to changes in the operating system scheduler. Developers have observed that over 70% of all connections are distributed to only two processes out of eight.

The workers created are separate processes that can be killed or respawned depending on the program without affecting other workers. As long as there are some active workers, the server will continue to handle incoming requests and vice versa.

Implementing Cluster

To implement Cluster, we use the node:cluster module integrated into Node. Usually, the number of workers created is equal to the number of CPU cores. The following example uses Node.js 18 LTS and express.js:

const cluster = require('node:cluster');
const numCPUs = require('node:os').cpus().length;
const express = require('express');

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

  for (let i = 0; i < numCPUs; i++) {
    cluster.fork();
  }

  cluster.on('exit', (worker, code, signal) => {
    console.log(`worker ${worker.process.pid} exited`);
  });
} else {
  const app = express();
  app.use(express).listen(port);

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

Then start it with the node command.

$ node index.js
Primary 3596 is running
Worker 4324 started
Worker 4520 started
Worker 6056 started
Worker 5644 started

You will see the number of workers created is equal to the number of CPU cores through the node:os module.

For more details on how to implement, refer to Node.js Cluster.

In addition, we can also use the tool pm2 to deploy Node.js applications similarly to the Cluster module.

Conclusion

Cluster is the fastest and most convenient method to increase the processing power of your JavaScript application by leveraging multi-core CPUs. Deploying Cluster is quite simple using the node:cluster module integrated into Node.js. The workload distribution algorithm of Cluster ensures that the workload is coordinated to prevent overloading of workers. If you have a multi-core server and want to make the most of those cores, try deploying Cluster right now.

Premium
Hello

Me & the desire to "play with words"

Have you tried writing? And then failed or not satisfied? At 2coffee.dev we have had a hard time with writing. Don't be discouraged, because now we have a way to help you. Click to become a member now!

Have you tried writing? And then failed or not satisfied? At 2coffee.dev we have had a hard time with writing. Don't be discouraged, because now we have a way to help you. Click to become a member now!

View all

Subscribe to receive new article notifications

or
* The summary newsletter is sent every 1-2 weeks, cancel anytime.

Comments (0)

Leave a comment...