Stop 17 · CI/CD & DeploymentTopics · 7/9

PM2

Part of the Backend Developer Roadmap

In short

Run node server.js over SSH, close the laptop, and your API dies with the terminal. PM2 keeps it running, restarts it when it crashes and uses all your CPU cores.

A production process manager for Node.js that keeps your app running, restarts it on crashes, and can run it in cluster mode across multiple CPU cores — a common choice for simpler, non-containerized deployments.

The purple blob restarts one stalled machine on a night shift while the others keep running.

Think of it like this

The night-shift supervisor

A garment factory runs all night. When one machine jams, the supervisor doesn't wait for morning: he restarts it in seconds and notes it in the register. He also runs one machine per worker so nobody sits idle. PM2 is that supervisor for your Node processes.

Watch it happen

PM2 keeping workers alive

You
PM2
Worker 0
Worker 1

pm2 start api.js -i 2

1/6You ask PM2 to run the app in cluster mode with two workers.

The big ideas

What you need to know

Auto-restart

If the process crashes, PM2 brings it back. A climbing restart count in pm2 ls is a handy warning sign.

Cluster mode

With -i max, PM2 runs one worker per CPU core and balances connections across them.

Survives reboots

pm2 startup plus pm2 save registers your apps with the system, so they return after a server restart.

Logs in one place

pm2 logs streams output from every worker. Add the pm2-logrotate module so log files don't fill the disk.

See it in code

Walk through the code

js
1// ecosystem.config.cjs2module.exports = {3  apps: [4    {5      name: "api",6      script: "dist/server.js",7      instances: "max",        // one per CPU core8      exec_mode: "cluster",9      max_memory_restart: "500M",10      env: { NODE_ENV: "production", PORT: 3000 },11    },12  ],13};14 15// pm2 start ecosystem.config.cjs && pm2 save16// pm2 reload api   (restarts workers one at a time)

1/4Cluster mode with one worker per CPU core.

Keep this file in the repo so every server runs the app the same way. pm2 monit gives a live view of CPU and memory per worker.

Save yourself the pain

Common mistakes

Oops

Running the API with nohup node server.js & and walking away.

Do this instead

nohup won't restart a crashed process or bring it back after a reboot. Use PM2 or systemd.

Oops

Keeping sessions in memory and then turning on cluster mode.

Do this instead

Each worker has its own memory, so users get logged out at random. Keep sessions in Redis or use tokens.

Your mission

Crash-proof your API

~20 min

Quick check

You run 4 PM2 cluster workers and keep a rate-limit counter in a JavaScript Map. What goes wrong?

Go deeper

  1. 1Run a Node app under PM2 and confirm it restarts automatically after a crash.
  2. 2Use PM2's cluster mode to run your app across all available CPU cores.
  3. 3Learn `pm2 logs` and `pm2 monit` for basic production monitoring.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge