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.

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
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
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
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
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
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 47930 226
New

We're in Beta

About us Mission Statement