0
0
Node.jsframework~8 mins

Load balancing between workers in Node.js - Performance & Optimization

Choose your learning style9 modes available
Performance: Load balancing between workers
HIGH IMPACT
This affects how efficiently the server handles incoming requests and distributes CPU work, impacting response time and throughput.
Distributing incoming HTTP requests across multiple Node.js worker processes
Node.js
const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
  cluster.schedulingPolicy = cluster.SCHED_RR;
  for (let i = 0; i < numCPUs; i++) {
    cluster.fork();
  }
  cluster.on('exit', (worker) => {
    cluster.fork(); // replace dead worker
  });
} else {
  http.createServer((req, res) => {
    // handle request
    res.end(`Handled by worker ${process.pid}`);
  }).listen(8000);
}
Using cluster module with multiple workers and round-robin scheduling allows Node.js to distribute load across CPU cores evenly, improving concurrency.
📈 Performance GainReduces request queuing and balances CPU load, lowering INP and improving throughput.
Distributing incoming HTTP requests across multiple Node.js worker processes
Node.js
const cluster = require('cluster');
if (cluster.isMaster) {
  cluster.schedulingPolicy = cluster.SCHED_NONE;
  cluster.fork();
  cluster.fork();
} else {
  require('http').createServer((req, res) => {
    // handle request
    res.end('Handled by worker');
  }).listen(8000);
}
Master process does not balance load; OS assigns requests unevenly causing some workers to be overloaded while others idle.
📉 Performance CostCauses uneven CPU usage and request queuing, increasing average response time and INP.
Performance Comparison
PatternCPU UtilizationRequest QueuingResponse TimeVerdict
Single worker or unbalanced clusterLow to unevenHigh under loadHigh latency[X] Bad
Balanced cluster with multiple workersHigh and evenLowLow latency[OK] Good
Rendering Pipeline
Load balancing affects the server-side event loop and CPU scheduling, which impacts how quickly requests are processed and responses sent.
Event Loop
CPU Scheduling
Network I/O
⚠️ BottleneckUneven worker load causes some event loops to block longer, delaying response generation.
Core Web Vital Affected
INP
This affects how efficiently the server handles incoming requests and distributes CPU work, impacting response time and throughput.
Optimization Tips
1Use one worker per CPU core for best CPU utilization.
2Restart workers automatically if they crash to maintain availability.
3Avoid single-threaded bottlenecks by distributing requests evenly.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance benefit of using multiple Node.js workers with load balancing?
AImproves CPU utilization and reduces request latency
BReduces memory usage per request
CEliminates the need for asynchronous code
DIncreases the size of the server bundle
DevTools: Performance
How to check: Record a CPU profile while sending concurrent requests; check CPU usage distribution and event loop delays per worker.
What to look for: Balanced CPU usage across workers and minimal event loop blocking indicate good load balancing.