tfischbach

tfischbach

Phoenix Channels in Distributed Systems

Hi,

currently I am testing running a Phoenix application on a system with multiple connected nodes. There are some observations I’d like to confirm/questions I’d like to ask that I could not find documented anywhere.

Consider the case where requests are load balanced between multiple phoenix applications on different (BEAM) nodes (via an external load balancer i.e. Docker Swarm ingress load balancing or HAProxy):

  1. When using websocket transport, channel communication is performed via one persistent HTTP connection which the load balancer routes to one of the Phoenix instances. The life span of a “channel session” (which begins with join and ends with terminate) is identical to the life span of that connection. Correct? Does Phoenix persist any channel related state that spans multiple such connections?

  2. When using long polling, the channel communication is divided into many requests. If the load balancer routes these to different Phoenix instances, I see errors in my JS client. Once Phoenix instances run on connected nodes, I so far was no longer able to reproduce these problems. Does Phoenix delegate channel traffic to the correct process across node boundaries? So far it looks like it does, but I could not find this documented anywhere.

  3. Assuming 2. is the case, what happens when one of the nodes disconnects because it is stopped (e.g. during a rolling update deploy)? Is there anything my application needs to do to ensure connections terminate gracefully (particularly in the long polling case) so that the client can establish a new connection?

Any insights on these topics or links to resources covering these questions would be greatly appreciated.

Most Liked

idi527

idi527

:waving_hand:

Please take these answers with a grain of salt since it’s been some time since I used phoenix. But I thought I’d write them down before someone more knowledgable comes around.


http connection?

Just to be clear, websocket connections are initiated with the help of an http upgrade request, but they operate over a tcp connection (just like http, though).

The channel process lifespan (channel session) is shorter than (or equal to) the lifespan of a Phoenix.Socket which supposedly corresponds to that of a cowboy process handling the tcp connection. I’m not aware of any persistence logic in phoenix channels which would’ve worked by default. You can persist the channels/socket states yourself though.


When using long polling, the channel communication is divided into many requests. If the load balancer routes these to different Phoenix instances, I see errors in my JS client. Once Phoenix instances run on connected nodes, I so far was no longer able to reproduce these problems. Does Phoenix delegate channel traffic to the correct process across node boundaries? So far it looks like it does, but I could not find this documented anywhere.

If the tcp connection is kept open (which is true by default in http/1.1), then there is no additional routing required from the load balancer and all your http long-polling requests go to the same node. It works just like websockets. If the connection is closed (if there is connection: close http header), then yes, the next request might be routed to another node in the cluster by the load balancer. Phoenix PubSub can route the messages within the cluster with the help of pg2, though, which is what probably happens in

Once Phoenix instances run on connected nodes, I so far was no longer able to reproduce these problems.


Assuming 2. is the case, what happens when one of the nodes disconnects because it is stopped (e.g. during a rolling update deploy)? Is there anything my application needs to do to ensure connections terminate gracefully (particularly in the long polling case) so that the client can establish a new connection?

The client would establish a new connection after a disconnect automatically (at least in phoenix.js). Whether the node to which the new connection is established has the previous channel state is mostly up to you, I think.


via an external load balancer i.e. Docker Swarm ingress load balancing or HAProxy

I like haproxy :+1:

Where Next?

Popular in Questions Top

albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
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
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
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement