What is a node.js cluster module? is it used for Load Balancing
Photo by Nangialai Stoman on Unsplash
Node.js is a popular server-side JavaScript runtime that is widely used to build high-performance web applications. One of the key features of Node.js is its ability to handle large numbers of incoming requests with minimal resource usage. However, as your application grows, you may find that a single Node.js process is no longer sufficient to handle all of the incoming traffic. That's where Node.js clusters come into play.
In simple terms, a Node.js cluster is a group of Node.js processes that work together to handle incoming requests. Each process in the cluster is called a worker, and the main process that manages the workers is called the master. The master process is responsible for creating the workers, distributing incoming requests to the workers, and monitoring the health of the workers.
The primary goal of using a Node.js cluster is to scale your application horizontally. Instead of adding more resources to a single process, you add more processes to distribute the load across multiple CPUs or servers. This allows you to handle more incoming requests while maintaining performance and stability.
Load Balancing with Node.js Clusters
One of the most common use cases for Node.js clusters is load balancing. Load balancing refers to the practice of distributing incoming requests evenly across multiple servers or processes. This helps to prevent any single server or process from becoming overwhelmed with traffic.
Node.js clusters provide a built-in load-balancing mechanism that makes it easy to distribute incoming requests across multiple workers. When a request comes in, the master process selects an available worker to handle the request. If all workers are busy, the request is queued until a worker becomes available.
By default, the cluster module uses a round-robin algorithm to distribute requests across the workers. This means that each worker is assigned a roughly equal number of requests. However, you can also implement your own custom load-balancing algorithm if needed.
Creating a Node.js Cluster
Creating a Node.js cluster is relatively straightforward. First, you need to require the built-in cluster module:
const cluster = require('cluster');
Next, you need to check if the current process is the master process or a worker process. This can be done using the cluster.isMaster
property:
if (cluster.isMaster) {
// This is the master process
} else {
// This is a worker process
}
If the current process is the master process, you can create the worker processes using the cluster.fork()
method:
if (cluster.isMaster) {
const numWorkers = require('os').cpus().length;
for (let i = 0; i < numWorkers; i++) {
cluster.fork();
}
}
This code creates a worker process for each CPU core on the system.
Once the workers are created, the master process can listen for incoming requests and distribute them to the workers:
if (cluster.isMaster) {
// ...
// Listen for incoming requests
const server = require('http').createServer((req, res) => {
// Find an available worker
const worker = cluster.workers[Math.floor(Math.random() * numWorkers)];
// Send the request to the worker
worker.send({ type: 'request', data: { req: req } });
// Receive the response from the worker
worker.on('message', (message) => {
if (message.type === 'response') {
// Send the response to the client
res.writeHead(message.statusCode, message.headers);
res.end(message.body);
}
});
});
server.listen(3000);
} else {
// This is a worker process
// Handle incoming requests
process.on('message', (message) => {
if (message.type === 'request') {
// Process the request
const req = message.data.req;
const res = {
writeHead: (statusCode, headers) => {
process.send({ type: 'response', data: { statusCode, headers } });
},
end: (body) => {
process.send({ type: 'response', data: { body } });
}
};
// Invoke your Node.js application logic
// ...
// When the response is ready, send it back to the master process
// ...
}
});
}
This code listens for incoming requests on port 3000 and distributes them to the worker processes using a round-robin algorithm. When a worker process receives a request, it processes the request and sends the response back to the master process.
Conclusion
Node.js clusters are an essential tool for scaling your application horizontally and handling large volumes of incoming traffic. With Node.js clusters, you can distribute incoming requests across multiple workers, enabling you to take full advantage of the available system resources. Whether you need to handle thousands of requests per second or ensure high availability for your application, Node.js clusters are an essential tool for any serious Node.js developer.
Reference Links:-
- Coders Gyan (https://www.youtube.com/live/xu_67IniKWM)