12

does anyone know if it is possible to change in NodeJS PM2 the number of cluster processes for an application at runtime?

regards Philipp

Philipp
  • 177
  • 1
  • 1
  • 10
  • 1
    To clarify, are you asking if it's possible to change the amount of cluster processes once you have started the app running? – Martyn Davies Mar 28 '15 at 18:28

4 Answers4

22

You can use pm2 scale to scale vertically the number of process at runtime, note that it only work with cluster mode. Example :

  • pm2 scale APPNAME 2 will scale the process to exactly 2 instances.
  • pm2 scale APPNAME +2 will add two process.
  • pm2 scale APPNAME -1 will remove one process.

source link

dschu
  • 4,992
  • 5
  • 31
  • 48
vmarchaud
  • 466
  • 3
  • 7
  • 2
    Note that "pm2 scale APPNAME 2" will not add two processes, rather scale to a total of two processes. Use "+2" to add two processes. See my answer to scale using the Programmatic API too. – Andrew Apr 05 '18 at 20:59
2

specify pm2 settings in json format:

{
 "apps": [{
    "name": "server",
    "script" : "index.js",
    "instances": 2,
    "exec_mode: "cluster",
    "cwd": "/path/to/script"
 }]
}

start the server:

pm2 start application.json

suppose you want to add 2 more instances, just run the same command again:

pm2 start application.json

check the processes list:

pm2 list

to test that all 4 instances are run in cluster mode:

pm2 restart server

it will restart each of the 4 processes.

Yevgen Safronov
  • 3,977
  • 1
  • 27
  • 38
0

At runtime (after the application is started), there are 2 ways to "scale" the application:

1) With the command line (documented here under "Scaling your cluster in realtime"), like this:

pm2 scale <app name> <n>

Note that can be a consistent number which the cluster will scale up or down to. It can also be an addition such as pm2 scale app +3 in which case 3 more workers will be added to the cluster.

2) With the Programmatic API (docs are here, but scale is not documented). As it's not documented, here's how you do it:

pm2.scale(<APPNAME>, <SCALE_TO>, errback)

Note that is the number that will be scaled up or down to, not the number added or removed. Here's a full example of connecting to and scaling to 4 instances:

var pm2 = require('pm2');
pm2.connect(function (err) {
    pm2.scale('appname', 4, function(err, procs) {
        console.log('SCALE err: ', err);
        console.log('SCALE procs: ', procs);
    });
});
Andrew
  • 2,368
  • 1
  • 23
  • 30
0

We had the same problem and did not find any solution. To solve this problem i wrote a small plugin for PM2 which can help dynamically scale applications based on utilization demand. You can try and play with it https://www.npmjs.com/package/pm2-autoscale

Or just install it directly

pm2 install pm2-autoscale

When module detects that CPU utilisation is higher then expected it will start increasing instances to max CPUs-1 and if server has available free memory.

When module detects CPU utilization is decreasing it will stop useless instances.

VeXell
  • 63
  • 2
  • 6