chris-menz

chris-menz

How to design a Phoenix application to leverage scaling features

I’m trying to plan my application structure and was wondering if someone who knows a bit more about how elixir works under the hood could give me advice on which of my two ideas are more scalable for the least amount of complexity. I am a beginner with Elixir and Phoenix and have really been enjoying it and want to learn more. I want to jump into a full project with it that I have been planning for a while

One thing I have learned about web sockets in other languages is that they have to be scaled in a different way that a simple crud api, so it might make sense to separate them into different services.

There’s three capabilities my backend needs to run. CRUD/HTTP Rest api, scheduled background task (fetching from external data source and writing to db multiple times an hour for about 15 hours every single day), and a real-time chat.

In the future, my application will need to fetch and serve live stream video that runs all the time during the day but won’t run at night.

With other tooling it would make sense to separate these into different services for scaling purposes. Do tools like Oban background jobs and Phoenix Channels solve this scaling issue under the hood? Or would a micro services design still provide scaling benefits?

Most example projects I see are mono repos. I am leaning toward this as it will make modeling relations across services much easier to manage. I’m more just interested in learning how things like Supervisor work. Is what the problem I described the use case for Supervisor / the actor model, or am I totally off base here?

Would love if anyone could point me to some resources on more advanced Phoenix/Elixir topics.

Most Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

For a newcomer, I think you have a very solid grasp of how to get going. From a raw users standpoint I don’t think you’ll run into any streaming issues at all. Keep in mind the whole elixir package management system ran for a very long time on a single 512mb server. Your ECS + Docker idea sounds like a fine idea, but you could probably get away with simpler depending on whether ECS is something you are familiar with or not.

Live streaming and video work however is an area I know less about, so perhaps someone else can weigh in on that. I’m going to add a membrane tag to the post to see if we can help.

From a code management / maintenance standpoint I definitely think you have the right idea with a phoenix mono repo.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Hi @chris-menz welcome! This is a potentially very large topic, but let me kick it off by providing some answers that I think reflect community norms, lay out the assumptions underlying those norms, and then you can see what may be different in your case.

Simple answers: A single Elixir application that has Phoenix for UI / chat / REST API, Oban for background jobs and postgres as your database would be the recommend place to start.

So, you haven’t actually talked about any scale, so it’s hard to say. But what can be said is that efficient use of Postgres, fronted by Phoenix, will easiliy scale to tends or even hundreds of thousands of concurrent users on a single server.

I would make a choice about separate services depending more on your team and the size of the team than the technologies. If you have just 1 small team, starting with separate services is likely to introduce a lot of complexity, because the boundaries between those services is unlikely to be very clear at the start, and the cost of refactoring is much higher. Deployment becomes significantly more complicated as well.

The way you are framing this is good. Building this to be the scale of youtube at the start is going to be super complex, and you can cross that bridge when you come to it. At the start simplicity is very important so that you can iterate quickly, and you just want to make sure the scaling will be good enough that it won’t cause issues prematurely. A traditional Phoenix app is pretty well tuned to give you what you want here.

To provide anything more concrete though you’d need to be more concrete about what you expect the application to need to do.

dimitarvp

dimitarvp

Then you can follow Phoenix’s tutorials and you’re already covered. A brand new app with zero optimizations and no caching will handle even 10k concurrent users just fine.

Though depending on database usage I’d tune some of your Ecto.Repo parameters e.g. queue_target can be a higher value (say 5000ms) so nobody gets the “request timed out” page even in loaded conditions.

And that still wouldn’t be on Elixir, it’d be your DB being the bottleneck.

Last Post!

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

A lot of it depends on what those channels do, but in The Road to 2 Million Websocket Connections in Phoenix - Phoenix Blog they fit 400k simultaneous channel connections on a single 16gb server.

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement