|
| 1 | ++++ |
| 2 | +title = "Elixir: Rate limit your LiveView forms" |
| 3 | +description = "Beware of back-and-forths in your Live Views" |
| 4 | +date = 2025-11-17 |
| 5 | +[taxonomies] |
| 6 | +tags = ["tip", "elixir", "phoenix", "liveview"] |
| 7 | ++++ |
| 8 | + |
| 9 | +## Context |
| 10 | +Not so long ago, on a Friday morning, while I was peacefully making some coffee and preparing for a relaxed day after weeks of crunch, I received a strange message that would eventually transform this day into a totally different experience. |
| 11 | + |
| 12 | +The feedback was about a specific Live View form that was behaving weirdly, and the phenomenon was supposedly new: using the form was causing some data to be lost, multiple selected items would be erased, and so on. |
| 13 | + |
| 14 | +I immediately thought about a bad connectivity issue but couldn't reproduce the issue on our test environment, even with the integrated browser throttling tools. |
| 15 | + |
| 16 | +But I eventually made it. With a VPN, connected to servers far away from my servers' location. |
| 17 | + |
| 18 | +## Issue |
| 19 | +Simply put, the culprit was the form itself, and how it was handling validation: on each key stroke. That was causing issues when the user experienced some connection latency (even with a fast connection, because of the delay that may happen between HTTP calls), and so LiveView's frontend part was overloading the backend, causing various behaviors, such as a flashing UI, unresponsive widgets, or even lost data. |
| 20 | + |
| 21 | +## Solution |
| 22 | +I remembered encountering a similar situation with React, and this is honestly Frontend Development 101: you have to throttle or debounce your validations, and you don't need to execute them while the user isn't done typing their name or their phone number anyway. |
| 23 | + |
| 24 | +Since its early days, [LiveView provides an easy way to do that][0]. `phx-debounce` allows passing an integer value representing milliseconds, or "blur", so that you can decide if you'll trigger validation once the input loses focus, or after a varying period of time. `phx-throttle` does the same thing, except it emits an event on the first change before the rate limit. |
| 25 | + |
| 26 | +So, nothing revolutionary here, but you'll probably want to check and optimize your LiveViews (and your components, and [libraries][1]) *before* it gets used by thousands of people. |
| 27 | + |
| 28 | +Some days just make you feel you're the cop being shot two days before retirement. |
| 29 | + |
| 30 | +[0]: https://hexdocs.pm/phoenix_live_view/1.1.17/bindings.html#rate-limiting-events-with-debounce-and-throttle |
| 31 | +[1]: https://github.com/nkezhaya/live_phone/pull/169 |
0 commit comments