@@ -33,23 +33,24 @@ Its main drawback is that resource usage is not evenly distributed in time and
3333it can overload the server at the window edges. In the previous example, a user
3434could make the 4,999 requests in the last minute of some hour and another 5,000
3535requests 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
3839Sliding 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
4344the 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
4647the 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.
4849At this point in time the user can only do 1,500 more requests.
4950
5051The math shows that the closer the last window is, the more will the hit count
5152of 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
5455Token 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