# How to avoid multiple database queries when using LiveView streams

**URL:** <https://forum.elixirforum.com/t/how-to-avoid-multiple-database-queries-when-using-liveview-streams/64173>\
**Category:** Questions\
**Tags:** phoenix, liveview\
**Created:** [June 12, 2024, 8:37pm UTC](https://forum.elixirforum.com/t/how-to-avoid-multiple-database-queries-when-using-liveview-streams/64173 "2024-06-12T20:37:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![teamon](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/teamon/32/23732_2.png) [@teamon](https://forum.elixirforum.com/u/teamon)\
**Post date:** [June 12, 2024, 8:37pm UTC](https://forum.elixirforum.com/t/how-to-avoid-multiple-database-queries-when-using-liveview-streams/64173/1 "2024-06-12T20:37:45Z")

</div>

I’m reading through the example [todo\_trek](https://github.com/chrismccord/todo_trek/tree/main) app to try to understand how it all works.  
One thing I’ve noticed is that in the `validate` event handler the edited todo is built from incoming params:

[https://github.com/chrismccord/todo\_trek/blob/main/lib/todo\_trek\_web/live/todo\_list\_component.ex#L123](https://github.com/chrismccord/todo_trek/blob/main/lib/todo_trek_web/live/todo_list_component.ex#L123)

While this works for the todo\_trek app, it will fail when some other non-editable field is necessary to properly render the stream entry - let’s say for the sake of example that I’d like to render `todo.inserted_at` for every todo.

Since streams do not keep data in memory, in the `validate` handler I need to fetch todo from database to keep the `inserted_at` field rendered. This is not ideas, as the validate handler will be called multiple times, even when using `phx-debounce`.

One solution I can think of is to provide a short-lived cache using process dict with timer-based ttl.

What would be the preferred solution here?  
Is there a way to avoid `validate` events at all?

---

<div class="post-metadata">

**Author:** ![cmo](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/cmo/32/19618_2.png) [@cmo](https://forum.elixirforum.com/u/cmo)\
**Post date:** [June 12, 2024, 9:20pm UTC](https://forum.elixirforum.com/t/how-to-avoid-multiple-database-queries-when-using-liveview-streams/64173/2 "2024-06-12T21:20:21Z")

</div>

I don’t see why you would need to query the database on every change to get information that the client already has. You ensure the client sends it back by making it part of the form, no?

---

<div class="post-metadata">

**Author:** ![teamon](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/teamon/32/23732_2.png) [@teamon](https://forum.elixirforum.com/u/teamon)\
**Post date:** [June 12, 2024, 9:47pm UTC](https://forum.elixirforum.com/t/how-to-avoid-multiple-database-queries-when-using-liveview-streams/64173/3 "2024-06-12T21:47:30Z")

</div>

The inserted\_at field is not part of the form, it’s just rendered as `form.data.inserted_at`.

On the first render it’s all good because `form.data` is a full Todo from database, but after validate event it has only id and list\_id fields and rendering of inserted\_at breaks

---

<div class="post-metadata">

**Author:** ![cmo](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/cmo/32/19618_2.png) [@cmo](https://forum.elixirforum.com/u/cmo)\
**Post date:** [June 13, 2024, 1:05am UTC](https://forum.elixirforum.com/t/how-to-avoid-multiple-database-queries-when-using-liveview-streams/64173/4 "2024-06-13T01:05:17Z")

</div>

What is stopping you from making it part of the form? You want that data, you can get it by making it part of the form, you can keep it in memory or you can query for it.

With regards to your process dictionary cache idea, I’d probably use a proper cache so you’re not hand building a cache in every LiveView process. Kind of defeats the point of using streams in the first place doesn’t it?

---

<div class="post-metadata">

**Author:** ![KP123](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/k/278dde/32.png) [@KP123](https://forum.elixirforum.com/u/KP123)\
**Post date:** [June 13, 2024, 1:10am UTC](https://forum.elixirforum.com/t/how-to-avoid-multiple-database-queries-when-using-liveview-streams/64173/5 "2024-06-13T01:10:11Z")

</div>

Save them in your markup with `phx-value-*` properties. As outlined in the docs [here](https://hexdocs.pm/phoenix_live_view/Phoenix.LiveView.JS.html#module-enhanced-push-events) you could add `phx-value-inserted-at` to each todo then on event send this values.

Using an in memory cache makes no sense here as you could just save the struct directly in your live view state without having to manage a new dependency.

---

<div class="post-metadata">

**Author:** ![teamon](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/teamon/32/23732_2.png) [@teamon](https://forum.elixirforum.com/u/teamon)\
**Post date:** [June 13, 2024, 6:23am UTC](https://forum.elixirforum.com/t/how-to-avoid-multiple-database-queries-when-using-liveview-streams/64173/6 "2024-06-13T06:23:07Z")

</div>

Sending a single value might be ok, but imagine you need to also render Todo author avatar and you end up sending multiple values just to rebuild the Todo and User and Avatar schemas by hand. This quickly becames unmanagable and extremaly prone to errors. Not to mention that even with single inserted\_at field it can be only sent as a binary so you need to convert it to proper DateTime which is just another layer of complexity. And if you render another field but forget to send it back you will only notice when you try to edit the Todo as the first render will work just fine.

I don’t agree the cache doesn’t make sense - with a long lists you can still benefit from not having to keep everything in memory but only to recently used items.

A proof of concept of a process-local cache could be something like this:

```ex
defmodule LiveCache do
  @ttl :timer.seconds(60)
  
  def fetch(key, ttl \\ @ttl, fun) do
    case Process.get(key) do
      nil ->
        value = fun.()
        put(key, value, ttl)
        value

      {value, timer_ref} ->
        Process.cancel_timer(timer_ref)
        put(key, value, ttl)
        value
    end
  end

  def put(key, value, ttl) do
    ref = Process.send_after(self(), { __MODULE__ , :clear, key}, ttl)
    Process.put(key, {value, ref})
  end

  def clear(key), do: Process.delete(key)
end

## in LiveView process
def handle_info({LiveCache, :clear, key}, socket) do
  LiveCache.clear(key)
  {:noreply, socket}
end

## in (nested) LiveComponent
def handle_event("validate", %{"id" => id, "todo" => data}, socket) do
  todo = LiveCache.fetch({Todo, id}, fn -> Todos.get_todo!(id) end)
  {:noreply, stream_insert(socket, :todos, to_change_form(todo, data, :validate))}
end

```

While it’s possible to use socket.assigns as the cache storage it seems more complex to work with when using nested live components that have their own state. If the cache is to be shared between components it must be passed as attributes, potentially causing unnecessary rerendering.

A different way of solving this issue would be to have two zipped streams like this:

```elixir
todos = Todos.list_todos()
forms = Enum.map(todos, &to_change_form(&1, %{}))

# ...

<%= for {todo, form} <- zip(@streams.todos, @streams.forms) do %>
  <.avatar user={todo.user}/>
  <.form for={form}> ... </.form>
<% end %>

```

and then allow updates to one stream only while keeping the other intact.

---

<div class="post-metadata">

**Author:** ![cmo](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/cmo/32/19618_2.png) [@cmo](https://forum.elixirforum.com/u/cmo)\
**Post date:** [June 13, 2024, 7:53am UTC](https://forum.elixirforum.com/t/how-to-avoid-multiple-database-queries-when-using-liveview-streams/64173/7 "2024-06-13T07:53:10Z")

</div>

You have the same problem with forgetting to cache a value, no?

Is a cache per liveview better than a cache between all the liveviews and the database? You don’t think you’ll be caching the same thing in each liveview for each person on the page? And if you’re caching most of the data structure why bother with streams at all?

Are you sure that having a cache in the process dictionary that is designed to be accessed/edited in multiple places is easier to manage and reason about than passing it down explictly?

---

<div class="post-metadata">

**Author:** ![KP123](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/k/278dde/32.png) [@KP123](https://forum.elixirforum.com/u/KP123)\
**Post date:** [June 13, 2024, 12:30pm UTC](https://forum.elixirforum.com/t/how-to-avoid-multiple-database-queries-when-using-liveview-streams/64173/8 "2024-06-13T12:30:47Z")

</div>

If you need the data in memory on the server don’t use streams, then you only need to send the ID of the entity you want to work on back to live view.
