Welcome to our comprehensive guide on the Cluster Module in Node.js! This lesson is designed for beginners and intermediate developers who want to learn about this powerful tool for creating high-performance, scalable Node.js applications. 📝 Note: The Cluster Module helps us create cluster-based processes to improve the performance of Node.js applications by handling multiple requests concurrently.
Node.js is a single-threaded, non-blocking I/O runtime that handles multiple requests by using the event loop and the libuv library. However, when the number of requests exceeds the handling capacity, the performance of the application can degrade. To tackle this issue, we use the Cluster Module.
The Cluster Module allows us to create a worker cluster, where each worker handles a specific number of requests concurrently. This helps improve the overall performance and scalability of Node.js applications.
To use the Cluster Module, first ensure that you have Node.js installed on your system. You can download it from the official website: Node.js
Next, let's create a simple Node.js application to understand the Cluster Module better.
// app.js
const cluster = require('cluster');
const numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
// Fork workers
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
// Listen for worker deaths
cluster.on('exit', (worker, code, signal) => {
console.log(`Worker ${worker.process.pid} died with code ${code} and signal ${signal}`);
});
} else {
// Worker processes share the same code
console.log `I am a worker: ${process.pid}`;
}cluster.isMaster check determines whether the current process is the master or a worker.numCPUs number of worker processes using the cluster.fork() function.Let's create a simple HTTP server using the Cluster Module to handle multiple requests.
// app.js
const http = require('http');
const cluster = require('cluster');
const numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
// Fork workers
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
// Listen for worker deaths
cluster.on('exit', (worker, code, signal) => {
console.log(`Worker ${worker.process.pid} died with code ${code} and signal ${signal}`);
});
// Create an HTTP server for the master process
http.createServer((req, res) => {
res.writeHead(200);
res.end('Hello from the master process\n');
}).listen(8000);
} else {
// Worker processes share the same code
http.createServer((req, res) => {
res.writeHead(200);
res.end('Hello from a worker process\n');
}).listen(8000);
}In this example, both the master and worker processes create an HTTP server that listens on port 8000. When you access the application, you'll see that the response comes from either the master or a worker process.
What is the main purpose of the Cluster Module in Node.js?
That's it for this lesson on the Cluster Module in Node.js! Stay tuned for more advanced topics and practical examples on CodeYourCraft. Happy coding! 💡 Pro Tip: Experiment with the example code and adjust the number of workers to see the impact on performance.