Skip to content

Node.js cluster mode on a single PaaS node: when it helps and when it duplicates work

Cluster mode multiplies Node workers across CPU cores — useful on a multi-core PaaS node, harmful on a single-core container, and sometimes redundant when the platform already horizontally scales.

You have an Express API running on a PaaS where each container is allotted 2 vCPU and 2 GB of RAM. You ship it with node server.js — single process, single event loop — and at 70% CPU the platform spins up a second container. Someone asks whether you should run cluster inside each container too, so the second vCPU isn’t sitting idle. The answer is “yes, usually, but for the right reason and with the right boundary.” Cluster mode on a multi-core PaaS node is not duplicative with the platform’s horizontal scaler — they solve different problems at different layers. But cluster mode on a single-vCPU container is pure overhead, and cluster mode on top of a platform that already pins each container to one core gives you the worst of both.

What’s happening

Node runs on a single OS thread for JavaScript execution. On a container with two vCPU, a single Node process uses one of them — period. The other vCPU is available for background work the container is doing (the platform agent, log shippers, system processes) but is not available to your application code. Cluster mode forks N worker processes that listen on the same socket; the kernel load-balances incoming connections across them, and each worker has its own event loop. With two vCPU and two workers, both cores carry application traffic and CPU saturation moves from “1 of 2 cores pinned” to “both cores pinned.” That’s the win.

The platform’s horizontal scaler is a different mechanism: when the node’s CPU sustained over a window crosses a trigger, it adds another node. Cluster fills the cores inside one node. Horizontal scaling adds more nodes. They compose — you want both. Without cluster, the platform sees a node hit 50% CPU (one of two cores at 100%, the other at 0%) and may or may not trigger depending on how the trigger is configured; that 50% figure is misleading, because the application is actually saturated. Without horizontal scaling, cluster alone hits a ceiling when both cores of one container are full.

The traps are at the edges. On a single-vCPU container, cluster mode runs two or more workers competing for one core — context switching overhead is real, and the IPC chatter between the cluster master and workers eats cycles you wanted for requests. On a platform that already maps each container to a fixed-size cgroup with one CPU, cluster mode is wasted complexity. On any setup, cluster duplicates per-worker memory: a worker that loads a 200 MB ML model is now 4 × 200 MB if you fork 4 times. If memory is the bottleneck and not CPU, cluster makes things worse.

The decision rule: count the vCPU your container actually gets, and run workers = vCPU (or vCPU - 1 if you want one core free for system work). If vCPU = 1, do not cluster. If memory per process times worker count exceeds the container memory minus headroom, lower the worker count or move to horizontal scaling instead.

Two more things to check before clustering. In-memory state inside the Node process — cached sessions, hot data, rate-limiter counters — is per-worker once you cluster. If any of that is load-bearing, it has to move to Redis or be made worker-safe. And WebSocket or long-poll connections terminate on a specific worker; if your app holds chat-style state in process memory, you’ll lose it on worker recycle or platform scale events. The fix for both is the same: don’t keep state in the Node process. That’s true even before cluster, but cluster forces the issue.

How to verify

What the container actually has:

nproc
# 2

cat /sys/fs/cgroup/cpu.max 2>/dev/null || \
  cat /sys/fs/cgroup/cpu/cpu.cfs_quota_us
# 200000 100000   <- quota / period; 200000 / 100000 = 2 vCPU

If quota is max or unset, the container is not CPU-limited and nproc is honest. If quota is set, the effective vCPU is quota / period.

Per-worker memory footprint of a hot Node process, before adding cluster:

ps --no-headers -o pid,rss,cmd -C node | \
  awk '{print $1, $2/1024 " MB", $3, $4, $5}'

Multiply by the worker count you’re planning. If that total is within 70% of the container memory cap, cluster is safe. If it pushes past 85%, drop a worker.

Whether you’re actually CPU-bound (cluster helps) or I/O-bound (cluster barely moves the needle):

# event loop lag from inside the app, sampled for 10s
node -e "
  const { performance } = require('perf_hooks');
  let last = performance.now();
  setInterval(() => {
    const now = performance.now();
    console.log('lag ms:', (now - last - 100).toFixed(2));
    last = now;
  }, 100);
"

Sustained event-loop lag above 50 ms while CPU is high means you are CPU-bound and cluster will help. Lag under 10 ms while CPU is also low means you are I/O-bound and cluster is solving a problem you don’t have — your bottleneck is downstream (database, upstream API, disk).

The fix

For a multi-vCPU container that is CPU-bound, use the platform’s process supervisor (or PM2 if your platform doesn’t have one — see the PM2 vs PaaS-native supervisor note) and configure cluster with workers = vCPU. The Node side looks like:

// server.js
const cluster = require('cluster');
const os = require('os');
const app = require('./app');

const workers = parseInt(process.env.WEB_CONCURRENCY || '', 10) ||
                Math.max(1, os.availableParallelism() - 1);

if (cluster.isPrimary && workers > 1) {
  for (let i = 0; i < workers; i++) cluster.fork();

  cluster.on('exit', (worker, code, signal) => {
    console.log(`worker ${worker.process.pid} exited (${signal || code}), restarting`);
    cluster.fork();
  });

  process.on('SIGTERM', () => {
    for (const id in cluster.workers) cluster.workers[id].kill('SIGTERM');
    setTimeout(() => process.exit(0), 30000).unref();
  });
} else {
  app.listen(process.env.PORT || 3000);
}

os.availableParallelism() honors the cgroup CPU quota, so this returns 2 on a 2-vCPU container — os.cpus().length doesn’t, and will lie on most PaaS containers. The WEB_CONCURRENCY env var lets the platform override the count without a redeploy.

package.json scripts that the platform will execute:

{
  "scripts": {
    "start": "node server.js",
    "start:single": "node app.js"
  },
  "engines": {
    "node": ">=20.0.0 <21.0.0"
  }
}

If your platform exposes its own process supervisor that already spawns N child processes pinned to cores, don’t double-cluster. Pick one boundary: either the platform forks workers and you run a single process per worker, or the platform runs a single container and you cluster inside it. Both fork in different layers and you’ll waste memory and create confusing crash semantics.

For single-vCPU containers, run a single Node process and lean on horizontal autoscaling. The configuration is WEB_CONCURRENCY=1 (or unset) and let the platform add containers when CPU sustained over 60s crosses 70-80%.

For workloads that load large objects into memory (a model, a translated catalog, a generated AST), share them via a sidecar — Redis, a small in-memory cache process, or the platform’s shared object store — instead of replicating per worker. Cluster mode is for parallelizing request handling, not for paying the memory tax N times on read-mostly data.


If you’d like our team to pick the worker count, autoscaling triggers, and supervisor boundary for your specific Node workload on PaaS, Cloud PaaS covers that under environment management — open a scoping conversation.