Skip to content

Commit 0434fbb

Browse files
committed
Tweaks
1 parent 556f3cc commit 0434fbb

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

rate_limiter.rst

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,24 @@ Its main drawback is that resource usage is not evenly distributed in time and
3333
it can overload the server at the window edges. In the previous example, a user
3434
could make the 4,999 requests in the last minute of some hour and another 5,000
3535
requests during the first minute of the next hour, making 9,999 requests in
36-
total in two minutes and possibly overloading the server.
36+
total in two minutes and possibly overloading the server. These periods of
37+
excessive usage are called "bursts".
3738

3839
Sliding Window Rate Limiter
3940
~~~~~~~~~~~~~~~~~~~~~~~~~~~
4041

41-
The sliding window algorithm is gracefully handling the drawback from the fixed
42-
window algorithm. To reduce bursts requests the rate limit is calculated based on
42+
The sliding window algorithm is an alternative to the fixed window algorithm
43+
designed to reduce bursts. To do that, the rate limit is calculated based on
4344
the current window and the previous window.
4445

45-
For example: The limit is 5,000 requests per hour. If a user made 4,000 requests
46+
For example: the limit is 5,000 requests per hour; a user made 4,000 requests
4647
the previous hour and 500 requests this hour. 15 minutes in to the current hour
4748
(25% of the window) the hit count would be calculated as: 75% * 4,000 + 500 = 3,500.
4849
At this point in time the user can only do 1,500 more requests.
4950

5051
The math shows that the closer the last window is, the more will the hit count
5152
of the last window effect the current limit. This will make sure that a user can
52-
do 5.000 requests per hour but only if they are spread out evenly.
53+
do 5,000 requests per hour but only if they are spread out evenly.
5354

5455
Token Bucket Rate Limiter
5556
~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -85,11 +86,12 @@ enforce different levels of service (free or paid):
8586
framework:
8687
rate_limiter:
8788
anonymous_api:
88-
strategy: fixed_window
89+
# use 'sliding_window' if you prefer that strategy
90+
strategy: 'fixed_window'
8991
limit: 100
9092
interval: '60 minutes'
9193
authenticated_api:
92-
strategy: token_bucket
94+
strategy: 'token_bucket'
9395
limit: 5000
9496
rate: { interval: '15 minutes', amount: 500 }
9597

0 commit comments

Comments
 (0)