I am using Cachex to store several types of data for fast retrieval, but all of it is also in the database. I have experimented with adding a task to the
application.ex that will read the database and populate the ETS table (i.e. Cachex) with current sessions. It works… but during deployments it can start barging so much if the database isn’t available when the app starts that it made me take it out.
Is that the wrong approach? My auth layer looks ONLY to the ETS cache, NOT to the database (for performance reasons), so it would be a problem if the app had to be restarted for any reason – everybody would get logged out! Is there a better way to warm up cache that isn’t so brittle?
I have something like this in my
defmodule Auth.Application do alias Auth.Contexts.SessionContext use Application def start(_type, _args) do import Supervisor.Spec, warn: false children = [ worker(Cachex, [:sessions_cache, ], id: :sessions_cache), ] # See https://hexdocs.pm/elixir/Supervisor.html # for other strategies and supported options opts = [strategy: :one_for_one, name: Auth.Supervisor] ret = Supervisor.start_link(children, opts) # Trying this as a task outside of the supervision tree... warmup_task = Task.async(fn -> SessionContext.warm_up_cache() end) Task.await(warmup_task) ret end end
What are alternative approaches? Better ways?