2

PM2 allows to run NodeJS apps on multiple instances, i.e., different cores, allowing load balancing using the same port.

PORT=3000 pm2 start -i NUMBER_OF_CORES(e.g 2) app.js

But I could also do load balancing in Nginx with different ports

upstream app_servers {
    server 127.0.0.1:3000;
    server 127.0.0.1:3001;
    server 127.0.0.1:3002;
    server 127.0.0.1:3002;
}

server { listen 80; server_name your-domain.com www.your-domain.com; location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $http_host; proxy_pass http://app_servers; } }

and then

pm2 start app.js -f --3000
pm2 start app.js -f --3001
pm2 start app.js -f --3002
pm2 start app.js -f --3003

Which is the best idea (I always assume the localhost does all the service)?

  • simply load balancing the same port on different instances (cores)
  • simply load balancing on different ports and let OS manage instances, or
  • load balancing by having different instances, each with a different port, thus using both Nginx and PM2 load balancers?

0 Answers0