roeland

roeland

Changing GenServer state without blocking calls

Hi,

I have a huge map which I store in a GenServer to quickly handle calls. Another process is responsible for creating this map and then sends it to the GenServer. However, as this table is very large, it blocks the GenServer from responding to other requests while the map is loaded into memory. I am not sure what would be the best approach to sending the map to the GenServer, while handling requests and I cannot find the right keywords to find the solution.

One possible solution I came up with is instead of updating the state of the GenServer to start a new GenServer process with the map, wait until it is ready and then direct requests to this process and stop the previous GenServer process. Using a Registry this is probably not very difficult, but I expect that I am not the first to run into this problem and that there are off-the-shelf solutions that I just cannot find.

I would love a recommendation on how to approach this problem. Thanks!

Marked As Solved

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

This seems like a perfect case for :ets. :ets tables are another kind of key value structure that you can actually edit and query from concurrently. They also don’t need to be garbage collected.

Also Liked

rvirding

rvirding

Creator of Erlang

The only thing to be aware of with ETS is that the interface has basically no transactions so if you are going to allow multiple processes to update the tables you can get into a mess if you are not careful. The solution is generally to have one server which does all the upgrading.

rvirding

rvirding

Creator of Erlang

Persistent term can be very costly if you do updates or deletes. Worst case it can cause a GC of the whole system.

mpope

mpope

If the map is never updated and never needs to be cleaned up then a :persistent_term could work, too.

roeland

roeland

That sounds brilliant. We previously used :ets, but then it was overkill. I reread ETS - The Elixir programming language and it describes something very close to what I want to do :+1:

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New

Other popular topics Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

We're in Beta

About us Mission Statement