当前位置:网站首页>Three ways for redis to realize current limiting
Three ways for redis to realize current limiting
2022-07-01 19:12:00 【Floating across the sea】
Redis Three ways to realize current limiting
The first one is : be based on Redis Of setnx The operation of
We are using Redis When using distributed locks , We all know it depends on setnx Instructions , stay CAS(Compare and swap) During the operation of , At the same time, give the designated key Set up expired practices (expire), Our main purpose in limiting current is to , Yes and no N The number of requests that can access my code program . So rely on setnx You can easily do this function .
For example, we need to be in 10 Seconds limit 20 A request , So we're in setnx You can set the expiration time when 10, When requested setnx The number of 20 Then the current limiting effect is achieved . The code is relatively simple, so I won't show it .
Of course, there are many disadvantages of this approach , For example, when Statistics 1-10 seconds , Unable to statistics 2-11 In seconds , If you need statistics N Seconds M A request , So our Redis You need to keep N individual key Problems, etc.
The second kind : be based on Redis Data structure of zset
In fact, the most important part of current limiting is sliding window , As mentioned above 1-10 How to become 2-11. In fact, the starting value and the end value are respectively +1 that will do .
And if we use Redis Of list The data structure can easily realize this function
We can make the request into a zset Array , When every request comes in ,value Keep it unique , It can be used UUID Generate , and score Can be represented by the current timestamp , because score We can use it to calculate the number of requests within the current timestamp . and zset Data structures also provide range Methods make it easy for us to get 2 How many requests are in timestamps
The code is as follows
public Response limitFlow(){
Long currentTime = new Date().getTime();
System.out.println(currentTime);
if(redisTemplate.hasKey("limit")) {
Integer count = redisTemplate.opsForZSet().rangeByScore("limit", currentTime - intervalTime, currentTime).size(); // intervalTime Is the time of current limiting
System.out.println(count);
if (count != null && count > 5) {
return Response.ok(" At most, you can only access 5 Time ");
}
}
redisTemplate.opsForZSet().add("limit",UUID.randomUUID().toString(),currentTime);
return Response.ok(" Successful visit ");
}
The third kind of : be based on Redis The token bucket algorithm of
When it comes to current limiting, we have to mention the token bucket algorithm . For details, please refer to Du Niang's explanation [ Token bucket algorithm ][Link 1]
The token bucket algorithm refers to input rate and output rate , When the output rate is greater than the input rate , Then the flow limit is exceeded .
That is, every time we visit a request , It can be downloaded from Redis Get a token from , If you get the token , That means the limit is not exceeded , And if you can't get it , The result is the opposite .
Rely on the above ideas , We can combine Redis Of List Data structure can easily do such code , It's just a simple implementation
rely on List Of leftPop To get the token
// Output token
public Response limitFlow2(Long id){
Object result = redisTemplate.opsForList().leftPop("limit_list");
if(result == null){
return Response.ok(" There is no token in the current token bucket ");
}
return Response.ok(articleDescription2);
}
Rely on Java Scheduled tasks for , Go regularly List in rightPush token , Of course, tokens also need uniqueness , So I still use UUID Generated
// 10S Add... To token bucket at the rate of UUID, Just to guarantee uniqueness
@Scheduled(fixedDelay = 10_000,initialDelay = 0)
public void setIntervalTimeTask(){
redisTemplate.opsForList().rightPush("limit_list",UUID.randomUUID().toString());
}
边栏推荐
- AI training speed breaks Moore's law; Song shuran's team won the RSS 2022 Best Paper Award
- Go语言自学系列 | go语言数据类型
- Leetcode-141 circular linked list
- 【AGC】如何解决事件分析数据本地和AGC面板中显示不一致的问题?
- 市值蒸发740亿,这位大佬转身杀入预制菜
- Navicat Premium 15 永久破解和2021版本最新IDEA破解(亲测有效)
- 解决方案:可以ping别人,但是别人不能ping我
- docker 部署mysql8.0
- 3. "Create your own NFT collections and publish a Web3 application to show them" cast NFT locally
- Prices of Apple products rose across the board in Japan, with iphone13 up 19%
猜你喜欢

Lake Shore—OptiMag 超导磁体系统 — OM 系列

实例讲解将Graph Explorer搬上JupyterLab

2. Create your own NFT collections and publish a Web3 application to show them start and run your local environment

生鲜行业B2B电商平台解决方案,提高企业交易流程标准化和透明度

AI training speed breaks Moore's law; Song shuran's team won the RSS 2022 Best Paper Award

Basic knowledge and commands of disk

Navicat premium 15 permanent cracking and 2021 latest idea cracking (valid for personal testing)

Mise en place d'une plate - forme générale de surveillance et d'alarme, quelles sont les conceptions nécessaires dans l'architecture?

微服务大行其道的今天,Service Mesh是怎样一种存在?

Love business in Little Red Book
随机推荐
Li Kou daily question - Day 32 -1232 Dotted line
宏观视角看抖音全生态
2020,最新手机号码手机验证正则表达式,持续更新
苹果产品在日本全面涨价,iPhone13涨19%
Three. JS learning - basic operation of camera (learn from)
Leetcode-83 delete duplicate elements in the sorting linked list
Privacy sandbox is finally coming
The best landing practice of cave state in an Internet ⽹⾦ financial technology enterprise
【AGC】如何解决事件分析数据本地和AGC面板中显示不一致的问题?
js找出数字在数组中下一个相邻的元素
Lumiprobe lumizol RNA extraction reagent solution
华为联机对战服务玩家掉线重连案例总结
The market value evaporated by 74billion yuan, and the big man turned and entered the prefabricated vegetables
斯坦福、Salesforce|MaskViT:蒙面视觉预训练用于视频预测
Solution: you can ping others, but others can't ping me
2020, the regular expression for mobile phone verification of the latest mobile phone number is continuously updated
app发版后的缓存问题
ETL development of data warehouse (IV)
毕业季 | 华为专家亲授面试秘诀:如何拿到大厂高薪offer?
Basic knowledge and commands of disk