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

jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
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
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
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
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New

We're in Beta

About us Mission Statement