Use Cluster Mode for load balancing and zero downtime
masterCluster mode allows you to start multiple instances of a Node.js application to load-balance HTTP/TCP/UDP queries across multiple CPU cores. This increases throughput and improves reliability.
To start an application in cluster mode, use the -i flag. The <processes> argument can be:
'max': Uses all available CPUs.'-1': Uses all CPUs minus one.- A specific integer: The exact number of instances to spawn.
Zero Downtime Reload: Use pm2 reload instead of restart to update applications without dropping connections.
# Start with max CPU instances
$ pm2 start api.js -i max
# Start with 2 instances
$ pm2 start api.js -i 2
# Zero downtime reload
$ pm2 reload all