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
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
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
Persistent term can be very costly if you do updates or deletes. Worst case it can cause a GC of the whole system.
mpope
If the map is never updated and never needs to be cleaned up then a :persistent_term could work, too.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #javascript
- #code-sync
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








