Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pm2 cluster mode, The ideal number of workers?

Tags:

pm2

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?

like image 727
Jonatan Lundqvist Medén Avatar asked Oct 03 '15 22:10

Jonatan Lundqvist Medén


People also ask

How does PM2 cluster mode work?

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.

What is cluster in PM2?

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.

Does PM2 use cluster?

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.

How does PM2 Load Balancing Work?

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.

How to enable Cluster Mode in PM2?

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

How do I increase the number of workers in PM2?

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.

How many clusters does PM2 START-I-1 spread?

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.

How do I scale my applications in real-time using PM2?

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.


1 Answers

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

like image 106
Jonatan Lundqvist Medén Avatar answered Oct 23 '22 21:10

Jonatan Lundqvist Medén