vitorfam
I’m currently using Redis to cache some information in my application. But I saw that ETS may be faster at lookup actions (https://github.com/minhajuddin/redis_vs_ets_showdown) . With that in mind, I want to create an ETS table at my app startup, so I can use it in a Plug.
I’ve tried to the following, but it did not work.
defmodule MyApp.Validator do
@bucket :ets.new(:bucket, [:set, :protected])
def put_validation(id) do
:ets.insert_new(@bucket, {id, %{"active" => true, "user" => "test"}}
end
end
It returns argument error. I think it’s because the ETS does not exist.
How and where can I initialize an ETS in order to use it inside my application?
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication):
toke...
New
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #ai
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #metaprogramming










Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
akhanaton
You can do it in the start function of your application.ex, just put the init code
:ets.new(:bucket, [:set, :protected])
right before the “opts = […]”
Then in your code:
kokolegorille
I prefer to have a dedicated GenServer, in charge of the ETS table… It starts the table in the init section.
vitorfam
It didn’t work. I’ve also removed the
:protectedtag, but still getting argument error.vitorfam
I’ll try it! Thanks!!
akhanaton
Try adding the :named_table atom to the list passed into new when you initialize it:
:ets.new(:bucket, [:set, :protected, :named_table])
hauleth
This will be evaluated in compile time, after that the VM is shut down and restarted, which mean that such table will be long gone. If you need to use that table then you either need pass table ref as an argument:
And then use it like
Or you need to create process that will be table owner:
vitorfam
I’ve created a GenServer as you and @kokolegorille suggested and it worked! Thank you very much.
Just to register what I’ve done, so others can see it.
I created a file called
validator.exinside mylib/myapp/folder with:Then, I start the GenServer at
lib/myapp/application.exTo test it, start the server with
iex -S mix phx.server. When MyApp.Validator.init/1 is called, it prints “Creating ETS MyApp.Validator”.