tjdam

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!

Showing Posts 1 to 10

mudasobwa

mudasobwa

Creator of Cure

GenServer.handle_cast/2 is asynchronous, so it’s not guaranteed to perform immediately. Probably changing it to synchronous GenServer.handle_call/3 would fix the issue.

In general, mastering GenServer as an interface to :ets is an anti-pattern, because you basically construct a bottleneck (GenServer’s mailbox) in front of :ets, ruining its performance. If you want to stick to GenServer, I’d suggest to use Agent instead of :ets.

Or, use :ets as is, without wrapping it into GenServer.

dmitrykleymenov

dmitrykleymenov

You have GenServer.call, but handle a cast. You have another handle or this code is invalid, it’s hard to tell what’s happening, check the current implementation and update plz

tjdam

tjdam OP

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 with GenServers :slight_smile:

Oh, that was my mistake here, in the actual code it’s cast all through. Thank you!

dimitarvp

dimitarvp

Playing creates habits that are hard to unlearn after. Do things that make sense in a production context.

GenServer centralizes 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.

:ets in 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

tjdam OP

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! :slight_smile:

Yeah, that makes sense. I’m thinking I call a module creating the tables I need at the Application module then just call :ets functions straight up, yeah?

dimitarvp

dimitarvp

Even better, you can put an entry inside your application.ex file (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. :wink: You must give it a name though and then refer to the table by it.

tjdam

tjdam OP

Oh right! There’s no need for it to be under a supervision tree…!
That’s really cool, thanks for the tip!

dimitarvp

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

tjdam OP

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

dimitarvp

Yep, that’s correct. That’s what the BEAM VM is about.

Where Next? Top

Trending in Questions Top

Hellowman
Hello everyone! I am new to coding with Elixir/Erlang. And i am looking for some utilities for fuzzing testing like AFL++. Does anyone ...
New
yarrichar
I’m very new to Elixir, so sorry if this is a silly question: I am wondering what happens to the mailbox of a process when the machine i...
New
pragdave
How do I tell Hologram not to escape HTML when substituting into templates? {Textile.render(@product.long_description)} escapes the HTM...
New
Fl4m3Ph03n1x
Background When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error: * assembling mark...
New
mandar1045
I load tested a plain Phoenix chat app backed by Postgres with 100 million messages in it, everything on default settings, to see what br...
New
rlipscombe
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
saleyn
Is there a way to import record definitions in the iex shell for pretty printing (similar to the rr command in the erl shell)? This woul...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
mudasobwa
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
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews