CodexBloom - Programming Q&A Platform

advanced patterns When Using Node.js Cluster Module with PM2 for Load Balancing

šŸ‘€ Views: 84 šŸ’¬ Answers: 1 šŸ“… Created: 2025-06-05
node.js express pm2 cluster JavaScript

I'm experimenting with I'm having trouble with I've been banging my head against this for hours... I'm currently using the Node.js cluster module with PM2 to scale my Express application across multiple CPU cores. However, I am working with a strange scenario where the request handling is inconsistent when under load. For example, when I send multiple concurrent requests, I notice that sometimes responses take much longer than expected, and occasionally, I get a `502 Bad Gateway` behavior from Nginx, which is acting as a reverse proxy in front of my Node.js app. Here's a simplified version of my server setup: ```javascript 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(); } cluster.on('exit', (worker, code, signal) => { console.log(`Worker ${worker.process.pid} died with code: ${code}, and signal: ${signal}`); }); } else { const express = require('express'); const app = express(); app.get('/api/data', (req, res) => { // Simulating a delay for data processing setTimeout(() => { res.json({ message: 'Hello from worker ' + process.pid }); }, Math.random() * 500); }); const PORT = process.env.PORT || 3000; app.listen(PORT, () => { console.log(`Worker ${process.pid} started on port ${PORT}`); }); } ``` When deploying with PM2, I run it with the command `pm2 start app.js -i max`. On the surface, it seems to distribute the load across the workers, but during peak usage, I observe that sometimes one of the workers becomes unresponsive, leading to increased request latency and the aforementioned `502` errors. I've tried adjusting the `max_memory_restart` option in PM2 and increased the timeout settings on Nginx, but the question continues. I’m also logging any errors within the Express app and haven't seen any unhandled exceptions that could lead to workers crashing. Could it be related to how Node.js handles cluster communication or perhaps an Nginx timeout scenario that I haven't configured properly? Any insights would be greatly appreciated! I'm working on a application that needs to handle this. Thanks in advance! This is part of a larger microservice I'm building. Hoping someone can shed some light on this. I'm using Javascript latest in this project. Thanks for taking the time to read this! I'm using Javascript stable in this project. Thanks for taking the time to read this!