Rate Limiter
Here is a more sophisticated discussion.
- General Goal
- Limit the number of actions within a period of time
- Open Source: Ruby / Django. Memcached.
- Scenario
- Feature: IP / User / Email
- Limite Login: IP & Email
- Buy Ticket: IP & User
- Criteria:
- Time period (minutes, hours, days)
- Feature: IP / User / Email
- Storage
- Requirement
- Need to log feature (IP/User/Email)/ time / event (Login/Buy Ticket, etc). The number of times.
- No need to keep history above the range.
- Need high efficient read/write access. (In-memory).
- No need to store the data permanently. (In-memory).
- Design
- Use feature + timestamp + event as key
- Use Memcached increment to record access. (Access + 1. TTL = 60s.)
- Use a ring buffer fashion.
- Accumulate the sum of access within the period.
- Trade-Off – Bucket
- When using the day as a limitation period, use the hour bucket.
- When using the hour as a limitation period, use the minute bucket.
- Etc.
- Requirement
DataDog
- Scenario
- Record access to a specific link within a period of time. Form a curve
- Storage
- Most write. Less read.
- Need persistent storage. (cannot in-memory)
- SQL / NoSQL / FS: All acceptable. Take NoSQL as an example.
- Store the historical data in the unit of times.
- Today, data points per minute.
- Lastday, data points per 5 minutes.
- Last month, data points per hour.
- Etc.
- Optimization:
- Aggregation: Reduce QPS
- Buffer the information at the webserver locally. Sync the information to the Datadog server periodically. (For example, per 15 seconds)
- Reduce QPS.
- Overload depends on the number of web servers. Not the number of users.
- Buffer the information at the webserver locally. Sync the information to the Datadog server periodically. (For example, per 15 seconds)
- Retention: Reduce data size
- Change the data statistics to coarse granularity.
- Aggregation: Reduce QPS
Leave a comment