Suggest an editImprove this articleRefine the answer for “What does PM2 do for scaling?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**PM2** is a Node.js process manager that automatically clusters an app across every CPU core (`pm2 start app.js -i max`), balances requests Round Robin, and reloads workers with zero downtime (`pm2 reload`). **Key point:** PM2 wraps the same `cluster` API - just without having to write the clustering code by hand.Shown above the full answer for quick recall.Answer (EN)ImagePM2 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 | Step | What happens | |---|---| | 1 | PM2 launches several Node.js processes (via `cluster.fork()`) | | 2 | Each process has its own event loop, memory and PID | | 3 | PM2 becomes the "master" and manages the workers | | 4 | Requests are distributed among workers (Round Robin) | | 5 | PM2 tracks process health (CPU, memory, status) | | 6 | A worker is automatically restarted on failure | | 7 | Scaling 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 | Capability | What it does | |---|---| | Monitoring | Tracks CPU, memory, uptime (`pm2 monit`) | | Automatic restart | Brings a crashed process back up | | Auto-reload | Restarts on file changes (`--watch`) | | Cluster mode | Automatically uses Node.js's `cluster` API | | Ecosystem file | Lets you describe several apps and environments (`ecosystem.config.js`) | | PM2 Plus / PM2 Enterprise | Sends metrics to the cloud and provides a web dashboard | | Docker integration | Scales easily inside containers | ## 8. How PM2 scaling differs from other approaches | Approach | How it scales | Where it's used | |---|---|---| | `cluster` | Manually in code, via the API | Directly in Node.js | | **PM2** | Automatically, via the CLI | Simple local scaling | | Docker/Kubernetes | At the container level | Horizontal scaling | | Nginx / a load balancer | Across servers | Cross-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 | Advantage | Description | |---|---| | Easy to start | One command: `pm2 start app.js -i max` | | Uses every CPU core | No manual `cluster` code | | Automatic restart | No need to watch for crashes | | Zero-downtime reload | Updates with no interruptions | | Monitoring and logs | Convenient DevOps tools | | Works with any framework | Express, Nest, Fastify, and others | ## 10. PM2's limitations | Drawback | Description | |---|---| | Works within a single server | Doesn't scale across different machines | | Doesn't synchronize state | Sessions and cache need to live in Redis/a database | | Doesn't balance across machines | Nginx, AWS ELB or Kubernetes are needed for that | | Less control than manual `cluster` | PM2 decides for itself how to distribute processes | ## Summary | Criterion | Description | |---|---| | What PM2 does for scaling | Launches several Node.js processes and distributes load | | How it's implemented | Via the built-in `cluster` API | | Type of scaling | Vertical (across CPU cores) | | Control | Via the CLI (`start`, `scale`, `reload`) | | Notable features | Zero downtime, monitoring, automatic restart | | Good for | Apps, 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**.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.