Mike2012
Mike2012

Reputation: 7725

Specific considerations when using pm2 cluster mode to launch node.js app

I would really like to use cluster mode to do some basic load balancing for my node.js app but I haven't found any documentation about special considerations when running in cluster mode. Are there any special modifications that need to be made to ones code when running in cluster mode? I would assume all node endpoints will be executed atomically but I haven't found any info about this.

Upvotes: 0

Views: 800

Answers (1)

Dave Mackintosh
Dave Mackintosh

Reputation: 2796

I doubt that much/any of the codebase would be executed any differently let alone atomically, it depends on the OS/hardware more than a process manager like pm2.

There's no need for you to change anything in your code to use pm2's cluster mode, just use the -i flag and set to the desired number (usually the number of available physical CPUs available on the system. Usually you can do pm2 start --name MyApp -i $(nproc) otherwise you can manually do it in your code using the cluster module.

Assuming var cluster = require('cluster') you can change the schedulinglink method by setting cluster.schedulingPolicy to cluster.SCHED_RR if you want Round Robin scheduling or cluster.SCHED_NONE if you want the OS to choose the scheduling.

Upvotes: 2

Related Questions