elvanja

elvanja

Rate limiting API with GenStage?

I have a rather expensive action (some PDF generation and upload to an other service) that occurs when related API endpoint is invoked. At the moment, there’s a pool of workers processing those requests and doing what they’re supposed to. However, this approach results in workers filling up their inboxes, potentially overwhelming the system.

The idea is to apply back pressure to API clients and just reject requests if certain conditions are met, e.g. all workers are busy.

Now, workers would be consumers within GenStage driven architecture. However, I’m at loss as to how to construct the producer part. Or, I could make it so that requests are queued somewhere and a producer that reads them, but no idea how to apply back pressure to API clients.

https://github.com/loucash/eqm solves this idea in the sense that I can use it in e.g. endpoint controller to dispatch work to workers and get information if the pool is exhausted or not, consequently informing the API client. So I’m looking for a similar solution via GenStage, if applicable.

Thank you for your time!

Most Liked

technomage

technomage

Look at the buffering demand part of the GenStage docs https://hexdocs.pm/gen_stage/GenStage.html. It includes a QueueBroadcaster which shows how to manage demand and back pressure. In the case of a web API the caller can make calls any time they choose. You would need to provide some reply they interpret as rejecting input that they would be responsible for resubmitting at a later time. This would be similar to how the public APIs like google provide rate limits. The request gets an error that is interpreted as hitting a limit.

jeroenvisser101

jeroenvisser101

There’s also an example in the gen_stage repo for rate limiting that you might want to check out.

Where Next?

Popular in Questions Top

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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
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

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
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
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
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
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement