en86

en86

The "costs" of LiveView

I’m very new to Phoenix and LiveView but I’ve been loving it so far. I’m currently using it on a traditional web app that would otherwise need to be structured as a SPA. Speed/productivity/etc. are incredible.

This got me thinking, though, about the costs of LiveView outside the context of an obvious use case.

For example, imagine a high-traffic online newspaper or blog. I love how quick navigation flows with LiveView and how I can avoid Javascript. That being said, this speed isn’t free.

There is the memory required in keeping open a socket connection and maintaining state for each site visitor.

Also, it’s not clear to me how caching would even work (for example, will a cached page still hit the database after loading given that mount is presumably always called?)

I guess I’m just curious about how I should think about the trade-offs. Besides memory usage and potentially issues with caching, are there any other obvious things to take into account? I love LiveView but want to be realistic about its limitations before depending on it for every project.

Most Liked

karolsluszniak

karolsluszniak

This is a very interesting topic and a question that all responsible engineers should ask themselves when deciding whether to go with LV. For sure I did ask it to myself many times. It’s even more important considering that LV is a highly promoted feature in Phoenix 1.5+, backed by a homepage redesign.

If the answer is “just return static HTML” then by itself it sounds like LV won’t scale well for these cases which is also not something we should state lightly. For sure, I wouldn’t treat this question as silly one and I wouldn’t underestimate the paradigm shift for many “oldschool web dev” that come with websockets and LV statefulness.

For the “viral blog” case there may be many scenarios when LV would still be applicable & profitable even for displaying static blog entries. This includes:

  • rendering entire website via LV e.g. to reduce HTTP connection times
  • rendering typical blog entry interactions e.g. like button with live counter
  • rendering website-wide interactions e.g. notification or search popover in navbar

From my perspective it would be great to know up front if LV scales well (and how well) out of the box, what options are there for optimizing it (like caching) and if there are cases where it obviously won’t work due to scalability limitations. I guess what we really need is a comprehensive benchmark similar to the one about reaching 2 milion connections with single Phoenix instance. I’m not an expert at doing such benchmarks (esp. concerning websockets) but even without one we could just calculate the minimal memory footprint of a single active connection without extra assigns and use that to calculate the best case scenario “users per GB of RAM”. From then on it’d be all about understanding the cost of each assign and keeping that under control with temporary assigns etc - esp. in LVs that contribute to high-traffic pages.

In the end, we may always approach to LV as to “MVP supercharger” - a solution that allows to create dynamic websites at unprecedented pace but that always ends up converted to client-side/SPA code when the scale grows. But perhaps it doesn’t have to? With regular Phoenix, there’s a great chance that (depending of a case of course) you’ll hit DB scalability issues 10 times before you outgrow the webserver (like discussed e.g. here Scaling LiveView for low latency in multiple regions), but is that still the case with LV?

10
Post #3
otijhuis

otijhuis

Yes, people have been creating all kinds of examples to see how far they could push it. There’s only one way to see where things break down and that’s to use it in anger. I consider that a good thing.

Personally I don’t agree with general statements like “never use it for user interaction”. Always and never are words I try to avoid. It’s all about context. Maybe you have an admin ui or very low latency connection where the trade-off is worth it and it’s good enough. Is saying that something is right or wrong without knowing the context any better than someone blindly following the hype?

All we can do is try to educate people so they can make an informed decision.

otijhuis

otijhuis

There are a few things I would pay particular attention to when thinking about using LiveView:

Do we need offline support?

Most likely the answer is no but if it’s yes then you can’t use LiveView.

What infrastructure is the application going to be running on in production?

How does it handle websockets?
Are there memory restrictions?
Are you running clustered or single instances?

Yes, LiveView will have limitations but the architectural limitations are just as important.

How will we deal with latency?

This one is extremely important and easy to forget if you only develop locally or with a high speed connection. Simple dynamic interactions like opening a dialog can suddenly become really jarring because of the latency involved. Alpinejs can help in that specific case but that means you might not be able to solve everything with LiveView alone if you want to have a great user experience.

How will we handle connection failures / application updates etc?

Because the state is on the server, how are you going to persist this state and load it back? The application might be updated, the server might crash. Anything can happen. What do you do then with forms that people are filling in at that exact time or things like that. This is of course not a LiveView specific problem but since it’s different from how most web applications work these days it’s wise to pay attention to it from the start.

Scaling LiveView would actually be the least of my worries. You should be doing load/performance tests specific to your application anyway. Hardly anyone is going from 100 users to 1.000.000 users overnight. And if you didn’t expect that load in the first place then most likely the rest of your architecture will fail before Elixir/Phoenix/LiveView does.

You mentioned high-traffic online newspapers and blogs. If the content is basically static I would do as much as possible with static html and just use a CDN to host it. Any framework would be overkill in that scenario. Maybe you want a personalized newsfeed. In that case the most likely bottleneck will be retreiving all the personal content on the backend so you just have to test it.

I don’t know what caching you’re worried about exactly. LiveView is for dynamic pages. This means you normally want to fetch the latest state once you mount again. That’s no different from Vue/React apps and such. If you want to avoid database calls you should use a caching layer in the backend itself.

What your system can handle will depend on the specific application and the complete infrastructure. That’s why I would say that LiveView itself must be dependable when you use it. It shouldn’t break suddenly. If you should use it depends on the situation.

Where Next?

Popular in Questions Top

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
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
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
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

Other popular topics Top

axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 48342 226
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
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
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31013 112
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54120 245
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New

We're in Beta

About us Mission Statement