tjdam
Hi, all!
So I’ve been using ETS to cache some data, the result from a series of queries so I don’t have to hit the database so often. The user sends a request, if there is an entry for it on ETS it returns the data and if not, it queries the database, returns it to the user and also adds to an ETS table.
So the solution I had in mind so the user always get the most updated data was to, every time some of the data changes, the update function calls the GenServer that I wrote to interface ETS and deletes everything and so the next user to send a request would get fresh data from the database and repopulate the ETS table with that fresh data.
def update_content(attrs) do
Cache.delete(:key, :table_name)
%Content{}
|> Content.changeset(attrs)
|> Repo.update()
end
The GenServer looks like this:
def delete(key, table) do
GenServer.cast(__MODULE__, {:delete, key, table})
end
def handle_cast({:delete, key, table}, state) do
:ets.delete(table, key)
{:noreply, state}
end
What is happening though is that sometimes the table entry is deleted, sometimes it’s not. The only thing I can think of is some sort of race condition going on, but I have no idea how that would be the case. Any ideas?
Also (except for the fact it’s not doing what I wanted it to do), is there anything wrong/inefficient with this approach?
I’m pretty new to ETS as well as writing my own GenServers, so this could be a very silly question…
Thank you!
Trending in Questions
Other Trending Topics
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
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #elixirconf-us
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #security
- #elixirconf-eu
- #processes
- #api
- #forms
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
mudasobwa
GenServer.handle_cast/2is asynchronous, so it’s not guaranteed to perform immediately. Probably changing it to synchronousGenServer.handle_call/3would fix the issue.In general, mastering
GenServeras an interface to:etsis an anti-pattern, because you basically construct a bottleneck (GenServer’s mailbox) in front of:ets, ruining its performance. If you want to stick toGenServer, I’d suggest to useAgentinstead of:ets.Or, use
:etsas is, without wrapping it intoGenServer.dmitrykleymenov
You have
GenServer.call, but handle acast. You have another handle or this code is invalid, it’s hard to tell what’s happening, check the current implementation and update plztjdam
Wouldn’t that cause the data to be garbage collected?
Thank you for your answer! I actually didn’t quite get why use a
GenServer, I did it because most of the stuff I read on ETS did use it and because I wanted to play more withGenServersOh, that was my mistake here, in the actual code it’s cast all through. Thank you!
dimitarvp
Playing creates habits that are hard to unlearn after. Do things that make sense in a production context.
GenServercentralizes the access to whatever it is having access to underneath. One of its uses is exactly this: make sure that access to something is linear / centralized, e.g. no actor can access the thing more than one at a time.:etsin contrast is specifically crafted to be as fast as possible and to tolerate parallel access (if the proper flags are passed to the table when created).So indeed, using both in tandem is rather weird.
tjdam
Fair enough. It’s just that I’m at that point where I can’t really make this assessment, though. I follow recipes then I go and try to understand it the best I can. Asking questions in this forum is probably where I learn the most!
Yeah, that makes sense. I’m thinking I call a module creating the tables I need at the
Applicationmodule then just call:etsfunctions straight up, yeah?dimitarvp
Even better, you can put an entry inside your
You must give it a name though and then refer to the table by it.
application.exfile (where OTP workers of your app are described and then started) and you’ll have any table you want created on your app’s startup.tjdam
Oh right! There’s no need for it to be under a supervision tree…!
That’s really cool, thanks for the tip!
dimitarvp
To clarify: it’s still going to be under a supervision tree but not one that you manage manually. It’ll be that of your app where Erlang takes care of bootstrapping all workers on your app’s startup. It’s still OTP mechanics but they are being taken care of for you.
tjdam
As I wrote my answer I thought of it, haha. There’s no code in the project that would not be part of the tree, right?
dimitarvp
Yep, that’s correct. That’s what the BEAM VM is about.