Score:0

Shall I do load balancing in nginx, pm2 or both?

eg flag

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?
djdomi avatar
za flag
Requests for product, service, or learning material recommendations are off-topic because they attract low quality, opinionated and spam answers, and the answers become obsolete quickly. Instead, describe the business problem you are working on, the research you have done, and the steps taken so far to solve it.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.