Design Rate Limiter
Scenario
- 根据IP来划分一些网络行为
- 注册 登入 POST
Service
- 就是一个简单的check访问数量的服务
Storage
- 因为要快速反应,所以一般就是cache
算法
用Event + IP + timestamp 作为key,访问次数作为value
如果有一次访问,就在对应的bucker +1
可以做分级储存,秒,分,小时 分别做bucket
Assuming there is a user request at “08:30”, then we just check whether the request number is exceed the limitation in “[08:00, 09:00]”.
Optimize the Resources Usage
If we put all the user request information to the main memory, sometimes it needs a lot of space that we can't afford.
Here we use the LRU (Least Recent Use) strategy to move some less likely malicious user from the cache to storage them in the hard disk (or SSD) which is larger, slower and cheaper. And we aggregate the information in the cache to reduce the visit time of database which is slow.