How do I use Elixir Term Storage (ETS) in Phoenix?

I would like to write a simple phoenix app that I can post JSON to. The app should hold on to the JSON and return it to me when I ask for it. I would like to be able to update this JSON from time to time.

I think I should use ETS for this.

I only have 4-5 blobs of JSON to store, but they will be accessed from millions of clients.

I was reading documentation here: Plug.Session.ETS — Plug v1.15.2 It says:

We don’t recommend using this store in production as every session will be stored in ETS and never cleaned until you create a task responsible for cleaning up old entries.

Also, since the store is in-memory, it means sessions are not shared between servers. If you deploy to more than one machine, using this store is again not recommended.

So, my question: how can I use ETS without violating the recommendation above. Also, how can I ensure that I don’t run into the problem information being shared between nodes.

Thanks.

1 Like

The getting started guides give some examples of how to create a basic :ets key value store.

As for the multi-node question, I would have some external thing act as the canonical store of the json blobs, and merely cache them with ets on each node.

2 Likes

Hi there!

The piece of text that you quoted just warns you to not use ETS as a session store, not to give up on it entirely! :slight_smile:

I strongly suggest you read this article that shows you basic ETS operations: https://elixirschool.com/lessons/specifics/ets/#key-lookup

Also, if you aren’t going to do a lot of updating, I suggest you set read_concurrency on, it will greatly speed up your lookups! :smiley:

3 Likes