Utilise Node.js clustering and load-balancing features to strengthen and improve your online application.

Utilise Node.js clustering and load-balancing features to strengthen and improve your online application.

Clustering in Node.js

Node.js is single-threaded, meaning it runs on a single core by default. However, modern CPUs are multi-core, so to take full advantage of the available hardware, Node.js provides a built-in module called cluster. This module allows you to fork the main process into multiple worker processes, each running on a separate core, which can handle requests concurrently.

To optimize processing capacity and performance, clustering divides incoming requests among several spawned Node.js processes, using multi-core CPUs.

What is the need of clusters in node.js?

You will encounter bottlenecks — dropped requests, unresponsive servers, or interruptions to work that was already running on the server — if your application only uses one thread to handle the increased workload and requests it receives as it becomes more popular (or complex).

Thankfully, the cluster module in Node.js provides a workaround for this.

How Clustering Works:

1.??Master Process: This is the main Node.js process that runs initially. It doesn’t handle any requests itself but is responsible for forking worker processes.

2.??Worker Processes: These are the forked processes that handle the incoming requests. Each worker runs in its own thread and can independently handle requests.

Example of Clustering in Node.js

const cluster = require('cluster');
const http = require('http');
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();
  }

  cluster.on('exit', (worker, code, signal) => {
    console.log(`Worker ${worker.process.pid} died`);
  });
} else {
  // Workers can share any TCP connection
  http.createServer((req, res) => {
    res.writeHead(200);
    res.end(`Handled by worker ${process.pid}\n`);
  }).listen(8000);

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

In this example: - The master process forks worker processes equal to the number of CPU cores. - Each worker process listens on port 8000 and handles incoming requests. - If a worker dies, the master can detect it and potentially fork a new worker.

Load Balancing in Node.js

Load balancing refers to distributing incoming network traffic across multiple servers. In Node.js, this can be done at the application level or via external tools like Nginx or HAProxy. The clustering module in Node.js helps achieve a form of load balancing, but it’s within a single machine. For more complex scenarios, an external load balancer is often preferred.

How Load Balancing Works with Node.js:

1.???Round-Robin: The most common technique where incoming requests are distributed to each worker in a circular order.

2.?? Sticky Sessions: In this method, requests from a particular client always go to the same worker. This is useful when you have session data stored in memory.

Load Balancing with External Tools

For production environments, you might want to use an external load balancer like Nginx or HAProxy. Here’s an example with Nginx:

1.??Install Nginx.

2.??Configure Nginx to distribute traffic to your Node.js instances running on different ports:

upstream nodejs_backend {
    server 127.0.0.1:8000;
    server 127.0.0.1:8001;
    server 127.0.0.1:8002;
}

server {
    listen 80;

    location / {
        proxy_pass https://nodejs_backend;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}        

In this configuration: - Nginx distributes traffic to multiple Node.js instances running on different ports. - This setup can be scaled across multiple machines.

Load balancing using PM2

For maintaining and growing Node.js applications, Process Manager 2 (PM2) is a well-liked process manager that offers several helpful features. PM2 can be used for process monitoring, load balancing, zero-downtime deployments, log management, process clustering, and security.

PM2's capability to carry out load balancing for Node.js apps is one of its primary features. The efficiency and scalability of applications can be enhanced by using PM2 to split up incoming traffic among several Node.js processes. In order to optimise the use of many CPU cores and boost application performance and throughput, PM2 may now cluster Node.js processes.

const http = require('http');

// Create a simple HTTP server
const server = http.createServer((req, res) => {
  res.writeHead(200);
  res.end(`Hello from process ${process.pid}\n`);
});

// Start the server using PM2
const cluster = require('pm2').clusterMode();

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

  // Fork workers
  for (let i = 0; i < 3; i++) {
    cluster.fork();
  }
} else {
  // Workers can share any TCP connection
  // In this case it is an HTTP server
  server.listen(8000);

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

In this example, Node.js http module used to develop a basic HTTP server. Next, use PM2 to start the server and enable load balancing and process clustering by invoking pm2.clusterMode().

Here cluster.fork() used to create three worker processes if the active process is the master process. Incoming HTTP requests from clients will be handled by each worker process. In the event that the running process is a worker process, you report a message indicating the worker process's start and use server.listen() to launch the HTTP server.

Conclusion

Clustering allows Node.js to utilize multiple CPU cores by creating multiple worker processes, while load balancing can distribute traffic among these processes or multiple machines. These techniques are essential for building scalable Node.js applications.

Bhagvendra Singh Parihar

Junior Backend Developer @IGTechso | NodeJS | Flutter | JavaScript | AWS S3 and EC2 | GitHub Actions | Tech Instructor | Postman Student Expert | Docker

6 个月

Really, all the approaches are very helpful for understanding the performace optimization of any nodejs application

要查看或添加评论,请登录

Usman Shaikh的更多文章

社区洞察

其他会员也浏览了