0
0
Node.jsframework~8 mins

Master and worker processes in Node.js - Performance & Optimization

Choose your learning style9 modes available
Performance: Master and worker processes
HIGH IMPACT
This concept affects how Node.js handles CPU-intensive tasks and concurrent requests, impacting server responsiveness and throughput.
Handling CPU-intensive tasks in a Node.js server
Node.js
const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
  for (let i = 0; i < numCPUs; i++) {
    cluster.fork();
  }
} else {
  http.createServer((req, res) => {
    // Worker handles request without blocking master
    let count = 0;
    for (let i = 0; i < 1e9; i++) count += i;
    res.end('Done ' + count);
  }).listen(3000);
}
Spreads workload across multiple worker processes, preventing any single event loop from blocking and improving concurrency.
📈 Performance GainReduces event loop blocking per process, improves INP by parallelizing CPU work across cores.
Handling CPU-intensive tasks in a Node.js server
Node.js
const http = require('http');
http.createServer((req, res) => {
  // Heavy computation blocking event loop
  let count = 0;
  for (let i = 0; i < 1e9; i++) count += i;
  res.end('Done ' + count);
}).listen(3000);
Heavy computation blocks the single main event loop, causing slow or unresponsive server during processing.
📉 Performance CostBlocks event loop for hundreds of milliseconds per request, causing high INP and poor responsiveness.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Single process heavy CPU taskN/AN/AN/A[X] Bad
Master with multiple worker processesN/AN/AN/A[OK] Good
Rendering Pipeline
Master process delegates tasks to worker processes, which run independently. This avoids blocking the main event loop and keeps the server responsive.
Event Loop
Task Scheduling
CPU Utilization
⚠️ BottleneckSingle-threaded event loop blocking due to CPU-heavy tasks
Core Web Vital Affected
INP
This concept affects how Node.js handles CPU-intensive tasks and concurrent requests, impacting server responsiveness and throughput.
Optimization Tips
1Offload CPU-intensive tasks to worker processes to keep the main event loop free.
2Use as many worker processes as CPU cores for best parallelism.
3Avoid blocking the event loop in the master process to improve server responsiveness.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance benefit of using worker processes in Node.js?
AThey improve CSS rendering speed in the browser.
BThey reduce the size of the JavaScript bundle sent to the browser.
CThey prevent the main event loop from blocking by parallelizing CPU work.
DThey eliminate the need for asynchronous code.
DevTools: Performance
How to check: Record a CPU profile while sending requests to the server. Look for long blocking tasks in the main thread.
What to look for: Long tasks blocking the event loop indicate poor use of worker processes; multiple smaller tasks across processes indicate good parallelism.