hudsonbay

hudsonbay

HAProxy load balancer in front of Phoenix nodes

Is it a common practice to put a load balancer like Nginx or HAProxy in front of different Phoenix Nodes?
My question is because maybe a single Phoenix web server is capable of handling even more open TCP connections than a single reverse proxy in front of them, because maybe the reverse proxy will go down before the Phoenix web server does. I’m even assuming I have a failover technique for the cluster of reverse proxies.

Also my question is, which is the recommended way to horizontally scale phoenix web servers?

Definitely this is a question from a newbie :slight_smile:

Thanks in advance

Marked As Solved

Andrei

Andrei

Hi there

I’ve used a plethora of web servers and proxies/loadbalancers over the years. It all depends on your needs. If you’re a single developer like me, you want the best energy conservation to productivity ratio.

If you have only one site on a VPS then yes, run Phoenix directly!

If you have multiple Phoenix apps and multiple sites then you need something to do load balancing and redirect requests from your 80/443 ports to your apps.

Here i’ve worked with many others and I’d recommend HAproxy, which I’m currently using to handle requests to a variety of apps including Phoenix apps.

Haproxy scales as long as you

  • Make sure your VPS/server never writes to SWAP
  • Don’t have anyother app which is high CPU intensive on the server where you’ve got haproxy

However, even now as we’re speaking I’m searching a way to use cowboy (or another erlang/elixir land app) for the reverse proxy stuff. I want simplicity and energy/productivity conservation because each app you add-> complexity. I was able to find GitHub - heroku/vegur: Vegur: HTTP Proxy Library. However it hasn’t been updated in some time and I’m not sure it has the latest HTTP/2 support.

I got GitHub - tallarium/reverse_proxy_plug: 🔛 an Elixir reverse proxy Plug with HTTP/2, chunked transfer and path proxying support working perfectly with just ONE LINE of code. However it doesn’t support websockets out of the box.
And this is a bummer.

Scaling will depend on other things aswell, your database will be the hardest hit so you’ll have to decide which databases you’ll want to use and scale that before you’ll have worries about Phoenix.

Good luck

Also Liked

konstantine

konstantine

I asked a similar question on this forum not long ago. Having enjoyed the benefits of a Caddy 2 server in front of Phoenix, I would now go as far as saying that it was a no-brainer. Here’s an updated list of the things Caddy 2 helped me achieve (I presume that most would likewise apply to HAProxy):

• adding support for the latest protocols, such as TLS 1.3 or HTTP/3 (experimental)
• serving a maintenance page when the Cowboy / Phoenix web server is down
• adding security and other headers to all requests, or to a broad range of requests
• adding an authentication token header, e.g. to protect a staging server
• redirecting automatically from the domain root to the www subdomain
• dealing with the issue of trailing slashes in URL paths
• hosting static websites on the reverse proxy server itself
• serving multiple domains / subdomains that share a single IP address

pablodavila

pablodavila

From what I’ve heard most people do set up a reverse proxy in front of Phoenix. I haven’t had to run a Phoenix server in production but I’ve used nginx in front of other servers with no issues and would probably do it with Phoenix.

I think it’s generally easier to manage SSL and configuration this way; I don’t think you’ll run into performance issues. That’s my 2 cents anyway.

bobek

bobek

It really depends on your deployment architecture.You would typically use some sort proxy as a load-balancer to send traffic to more then one application server. That would simplify your deployments, as it is easier to drain nodes.

You would also handle ssl certs on the front-end nodes. Configure all your cypher suites and such.

If you have microservices, you may also push additional functionality into the front-end layer. For example authentication. Your services may then trust headers as they would be injected by the front-end machine and not user directly.

axelson

axelson

Scenic Core Team

You might be interested in a combination of:

Those two libraries are what I use to serve two Phoenix applications:

Without those applications being aware that they’re behind a proxy. And it’s actually all running on nerves via GitHub - nerves-project/nerves_system_vultr: Experimental - join the #nerves channel on elixir-lang slack if interested (although that is experimental)

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
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 record...
New
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New

Other popular topics Top

aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
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
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement