sc4224
Using Agents or redis to store user sessions
Hi I’m new to session storage in the backend. Would you store a session in an agent or something like redis?
Most Liked
akoutmos
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








