Pm2 cluster mode, The ideal number of workers?
Asked Answered
C

1

6

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?

Carry answered 3/10, 2015 at 22:58 Comment(0)
C
19

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

Carry answered 4/10, 2015 at 7:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.