5 Rate limiting algorithm you should?know
In February 2018, GitHub faced one of the largest DDoS attacks, peaking at 1.35 terabits per second. This incident highlighted the crucial role of rate limiting in preventing such traffic surges from crippling systems. In this article , we’ll explore various rate-limiting algorithms that can help safeguard services from excessive traffic.
1. Fixed Window?Counter:
lets say you can send maximum 10 API request per minute in a particular server. so how actually those request will be process by fixed window counter algorithm? basically It divides time into fixed windows and it counts the number of requests in each window. lets try to understand the scenario.
steps:?
Pseudocode:?
Initialize counter = 10
Start a timer for 1 minute
When request comes in:
If counter > 0:
Process the request
Decrement counter by 1
Else:
Reject the request
After 1 minute:
Reset counter to 10
Restart the timer
2. Sliding Window?Log
You can make 10 API requests per minute, but instead of fixed windows, it tracks requests over a rolling time frame.
Steps:
Pseudocode:
Initialize empty log for storing request timestamps
When request comes in:
Remove any request from log older than 60 seconds
If log has less than 10 requests in last 60 seconds:
Process the request
Add current timestamp to the log
Else:
Reject the request
3. Sliding Window?Counter
Sliding window counter is almost similar with sliding window log but instead of logging each request, it uses counters. lets explain the same example here also.?
Steps:
领英推荐
Pseudocode:
Initialize small intervals (for example, every 6 seconds) with request counters
Total request_count = 0
When request comes in:
Remove any counts older than 60 seconds
If request_count < 10:
Process the request
Add 1 to the current interval’s counter
Update total request_count
Else:
Reject the request
4. Token?Bucket
In this algorithm requests consume tokens. If the bucket does not contain any tokens, new requests are denied. You can make 10 API requests per minute, and tokens are used to allow requests. lets see the explaination for this.?
Steps:
Pseudocode:
Initialize bucket with 10 tokens
Start a refill timer (e.g., 1 token every 6 seconds)
When request comes in:
If bucket has tokens:
Process the request
Remove 1 token from the bucket
Else:
Reject the request
At every 6-second interval:
Add 1 token to the bucket (up to 10 tokens max)
5. Leaky?Bucket
In this Algorithm Requests are added to a queue (bucket) and processed at a fixed rate to avoid traffic spikes. so in our example we can use 10 API requests per minute, but instead of tokens, requests are queued and processed at a constant rate.
Steps:
Pseudocode:
Initialize empty queue for requests
When request comes in:
If queue is not full:
Add request to queue
Else:
Reject the request
At every fixed interval (e.g., 1 request per 6 seconds):
Process the first request from the queue (if any)
I’d love to hear your thoughts on this article! If you have any suggestions or notice areas for improvement, feel free to share. Your feedback will help me improve the content for future readers.