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
  • Manus has officially opened its doors to all users. For those who don't know, this is a reporting tool (making waves) similar to OpenAI's Deep Research. Each day, you get 300 free Credits for research. Each research session consumes Credits depending on the complexity of the request. Oh, and they seem to have a program giving away free Credits. I personally saw 2000 when I logged in.

    I tried it out and compared it with the same command I used before on Deep Research, and the content was completely different. Manus reports more like writing essays compared to OpenAI, which uses bullet points and tables.

    Oh, after signing up, you have to enter your phone number for verification; if there's an error, just wait until the next day and try again.

    » Read more
  • I just found a quite interesting website talking about the memorable milestones in the history of the global Internet: Internet Artifacts

    Just from 1977 - when the Internet was still in the lab - look how much the Internet has developed now 🫣

    » Read more
  • Just thinking that a server "hiding" behind Cloudflare is safe, but that’s not necessarily true; nothing is absolutely safe in this Internet world. I invite you to read the article CloudFlair: Bypassing Cloudflare using Internet-wide scan data to see how the author discovered the IP address of the server that used Cloudflare.

    It's quite impressive, really; no matter what, there will always be those who strive for security and, conversely, those who specialize in exploiting vulnerabilities and... blogging 🤓

    » 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...