Is it a problem to run several Elixir/Phoenix applications on the same server with a shared postgres db?

I have 3 Elixir/Phoenix applications and I have 2 Servers,

1 Server is the Production and the other is my Staging environment.

these 2 servers have each 1 postgres database running, they are quite beefy and CPU and Memory usage is very low.

Each server have a simple setup of running elixir/phoenix/liveview in it’s own directory with these 3 applications connecting to the same Postgres database installed on each Prod and Stage server isolated with 3 databases and 3 separate users for each application.

Then I launch 3 Elixir/phoenix instances each serving on a different port, a reverse nginx proxy takes care of the correct traffic routing.

Is this an acceptable setup?

I was thinking about running an Kubernetes cluster but I feel it’s not bringing much to the table since i would need ALOT of users before i would ever need to scale anything, which would be solved by just putting that 1 heavy used of the 3 apps on a separate server. I have setup everything with ansible and postgres dumps are being send to an S3 bucket so I can rebuild a server very fast and automated without any issue.

What am I doing wrong? Any advise? Am I going to run into troubles running these 3 separated apps on the same server?

Thanks for helping.

I see no problem with this setup as long as it can handle the load.

Here are the downsides that I can see of this approach.

  1. If one app get’s a ton a trafic, the other apps could struggle to handle the load even though they are unrelated to each other from the user’s perspective.
  2. If one app get’s compromised, then an attacker will be able to access all three apps relatively easily.

These are really acceptable downsides when you take into consideration that it simplifies your architecture a lot. And if you follow Phoenix best pratices, the chance of getting hacked are low. Kubernetes is good, but it’s complicated and for small projects it’s often overkill.

My advice would be to go with that setup and see how far you can go. You can keep upgrading the CPU/RAM/Harddrive until it doesn’t make sense anymore. Elixir/BEAM is amazing at vertical scaling.

Once you can’t scale that single server anymore, you can seperate the apps and/or the database to their own server, and scale even more. I believe you can go really far before this starts to become a real problem that something like Kubernetes would solve. By that point, your app would probably be successful enough that you would have the resources to invest in upgrading the infrastructure to handle huge loads.

2 Likes

Thanks, I forgot to mention that basically the choice is either leaving my current setup or installing proxmox or esxi and making 3 VMs on the 1 server to separate them, or leave them all 3 on 1 server baremetal, if that makes sense.

Personally, I would run a single OS on baremetal and seperate the apps using Docker. That way you keep more processing power for your apps but you still benefit from isolation through Docker which makes it easier to upgrade apps independently and is also more secure if you follow best practices.

4 Likes

Docker will never be more secure nor reliable compared to separate VMs.

The only advice I can give is to separate your database so it doesn’t share resources with your servers if you plan on having a lot of data/queries. I’ve worked once with a similar setup where we were running 2 elixir apps and 1 postgres database on a single VM and we were getting performance degradation when the database was under heavy load.

2 Likes

I kinda agree with you, it makes sense to just have 1 large database on bare metal and 3 vms on 1 server with proxmox connecting to that database.

I hope people realize it’s way cheaper these days to run things on prem than in the cloud :slight_smile:

3 Likes

Yes for sure! I ment it was more secure than hosting everything on the same baremetal server without using Docker.

2 Likes

That should do the trick, otherwise you could run a exsi + docker setup if you want to use a single server.

One VM for postgres and the other with the stuff you need for deploy, I usually use docker-compose for such setups where no distribution is needed.

Generally speaking yes, I am also a big fan of having your own hardware, but I would say you get benefits when you need beefier setup. If your product can get away with 100$ per month for hosting then it’s hard to motivate paying upfront for the server hardware that might cost 20k+ $.

2 Likes

It’s cheaper if you can afford the capex for hardware and you’re at a scale where you can utilize an entire server, for sure.

But if you’re just starting out a couple shared Hetzner cores will run you like $4 a month and even if you get your hardware for free at a garage sale I doubt you can even find an internet connection for that price.

It’s also not true for all hardware: GPU servers for training, for example, are an interesting case where shared infra is unbeatable. An H100 is like $30k or $2 an hour, so unless you’re a centibillionaire choosing rental is not even a question.

But of course where you really get into (financial) trouble is with PaaS. That’s when the prices go into orbit :slight_smile:

1 Like

Thanks, but for most SaaS that isn’t using their own ML model (are there still SaaS without AI lol) you just buy a good $1k second hand server and you’re good to go imho.

2 Likes

Bare metal is cheaper if you don’t care about operational concerns for your db :stuck_out_tongue:

However, on Scaleway you can actually use their RDS for database and buy bare metal for your hosts. Best of both worlds (I am still at a scale where I just stick with VMs because I don’t really need bare metal for any of our projects, but its nice to know I can spin up a 120gb ram server with 32 cores if I need one, for the cost of a 16GB 4vcpu vm in aws, give or take).

Edit: realized I am shilling Scaleway in two posts in a row. I am not affiliated or paid by them :laughing:

2 Likes

I’m not sure I am following you here - but are you saying you have Postgres installed on the server with three databases and each app only accesses its own database? If so Postgres can handle multiple databases, you can literally run hundreds of sites on a single server each with their own database (you might need to tweak the config at some point tho). I think the limit is something like 4M databases :lol: The main potential problem here is what @jekae said, that one app could use all the resources - but you would cross that bridge when you come to it (such as tweaks or moving an app to another server, upgrading the server, etc).

If you have more than one app accessing the same database then you have to be mindful that one might acquire locks on tables.

But in general…

Not really, not unless I am missing something; Postgres can run a large number of databases if your hardware can handle it (and it sounds like you have got some pretty powerful hardware) :smiley:

3 Likes

Thanks, no each app has its own database on 1 postgres server instance

1 Like