关于pm2 scale选项用途及集群数设为-1作用的技术咨询
PM2 Cluster Mode: Explaining
scale and -i -1 Usage Hey there! Let's break down your questions about PM2's cluster mode commands clearly:
1. What does pm2 scale [app-name] 10 do?
You already know that pm2 start app.js -i 4 launches your app with 4 cluster instances right from the start. The pm2 scale command is for adjusting the number of running cluster instances dynamically, without having to stop and restart your entire application.
- If your app is already running with, say, 4 instances, running
pm2 scale my-app 10will spin up 6 additional instances, bringing the total to 10. - Conversely, if you wanted to scale down (e.g.,
pm2 scale my-app 2), PM2 will gracefully shut down the extra instances, ensuring no in-flight requests get dropped unexpectedly. - This is perfect for handling traffic spikes or trimming resource usage during off-peak times—you don't have to kill the whole app to adjust capacity on the fly.
2. What happens when you run pm2 start app.js -i -1?
Setting the instance count to -1 tells PM2 to automatically match the number of cluster instances to your machine's CPU core count.
For example:
- If your server has 8 CPU cores, PM2 will start 8 cluster instances of your app.
- This is essentially a shortcut for using
-i max(which does the exact same thing). - The goal here is to fully leverage your server's multi-core resources—since Node.js is single-threaded by default, running one instance per core lets you maximize performance and throughput for your app.
内容的提问来源于stack exchange,提问作者Srinivas Nahak
相关产品推荐
相关产品推荐

