Rate-limiting mathematics: rates, windows, and GCRA
What mathematical model hides behind a rule such as 100 requests per minute? We build Token Bucket and GCRA from average rate to burst tolerance.
Learning goal
Understand the relationship between limit, window, rate, interval, and burst tolerance, and explain the core mathematics of Token Bucket and GCRA.
Prerequisites
- Elementary algebra
- Basic time and rate concepts
1. From limit and window to rate
If L requests are allowed during a time window W, the average allowed rate is L divided by W. For example, 100 requests per 60 seconds is about 1.67 requests per second.
2. Token Bucket
In Token Bucket, tokens arrive at a refill rate and each request consumes tokens according to its cost. Bucket capacity controls how much burst traffic can be absorbed at once.
- rate: token generation speed
- capacity: maximum stored tokens
- cost: logical request cost
- remaining: available token budget
3. The GCRA idea
GCRA tracks a theoretical arrival time instead of simply counting requests. For limit L over window W, the base interval and burst tolerance follow these relationships.
4. Numerical example
For L=100 and W=60s, the base interval is 0.6 seconds. The burst-tolerance quantity from this formula is 0.6×99 = 59.4 seconds. This timing quantity is not the same thing as Token Bucket capacity.
5. Why this mathematics matters in systems
In a distributed system, the meaning of rate and time must be explicit: what is counted, which clock is used, and what decision is returned near the limit. These are part of system semantics.
6. Direct connection to RateLimitEngine
RateLimitEngine implements Fixed Window, Sliding Window, Token Bucket, and GCRA, using Redis server time and atomic transitions for distributed state. It is a direct example of turning mathematical behavior into an engineering contract.
7. Exercises
- Compute the average rate for 60 requests in 30 seconds.
- Explain the difference between Token Bucket capacity and GCRA burst tolerance.
- If one request costs 5 permits, what happens to remaining capacity?
Related project
RateLimitEngine
A .NET 8 implementation of four rate-limiting algorithms, including Token Bucket and GCRA.
Open sourceWorking on rate limiting or load-control semantics?
We can examine the model, algorithm, and distributed behavior together.