Skip to main content

What does PM2 do for scaling?

PM2 is a powerful process manager for Node.js, and when it comes to scaling, it acts as an automatic clusterer and load balancer.

Short version:

PM2 lets you scale a Node.js app across every CPU core, launching several processes (workers), and distributes requests among them - with no need to use cluster by hand.

Now let's break it down.

What PM2 is

PM2 (Process Manager 2) is a manager for running Node.js applications, providing:

  • starting, restarting and monitoring processes;
  • logging and memory management;
  • clustering (scaling);
  • zero-downtime deployment.

In other words:

PM2 is a "layer" on top of Node.js that manages cluster and child_process for you, making them transparent to the developer.

1. How PM2 scales an app

When you run:

javascript
pm2 start app.js -i max

PM2 does the following under the hood:

  1. Figures out how many CPU cores the system has (for example, 8).
  2. Launches 8 independent Node.js processes running app.js.
  3. Creates a built-in cluster (equivalent to Node.js's cluster).
  4. Distributes HTTP requests across processes using Round Robin.
  5. Watches process health, restarting them on failure.

So PM2 clusters automatically, with no need to write cluster code yourself.

2. Examples in different modes

Manual scaling:

javascript
pm2 start app.js -i 4

⟶ Starts 4 instances of the app (the number you gave).

Scaling across every CPU core:

javascript
pm2 start app.js -i max

⟶ PM2 automatically detects how many cores you have and starts one process per core.

Dynamic scaling:

javascript
pm2 scale app +2

⟶ Adds 2 more workers to the ones already running.

javascript
pm2 scale app 0

⟶ Stops every process of the app.

3. What PM2 does "under the hood" when scaling

StepWhat happens
1PM2 launches several Node.js processes (via cluster.fork())
2Each process has its own event loop, memory and PID
3PM2 becomes the "master" and manages the workers
4Requests are distributed among workers (Round Robin)
5PM2 tracks process health (CPU, memory, status)
6A worker is automatically restarted on failure
7Scaling can happen without stopping the app (zero-downtime reload)

4. Zero-downtime scaling

The command:

javascript
pm2 reload app

⟶ Restarts every worker one at a time, so the server never stops accepting requests.

It's the equivalent of a cluster hot reload:

One process restarts → the rest keep serving traffic → once the new one is up, the next worker restarts.

Ideal for updating production code with zero downtime.

5. Example: an Express server with PM2

app.js

javascript
const express = require('express'); const app = express(); app.get('/', (req, res) => { res.send(`Response from process ${process.pid}`); }); app.listen(3000, () => { console.log(`Server started, PID=${process.pid}`); });

Starting it:

javascript
pm2 start app.js -i max

Now:

  • Every process listens on port 3000.
  • PM2 acts as a load balancer, distributing requests.
  • If a process crashes, PM2 starts a new one.
  • If traffic grows, workers can be added with pm2 scale app +2.

6. How PM2 distributes load

PM2 uses the same mechanism as cluster:

  • The master process listens on the port.
  • Workers connect to it.
  • Incoming requests are distributed across workers evenly.

The default algorithm is Round Robin, meaning each next request goes to the next process.

7. What else PM2 does when scaling

CapabilityWhat it does
MonitoringTracks CPU, memory, uptime (pm2 monit)
Automatic restartBrings a crashed process back up
Auto-reloadRestarts on file changes (--watch)
Cluster modeAutomatically uses Node.js's cluster API
Ecosystem fileLets you describe several apps and environments (ecosystem.config.js)
PM2 Plus / PM2 EnterpriseSends metrics to the cloud and provides a web dashboard
Docker integrationScales easily inside containers

8. How PM2 scaling differs from other approaches

ApproachHow it scalesWhere it's used
clusterManually in code, via the APIDirectly in Node.js
PM2Automatically, via the CLISimple local scaling
Docker/KubernetesAt the container levelHorizontal scaling
Nginx / a load balancerAcross serversCross-server distribution

In other words:

PM2 handles vertical scaling (across CPU cores), while Kubernetes / Nginx handle horizontal scaling (across servers).

9. Advantages of PM2 for scaling

AdvantageDescription
Easy to startOne command: pm2 start app.js -i max
Uses every CPU coreNo manual cluster code
Automatic restartNo need to watch for crashes
Zero-downtime reloadUpdates with no interruptions
Monitoring and logsConvenient DevOps tools
Works with any frameworkExpress, Nest, Fastify, and others

10. PM2's limitations

DrawbackDescription
Works within a single serverDoesn't scale across different machines
Doesn't synchronize stateSessions and cache need to live in Redis/a database
Doesn't balance across machinesNginx, AWS ELB or Kubernetes are needed for that
Less control than manual clusterPM2 decides for itself how to distribute processes

Summary

CriterionDescription
What PM2 does for scalingLaunches several Node.js processes and distributes load
How it's implementedVia the built-in cluster API
Type of scalingVertical (across CPU cores)
ControlVia the CLI (start, scale, reload)
Notable featuresZero downtime, monitoring, automatic restart
Good forApps, APIs, single-machine production servers

Conclusion:

When scaling, PM2 creates a cluster of Node.js processes, uses every CPU core, automatically balances load, watches worker health and restarts them on failure - all without writing cluster code yourself.

It's an ideal tool for vertically scaling Node.js apps and managing them in production.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.