Live data from Hacker News

An Interactive Guide to Rate Limiting

blog.sagyamthapa.com.np

31–40 of 48 posts

Re: An Interactive Guide to Rate Limiting

#31
post #3

I wonder if anyone has switched algorithms after hitting real-world scaling issues with one of those? Curious if there are any “gotchas” that only show up at scale. I only have experience with fixed window rate limiting

We used leaky bucket IIRC and the issue I saw was that the distributed aspect of it was coded incorrectly and so depending on the node you hit you were rate-limited or not :facepalm:

So it wasn’t really implemented correctly then.

Re: An Interactive Guide to Rate Limiting

#32
post #3

I wonder if anyone has switched algorithms after hitting real-world scaling issues with one of those? Curious if there are any “gotchas” that only show up at scale. I only have experience with fixed window rate limiting

I have experience with token bucket and leaky bucket (or at least a variation where a request leaves the bucket when the server is done processing it) to prevent overload of backend servers. I switched from token bucket to leaky bucket. Token bucket is “the server can serve X requests per second,” while leaky bucket is the “the server can process N requests concurrently.” I found the direct limit on concurrency much more responsive to overload and better controlled delay from contention of shared resources. This kind of makes sense because imagine if your server goes from processing 10 QPS to 5 QPS. If the server has a 10 QPS token bucket limit it keeps accepting requests and the request queue and response time goes to infinity.

Re: An Interactive Guide to Rate Limiting

#33
post #12

Something I’ve long wondered is why you never hear about rate limiting algorithms that are based on the cost to serve the request or algorithms that dynamically learn the capacity of the system and give everyone a fair share. In the field of router buffer management, there are algorithms like Stochastic Fair Blue, which does the latter, but is somewhat hard to apply to HTTP because you’d have to define a success/fail…

One of those times when I was still learning that asking forgiveness is easier than asking permission, I wanted to eliminate a very expensive presence calculation that I and a coworker determined were accounting for almost 10% of average page load time. Some idiot in product has decided they wanted an OLTP-ish solution that told you -exactly- how many people were online and like a fool I asked if we could do a sane version and they said no. If you don't ask, then it's not insubordination.

For situations where eventual consistency is good enough, you can run a task in a loop that tries every n seconds to update a quantity. But as you say that can also saturate, so what you really want is for the task to update, then wait m seconds and go again, where m is more than the time you expect the task to complete in (I don't think voluntary rate limiting on the client side gets enough column inches. Peer to peer you end up footguning yourself if you bite off more than you can chew, and if you start stalling on responses then you gum up the server as well.

Re: An Interactive Guide to Rate Limiting

#34
My favourite algorithm is generic cell rate algorithm (GCRA). It works like token bucket in this post. The implementation is dead simple and requires no background tasks and needs very minimal state.

Instead of storing the current number of tokens, you instead store when the bucket will be full. If you take a token from the bucket, you increment the timestamp accordingly by 1/rps. The only complication is it the filled timestamp was in the past, you have to first update it with the current timestamp to avoid overfilling.

What's even nicer is that it doubles as a throttle implementation rather than just a rate limiter. You know the bucket is empty if you compute empty_at=filled_at-(max_tokens/rps) which is still in the future. From that calculation you now know when it will have capacity again, so you can sleep accordingly. If you use a queue before the gcra, it then starts sowing down new connections rather than just dropping them.

You should still have a limit on the queue, but it's nice in that it can gracefully turn from token bucket into leaky bucket.

Re: An Interactive Guide to Rate Limiting

#35

> Follow Sagyam's Blog's journey > By following, you'll have instant access to our new posts in your feed. > Continue with Google > More options As soon as I see this in a blog, I quit tab. Why do authors do this to themselves?

They do it when their real goal is to funnel you into a newsletter to later sell you stuff. The only purpose of the article is to show you that prompt.

Re: An Interactive Guide to Rate Limiting

#36
post #16
post #8

Earlier quoted context omitted.

It really looks AI generated

Yes, I usually prompt (Claude, GPT and Deepseek),on my rough vision, and take ideas from all of them. They never quite get it right on their own. But for a code that's deploy and forget, AI generated code is good enough.

you're 80% there, especially with functionality, but it needs some polishing

Re: An Interactive Guide to Rate Limiting

#37
post #29
post #21

Earlier quoted context omitted.

- I like bullet points, they are easy to read. - "Working" I wanted to keep things consistent. - Content getting cut was a limitation of iframe. Most blogging platform don't allows you to embed another page. This was best I could do given the limitation. - I do use AI to bounce ideas, but a lot of effort went into getting the apps working as intended.

Why "Working?" It's unclear what that means. Is it supposed to say, "How it works"?

Now that you have mentioned it should have been working principle or algorithm. It made sense in my head. English isn't my first language sorry about that.

Re: An Interactive Guide to Rate Limiting

#39

> Follow Sagyam's Blog's journey > By following, you'll have instant access to our new posts in your feed. > Continue with Google > More options As soon as I see this in a blog, I quit tab. Why do authors do this to themselves?

Sorry about that that is my blogging platform Hashnode. It was lesser of four evils:

- Medium which paywalls the article and forces you to sign up just to read.

- Substack has same problem, it's great for funneling people to your paid newsletter but there is a sign up banner as soon the page loads.

- Build your own and miss out on the social aspect and there's no proof if the numbers are real.

Re: An Interactive Guide to Rate Limiting

#40

My favourite algorithm is generic cell rate algorithm (GCRA). It works like token bucket in this post. The implementation is dead simple and requires no background tasks and needs very minimal state. Instead of storing the current number of tokens, you instead store when the bucket will be full. If you take a token from the bucket, you increment the timestamp accordingly by 1/rps. The only complication is it the fill…

Intresting, I am working on another list of more advance rate limiting algorithms. I will add GCRA there.
Post reply on HN