The cluster_mode will automatically wrap your Node.js app into the cluster module and will enable you to reload your app without downtime and to scale your processes across all CPUs available.
To enable the cluster_mode, just pass the -i
$ pm2 start app.js -i 1
max instances (
max depending on the number of CPUs available) and set the load balancer to balance queries among process:
$ pm2 start app.js --name "API" -i 0
DEPRECATED (STILL COMPATIBLE):
$ pm2 start app.js --name "API" -i max
If your app is well-designed (stateless) you'll be able to process many more queries.
Important concepts to make a Node.js app stateless:
- Sessions must not be stored in memory but shared via a database (Redis, Mongo, whatever)
- WebSocket/Socket.io should communicate via a database