I have a server configuration which spins up 4 instances of my server using pm2
and a load balancing setup that routes traffic (all network requests, including socket traffic) to each of these instances (nginx).
Here is my ecosystem.config.js
file:
const { cpus } = require('node:os');
const numCPUs = cpus().length;
console.log('number of CPUs: ', numCPUs);
const apps = Array.from({ length: numCPUs }, (_, index) => {
const result = {
script: './build/entry.js',
exec_mode: 'fork',
// exec_mode: 'cluster',
// instances: 'max',
name: `worker-${index}`,
node_args: '-r ts-node/register -r tsconfig-paths/register',
env: {
TS_NODE_BASEURL: './build',
PORT: 8700 + index,
},
};
return result;
});
module.exports = {
apps,
};
And my nginx.conf
file:
upstream backend_loadbalancer {
least_conn;
server localhost:8700;
server localhost:8701;
server localhost:8702;
server localhost:8703;
}
server {
index index.html index.htm index.nginx-debian.html;
server_name example.com www.example.com;
# static files
location / {
root /srv/www/example_web/dist;
try_files $uri /index.html;
}
# socket
location ^~ /sio {
proxy_pass http://backend_loadbalancer;
#. . .
}
# REST API services
location /api/ {
proxy_pass http://backend_loadbalancer/;
#. . .
}
}
This works as expected, but I’ve ran into a bit of a brick wall with trying to figure out how I’d be able to update my server in the future with little to no downtime.
If I make changes to my backend and want to update it, I reckon the best way to do so without incurring any of that downtime is to take these server instances down one by one. But the problem is that I’m not sure how to do so. I know I can stop an instance running on port, say, 8700, like so:
pm2 stop [process_id]
But then I’d have to also create another build directory, maybe call it build2
, and then I’d have to somehow restart this specific instance on this specific 8700 port:
pm2 start ecosystem.config.js -- 8700 build2 # pass the port and the name of build directory
Theoretically I can get this to work, check in ecosystem.config.js
to see if the port passed as an argument above matches the current port in the current iteration in Array.from
, but I just feel like there’s a much better way to do this.
I also considered splitting my server up into two (well, three) different physical servers, each IP address of which would be specified in the nginx.conf
load balancing config of the main “routing” server — this way, I can completely shut down one server, update it, bring it back up, and then shut down the second one. But the configuration of such a scenario would be odd, no? I’d have 3 servers, the first would just act as a loadbalancer/router, running nginx, and the other two would be up and running with as many instances as there are cores (which also sounds promising, because I can just scale each server as needed).