Rate limiter is used to control the rate of requests sent or received by our system. Many services like Tiny URL’s, Twitter API’s, Facebook API’s use rate limiters. Other term for rate limiting is throttling.
Types of throttling
Prerequisites:
System design introduction : 3 principles of distributed system, 5 step guide for System design.
System design concepts & components : Horizontal scaling
Functional requirements
Non-functional requirements
boolean isRequestAllowed(params)
The user or entity accessing the apis should have some unique id against which we will apply rate limiter. We can keep the rate or count related data in-memory.
Problem statement:
Calculate the number of requests the user sends per minute.
Solution:
There are various Algorithms which perform similar functionality.
Let us revisit our non-functional requirements
1. System should be highly available since it protects our system which can be achieved by no single point of failure principle.
2. Rate limiter should not slow down the api requests. This can be achieved by no bottleneck principle.
Capacity to process the sliding window algorithm?
Consider with the counter and the array it is 10KB per user so for 1 million users the capacity needed would be 10KB * 1 million = 10KB * 1000000 = 1000000 KB = 10000MB = 10GB which can easily be handled by a single server.
How to synchronize servers?