Table of Contents [expand]
Last updated November 17, 2026
Node.js has a limited ability to scale to different container sizes. It’s single-threaded, so a single Node.js process can’t take advantage of additional CPU cores.
To use all of a larger dyno’s CPU, run multiple Node.js processes. This “clustering” is supported by the Node.js Cluster API. This article recommends how many processes to run and how much memory to give each one on every dyno size, and provides a script that clusters your app for you.
The examples in this article use ES modules. To run them, set "type": "module" in your package.json or use the .mjs file extension.
Heroku Enterprise customers with Premier or Signature Success Plans can request in-depth guidance on this topic from the Customer Solutions Architecture (CSA) team. Learn more about Expert Coaching Sessions here or contact your Salesforce account executive.
Recommended settings
We recommend:
- Number of processes – 1 on dynos with 1 GiB of memory or less. On larger dynos, one per vCPU.
- Heap limit per process – half of the dyno’s memory divided by the number of processes, up to 4096 MiB. Set it with
--max-old-space-size.
Half of the dyno’s memory leaves room for memory Node.js uses outside the heap limit, such as New Space and Buffers. 4096 MiB is the largest heap Node.js chooses by default. If your dyno has more memory than that per vCPU, run more processes rather than giving each one a larger heap. See Troubleshooting Node.js Memory Use for details.
The tables below apply these recommendations to each dyno size. To run more than one process, see Clustering your app.
Cedar-generation apps
| Dyno size | Memory | vCPU | Processes | Heap per process (MiB) |
|---|---|---|---|---|
| Eco, Basic, Standard-1X | 512 MiB | shared | 1 | 256 |
| Standard-2X | 1 GiB | shared | 1 | 512 |
| Private-S, Shield-S | 1 GiB | 2 | 1 | 512 |
| Performance-M, Private-M, Shield-M | 2.5 GiB | 2 | 2 | 640 |
| Performance-L, Private-L, Shield-L | 14 GiB | 8 | 8 | 896 |
| Performance-L-RAM, Private-L-RAM, Shield-L-RAM | 30 GiB | 4 | 4 | 3840 |
| Performance-XL, Private-XL, Shield-XL | 62 GiB | 8 | 8 | 3968 |
| Performance-2XL, Private-2XL, Shield-2XL | 126 GiB | 16 | 16 | 4032 |
Fir-generation apps
| Dyno size | Processes | Heap per process (MiB) |
|---|---|---|
| dyno-1c-0.5gb | 1 | 256 |
| dyno-2c-1gb | 1 | 512 |
| dyno-1c-4gb | 1 | 2048 |
| dyno-1c-8gb | 1 | 4096 |
| dyno-2c-4gb | 2 | 1024 |
| dyno-2c-8gb | 2 | 2048 |
| dyno-2c-16gb | 2 | 4096 |
| dyno-4c-8gb | 4 | 1024 |
| dyno-4c-16gb | 4 | 2048 |
| dyno-4c-32gb | 4 | 4096 |
| dyno-8c-16gb | 8 | 1024 |
| dyno-8c-32gb | 8 | 2048 |
| dyno-8c-64gb | 8 | 4096 |
| dyno-16c-32gb | 16 | 1024 |
| dyno-16c-64gb | 16 | 2048 |
| dyno-16c-128gb | 16 | 4096 |
| dyno-32c-64gb | 32 | 1024 |
These values don’t change automatically. When you change your dyno size, update your heap limit and number of processes to match.
Don’t choose the number of processes with os.cpus() or os.availableParallelism(). On Fir and on Cedar Basic and Standard dynos, these can report the host machine’s CPUs rather than your dyno’s, so the count can be several times too high.
Clustering your app
This example uses the Node.js Cluster API to run an Express app as a cluster of worker processes. A primary process starts the workers, replaces any that exit, and shuts them down gracefully when the dyno stops:
import cluster from 'node:cluster';
// Set this to the number of processes recommended for your dyno size.
const WORKERS = 2;
if (cluster.isPrimary) {
// Only `cluster` is loaded up here. The primary just forks and manages
// workers, so it shouldn't import your app. Importing it at the top of the
// file loads a full copy of the app into the primary as well as every worker.
let shuttingDown = false;
const exits = [];
for (let i = 0; i < WORKERS; i++) cluster.fork();
cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} exited code=${code} signal=${signal}`);
// Don't replace workers that exit because the dyno is shutting down.
// Exit once the last one is gone.
if (shuttingDown) {
if (Object.keys(cluster.workers).length === 0) process.exit(0);
return;
}
// Throttle restarts. Keep a 60s window of exit times: one exit per
// worker per minute re-forks after 1s, and anything beyond that doubles
// the delay (2s, 4s, 8s...) up to 30s, so a crash loop backs off instead
// of re-forking instantly.
const now = Date.now();
exits.push(now);
while (exits[0] < now - 60_000) exits.shift();
const delay = Math.min(1000 * 2 ** Math.max(0, exits.length - WORKERS), 30_000);
setTimeout(() => {
// Check again: SIGTERM may have arrived while this restart was waiting.
if (shuttingDown) return;
cluster.fork();
}, delay);
});
process.on('SIGTERM', () => {
// Stop restarting workers and pass SIGTERM on to them. On Cedar, every
// process in the dyno already receives it, but on Fir only this one does.
// Exit once the workers are gone, or after 25s, before Heroku's 30s SIGKILL.
shuttingDown = true;
const workers = Object.values(cluster.workers);
if (workers.length === 0) process.exit(0);
for (const worker of workers) worker.process.kill('SIGTERM');
setTimeout(() => process.exit(0), 25_000).unref();
});
} else {
// Only workers load the app.
const { default: express } = await import('express');
// ...import your routes and middleware
const app = express();
const server = app.listen(process.env.PORT || 3000);
// Shut down gracefully on SIGTERM: stop accepting new connections,
// let in-flight requests finish, then exit.
process.on('SIGTERM', () => {
server.close(() => process.exit(0));
// Force exit after 20s, before the primary's 25s exit and Heroku's 30s SIGKILL.
setTimeout(() => process.exit(1), 20_000).unref();
});
}
Set WORKERS to the number of processes for your dyno size from the tables above, and set the heap limit in your Procfile. For example, on a Performance-M dyno, set WORKERS to 2 and use:
web: node --max-old-space-size=640 server.js
Node.js flags on the primary process, such as --max-old-space-size, apply to every worker.
Shutting down gracefully
When a dyno restarts or stops, Heroku sends SIGTERM to your app and gives it 30 seconds to exit. By default, Node.js exits immediately on SIGTERM, which drops any requests still in progress.
In the clustering example, each worker stops accepting new connections and finishes its in-flight requests before exiting. If your app runs a single process, handle SIGTERM the same way:
process.on('SIGTERM', () => {
server.close(() => process.exit(0));
});
Start your app with node in your Procfile, not npm start. When the dyno stops, npm exits as soon as it receives SIGTERM, without waiting for your app, so in-flight requests are dropped even if your app handles SIGTERM.
Tuning the number of processes
Each app has unique memory, CPU, and I/O requirements, so there’s no such thing as a one-size-fits-all scaling solution. Start with the recommended settings, then load test your app and watch its memory usage and response times.
- If your app needs more memory per process – run fewer processes and raise the heap limit to match.
- If your app spends most of its time waiting on I/O – such as database queries or API calls, you can try more processes than vCPUs.
When you change the number of processes, recalculate the heap limit: half of your dyno’s memory, in MiB, divided by the number of processes, up to 4096. For example, 3 processes on a Performance-M dyno (2560 MiB) get --max-old-space-size=426.
The total memory used by all processes must stay within your dyno’s memory limit. See Troubleshooting Node.js Memory Use.