Testing, Performance & Deployment
Node.js PM2 Process Manager
PM2 keeps a Node application running: it restarts on crash, runs one process per core, reloads with zero downtime, manages logs and restores the process list after a server reboot.
What is PM2 Process Manager in Node.js?
PM2 keeps a Node application running: it restarts on crash, runs one process per core, reloads with zero downtime, manages logs and restores the process list after a server reboot.
PM2 Process Manager example
Terminal
pm2 start src/index.js --name api -i max # one worker per core
pm2 list
pm2 logs api --lines 100
pm2 reload api # zero-downtime restart
pm2 restart api # hard restart
pm2 monit # live CPU and memory
pm2 save && pm2 startup # survive a server rebootecosystem.config.cjs
JavaScript
module.exports = {
apps: [{
name: 'api',
script: 'src/index.js',
instances: 'max',
exec_mode: 'cluster',
max_memory_restart: '500M',
env: { NODE_ENV: 'production', PORT: 5000 },
}],
};Key points to remember
- reload is zero-downtime in cluster mode; restart is not.
- max_memory_restart is a safety net for slow leaks, not a fix.
- pm2 save plus pm2 startup is what makes the app survive a reboot.
- Containerised deployments usually let the orchestrator do this instead.
Common mistakes with PM2 Process Manager
- Forgetting pm2 save, so nothing comes back after a reboot.
- Cluster mode with in-memory sessions, which breaks across workers.
Node.js PM2 Process Manager— Interview Questions & FAQs
What is the difference between pm2 restart and pm2 reload?+
restart kills and restarts each process, causing a brief outage. reload starts new workers before stopping old ones in cluster mode, so no request is dropped.
