I using PM2 to run my nodejs application.
When starting it in cluster mode "pm2 start server -i 0": PM2 will automatically spawn as many workers as you have CPU cores.
What is the ideal number of workers to run and why?
PM2 is a Production Process Manager for Node. js applications with a built-in Load Balancer. When properly configured, PM2 will automatically run your app in cluster mode, spawn workers for you, and take care of spawning new workers when a worker dies.
The cluster mode allows networked Node. js applications (http(s)/tcp/udp server) to be scaled across all CPUs available, without any code modifications. This greatly increases the performance and reliability of your applications, depending on the number of CPUs available.
exec_mode: "cluster" tells PM2 to use the Node. js cluster module to execute the code. instances:0 when this is set to 0, PM2 will automatically spawn a number of child processes that are equal to the available number of cores. Respawning on termination is handled out of the box.
When you use restart , pm2 kills and restarts all the processes at the same time. There is a short period of time during which the service is unavailable. If the reload system hasn't managed to reload your application, a timeout will fallback to a classic restart.
To enable the cluster mode, just pass the -i option: max means that PM2 will auto detect the number of available CPUs and run as many processes as possible NOTE: you need to set the exec_mode to cluster so PM2 know you want to load balance between each instances, by default it will not
If you need more or less workers than currently available, use PM2’s scale command and adjust the cluster size respectively: The scale command is only available for processes that already run in cluster mode. PM2 won’t restart your app which currently runs in fork mode.
If 4 cores, pm2 start -i -1 will spread 3 clusters (max - integer). In the context of clustering, you first need to be sure that your application has no internal state.
Using PM2 and its cluster mode allows you to scale your applications in real-time. If you need more or less workers than currently available, use PM2’s scale command and adjust the cluster size respectively: The scale command is only available for processes that already run in cluster mode.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With