@@ -33,23 +33,24 @@ Its main drawback is that resource usage is not evenly distributed in time and
33
33
it can overload the server at the window edges. In the previous example, a user
34
34
could make the 4,999 requests in the last minute of some hour and another 5,000
35
35
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".
37
38
38
39
Sliding Window Rate Limiter
39
40
~~~~~~~~~~~~~~~~~~~~~~~~~~~
40
41
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
43
44
the current window and the previous window.
44
45
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
46
47
the previous hour and 500 requests this hour. 15 minutes in to the current hour
47
48
(25% of the window) the hit count would be calculated as: 75% * 4,000 + 500 = 3,500.
48
49
At this point in time the user can only do 1,500 more requests.
49
50
50
51
The math shows that the closer the last window is, the more will the hit count
51
52
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.
53
54
54
55
Token Bucket Rate Limiter
55
56
~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -85,11 +86,12 @@ enforce different levels of service (free or paid):
85
86
framework :
86
87
rate_limiter :
87
88
anonymous_api :
88
- strategy : fixed_window
89
+ # use 'sliding_window' if you prefer that strategy
90
+ strategy : ' fixed_window'
89
91
limit : 100
90
92
interval : ' 60 minutes'
91
93
authenticated_api :
92
- strategy : token_bucket
94
+ strategy : ' token_bucket'
93
95
limit : 5000
94
96
rate : { interval: '15 minutes', amount: 500 }
95
97
0 commit comments