acrolink

acrolink

Hi,

I am developing a chat-bot using Phoenix and LiveView. My question, as the application gets more users, I will have to scale it up by adding more machines (using Kubernetes, AWS Elastic Beanstalk, AWS ECS etc). Would Phoenix handle this out-of-the-box?

Basically, there will be a load balancer routing incoming requests to different machines in a cluster (all running the same Phoenix application). Would LiveView and Sockets behave normally? Or does LiveView assume a single server machine running?

How to do that while scaling up horizontally?

Thank you.

Showing Posts 1 to 8

LostKobrakai

LostKobrakai

There is no expectation that you run a single machine. But there is an expectation that you run Phoenix.PubSub connected across all nodes (through one of their available adapters) if you want to make use of cluster wide PubSub. LV doesn’t technically need that, but given you’re talking about chat you might be using PubSub for message delivery between LV processes.

acrolink

acrolink OP

Thanks. If the chat data (actually sent always to Chat-GPT) i.e. the chain is stored in conn.assigns then I think Phoenix.PubSub is not needed ?!

Nicodemus

Nicodemus

That is correct. If each LiveView is isolated, meaning it doesn’t communicate with any other process, nor save any data on the local machine, then you can scale horizontally infinitely. Every client will get a LV process started on an available node in the cluster, which will be “sticky” in that it has an open websocket. On every refresh it will start a new LV process on perhaps a different node. You don’t even need to use BEAM distribution if all you do is save data as needed with a central repository, such as a shared database. The only thing you might have issues with is to make sure your load balancer supports websockets, and long lived connections for long-polling for those clients that fall back.

LostKobrakai

LostKobrakai

If clients fall back to long polling you either need sticky sessions or pubsub needs to work for LV to move state between nodes when the client eventually is routed to a different node on a new long poll request.

acrolink

acrolink OP

Same applies and holds true if using Channels where each channel is merely a server <==> specific user communication ?

Nicodemus

Nicodemus

Yup, LiveView is build on Phoenix Channels, so it behaves the same way with either.

Nicodemus

Nicodemus

If the websocket or long poll request is closed, a new request will be made to the load balancer, which might choose a new web server, but then a whole new Channel/LiveView process is created from scratch, so sticky sessions or state migration shouldn’t be needed. (Unless you’re doing something really really weird, like saving state locally on the machine.)

In the case of LiveView, the mount/3 might actually be called once in normal http (unconnected) mode, then when the phoenix.js takes over and creates a new websocket/long poll connection, a whole new machine could be selected, and the mount/3 will run the second time on a different machine. As long as you aren’t storing anything locally between those two requests, it’s fine that they happen on completely different machines. Each request should be atomic.

LostKobrakai

LostKobrakai

That’s not the case for long polling. With long polling you get a request per each message being sent on a channel (applies to LV as well given it uses channels). So it’s not one request/connection per livecycle of a channel process, but many. Hence you either need clustering to be able to connect to the process across the whole cluster or sticky sessions to not be routed to a different node.

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
psy-q
I’m trying to set up Emacs with elixir-ls via lsp-mode and credo via Flycheck. This should mostly be preconfigured as Flycheck picks up c...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews