Scaling with Node.js Clustering: Cluster and PM2 Guide
What Will You Learn in This Guide?
In this guide, you will learn how to overcome the single core limit of Node.js.
We will scale your application to use all CPU cores.
You will clearly see the difference between Cluster and PM2 with the load test.
🧠 Technical Summary
Main topic: Clustering in Node.js applications
Problem: CPU bottleneck of single-process Node.js applications
Solution: Multiple process usage with Cluster module and PM2
Steps:
- Single process application
- Scaling with Cluster
- Load test
- Production build with PM2
Why is Node.js Clustering Necessary?
Node.js uses single core by default.
CPU-intensive operations seriously reduce performance.
In heavy traffic, a single process easily gets bogged down.
Cluster runs the same application through multiple processes.
The load is balanced between cores.
If one process goes down, the others continue to operate.
Prerequisites
- A server with at least 4 core
- A system with Node.js and npm installed
- Basic Express.js knowledge
- Terminal usage
1️⃣ Preparing the Project Environment
mkdir genixnode-cluster-demo
cd genixnode-cluster-demo
npm init -y
- This command creates a new Node.js project.
Add ES module support to package.json:
"type": "module"
2️⃣ Installing Required Packages
npm install express
npm install -g loadtest pm2
- Express sets up the server, loadtest performs load testing, PM2 manages the process.
3️⃣ Creating Applications Without Clusters
import express from "express";
const port = 3000;
const app = express();
console.log(`Worker PID: ${process.pid}`);
app.get("/yuksek-islem", (req, res) => {
let toplam = 0;
for (let i = 0; i < 5_000_000; i++) {
toplam++;
}
res.send(`İşlem sonucu: ${toplam}\n`);
});
app.listen(port, () => {
console.log(`${port} portunda dinleniyor`);
});
- This endpoint deliberately tires the CPU.
Run:
node index.js
4️⃣ Scaling with Cluster Module
import cluster from "cluster";
import os from "os";
import { dirname } from "path";
import { fileURLToPath } from "url";
const __dirname = dirname(fileURLToPath(import.meta.url));
const cpuSayisi = os.cpus().length;
console.log(`CPU Sayısı: ${cpuSayisi}`);
console.log(`Primary PID: ${process.pid}`);
cluster.setupPrimary({
exec: __dirname + "/index.js",
});
for (let i = 0; i < cpuSayisi; i++) {
cluster.fork();
}
cluster.on("exit", (worker) => {
console.log(`Worker ${worker.process.pid} düştü, yenisi başlatılıyor`);
cluster.fork();
});
- This structure starts a worker for each core.
5️⃣ Performance Comparison
- 🔹 Without Cluster
loadtest -n 1200 -c 200 -k http://localhost:3000/yuksek-islem
- Estimated result:
~80–90 requests/second
- High latency
- 🔹 With Cluster
- Run the same command again.
Estimated result:
~300–350 requests/second
- Latency drops seriously
6️⃣ Production Scaling with PM2
pm2 start index.js -i 0
-i 0 uses all CPU cores.
Check status:
pm2 ls
Watch the logs:
pm2 logs
❓ Frequently Asked Questions
-
Why does Node.js use a single core? The event-driven structure is I/O focused rather than CPU driven.
-
Does clustering increase memory consumption? Yes, each worker is a separate process.
-
Can workers share data? No. External solutions such as Redis are required.
-
Which one is recommended for Production? PM2 is more practical.
Result
In this guide, you learned how to scale Node.js applications. You used CPU power efficiently with Cluster. You are ready for the production environment with PM2.
🚀 You can immediately test this structure on the GenixNode multi-core VDS infrastructure. Do not let performance decrease as load increases.

