pm2集群模式,理想的worker数量?

Pm2 cluster mode, The ideal number of workers?

我使用 PM2 运行 我的 nodejs 应用程序。

在集群模式下启动时 "pm2 start server -i 0":PM2 将自动生成与您拥有的 CPU 个内核一样多的工作程序。

运行 的理想工人人数是多少?为什么?

Beware of the context switch When running multiple processes on your machine, try to make sure each CPU core will be kepy busy by a single application thread at a time. As a general rule, you should look to spawn N-1 application processes, where N is the number of available CPU cores. That way, each process is guaranteed to get a good slice of one core, and there’s one spare for the kernel scheduler to run other server tasks on. Additionally, try to make sure the server will be running little or no work other than your Node.JS application, so processes don’t fight for CPU.

We made a mistake where we deployed two busy node.js applications to our servers, both apps spawning N-1 processes each. The applications’ processes started vehemently competing for CPU, resulting in CPU load and usage increasing dramatically. Even though we were running these on beefy 8-core servers, we were paying a noticeable penalty due to context switching. Context switching is the behaviour whereby the CPU suspends one task in order to work on another. When context switching, the kernel must suspend all state for one process while it loads and executes state for another. After simply reducing the number of processes the applications spawned such that they each shared an equal number of cores, load dropped significantly:

https://engineering.gosquared.com/optimising-nginx-node-js-and-networking-for-heavy-workloads