Hi I’m new to session storage in the backend. Would you store a session in an agent or something like redis?
It’s advisable to start with an elixir option before considering alternatives. If you don’t get the expected performance from Agents or ETS, then you can try other options.
@kodepett on that note, why would we use ets over agents?
ETS is just much faster
Agent is basically a GenServer; due to this, concurrent calls are serialized, ETS implementation is quite different allowing concurrent read and write
Like most things in software it’s a matter of tradeoffs. With ETS you’ll get a considerable performance bump when reading sessions (you can also refer to https://hexdocs.pm/plug/Plug.Session.ETS.html) if many requests come in from the same user and reference the same agent. And you also won’t have to worry much about spawning too many processes and taking down the BEAM (http://erlang.org/documentation/doc-5.8.4/doc/efficiency_guide/advanced.html).
On the other hand, when mutating data in ETS, you may come across some race conditions as data persisted to ETS will depend on what process writes last to it. In other words, if multiple requests come in from the same user and changes need to be made to their session data, the process that mutates ETS last will be the state of the session.
It sounds like if you’re just experimenting with things and learning, the ETS session Plug should work just fine. If you want further reading into ETS vs Agents/GenServers this is a good blog post: https://dockyard.com/blog/2017/05/19/optimizing-elixir-and-phoenix-with-ets
so which one is the best for storing user sessions
There is Plug.Session to manage this…
It can be configured to use ETS or Cookies. Here is official documentation.
The recommended way is not to use ETS in production.