josefrichter

josefrichter

Eventually persisting in-memory state?

Suppose I have a GenServer or Agent that holds some state and serves immediate responses.

What are some strategies to asynchronously persist this state in database, without slowing down the GenServer, but then also handling failures resulting in portion of state not written? In a way, this is sort-of “write cache” or buffer/queue for high-concurrency write situations…

My specific example: I have limited-capacity course with 30 seats, and I have 10000 within single second trying to enroll. I wanna put them in a queue, and give them immediate response whether or not they made it among the first 30, but then in the next step I also need to write it to db, so that the initial response is “set in stone” so to speak. It would be fine to send them kind-of “pre-approval” immediately in the first step, and then “full confirmation” a few seconds later once it’s written in the database.

I didn’t study computer science, so maybe this is a common problem with simple solution.

Thank you!

Most Liked

josefrichter

josefrichter

Thank you. I was thinking or Redis ordered sets (ordered by time of signing up) but was waiting whether someone will bring it up instead of (D)ETS and Mnesia - they do seem to offer similar features, but it feels like a few bits and pieces need to be added manually, while Redis is more up to date with today’s needs and has it all out of the box… So I would just ZADD everyone into ordered list, then save the first 30 in bulk to Postgres, but keep the list as a “waiting list” around, maybe just using Redis persistence options…

Then I was thinking also about not scaling prematurely because I believe Postgres is in fact much more powerful than most people realize :slight_smile: However, I am worried mostly about the atomicity - INSERT with RETURNING the “rank” in the table, which might be not so performant anymore…?

This is a university system and there’s gonna be dozens, maybe hundreds, of courses - I was thinking each having its own GenServer taking care of the queue. And they open the floodgates at the same moment for all the students, which is 40k people – so it’s huge sudden spike in traffic, but it’s also true that the atomic writes, which I’m afraid of the most, would likely be spread at least over several seconds, so probably not reach the rate of thousands per second.

I’m just trying to prevent the repeating scenario that the system always goes down within seconds of opening and then they’re putting out fires for the whole day while people at home are crazily hitting refresh button all day, desperately needing to enroll before it’s all full :smiley:

stefanchrobot

stefanchrobot

How many instances of web server are you going to have? Is it going to sustain the 10000 requests in a second?

If one server instance is fine, then a single GenServer per course sounds like a good solution. Looks like your constraint is just a counter (30 seats). Assuming you have a reasonable DB (INSERT is just a few milliseconds), you can just write the record immediately and have all the consistency guarantees. So the GenServer would be something like:

  1. Start the GenServer with course id,
  2. On init (or handle_continue) get the booked seats,
  3. On each “book a seat” message, check the counter, write to DB if seat available, return result.

Be sure to bump the timeout on the GenServer.call when sending the “book a seat” message. I think this should be good enough, but it’s hard to tell without real performance tests.

If it turns out that you need multiple instances of the web server, then things get a bit more hairy. You could go with Erlang distribution or use out-of-process cache like Redis.

lucaong

lucaong

Yep, I mostly agree with what @stefanchrobot said.

You could theoretically go with a singleton GenServer (a single global process), but that will make horizontally scaling your app to several instances and ensuring you don’t loose data in case of restarts a bit more difficult.

Honestly, I would rather:

  • Implement it efficiently with standard database queries. You can go a long way with a database like Postgres before you hit such a bottleneck. How likely it is that you would actually get 10.000 requests per second? Remember you can always optimize your solution when traffic actually grows to the point you start to see scalability problems.

  • If you need more performance, I would implement this with Redis rather than in a GenServer. You could use a simple integer counter, or even a set, so you can efficiently check if the course is full and only write to database if not (in cases when the sets are much bigger and one is fine with an approximated count Redis even provides HyperLogLog). With Redis you would get performance, but also durability if you use the WAL, so you don’t risk loosing bookings if your server crashes or restarts.

Of course, it is possible with Elixir to run a single global GenServer in your cluster taking care of each counter, but you would need to take special care in your logic to make it work properly in all cases (think about restarts, deployments, etc.).

Where Next?

Popular in Questions Top

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
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
jaysoifer
Is there a way to rollback a specific migration and only that one ("skipping" all the other ones)? Would mix ecto.rollback -v 2008090...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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

Other popular topics Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
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
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
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
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
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
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
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
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 47849 226
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

We're in Beta

About us Mission Statement