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!

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.

6 Likes

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

2 Likes

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:

1 Like

It does get updated depending on the usage hourly to weekly, from Erlang -- persistent_term it seems that the update is costly. Might still be a possibility :+1: . Thanks.

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

6 Likes

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.

7 Likes