In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article is about how Redis current limiting is implemented. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.
In the face of more and more high-concurrency scenarios, current-limiting display is particularly important.
Of course, there are many ways to achieve current limiting. Redis has very powerful functions. I have used Redis to implement three ways, which can be implemented in a relatively simple way. Redis can not only do current limiting, but also do data statistics, nearby people and other functions, which may be written later.
The first type: setnx operation based on Redis
When we use Redis distributed locks, we all know that we rely on setnx instructions. When CAS (Compare and swap) operates, we also set the expiration practice for the specified key. Our main purpose in current limiting is to have N and only N requests to access my code program in unit time. This is why setnx makes it easy to do this.
For example, we need to limit 20 requests in 10 seconds, so we can set the expiration time of 10 in setnx, and when the number of setnx requests reaches 20, the current limiting effect is achieved. Code is relatively simple not to do the show.
For the specific usage of setnx, please refer to my other blog, Redis Template, a series of problems caused by Redis distributed locks.
Of course, there are many disadvantages to this approach. For example, when counting 1-10 seconds, it is impossible to count 2-11 seconds. If we need to count M requests within N seconds, then we need to keep N keys in Redis.
The second type: Redis-based data structure zset
In fact, the most important thing involved in current limiting is the sliding window. It is also mentioned above how 1-10 becomes 2-11. In fact, both the starting value and the end value can be +1.
And if we use Redis list data structure can easily achieve this function
We can construct requests as a zset array, where value remains unique for each incoming request and can be generated using UUID, while score can be represented by the current timestamp, because score can be used to calculate how many requests there are within the current timestamp. The zset data structure also provides a range method that allows us to easily get how many requests there are in 2 timestamps
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("Access up to 5 times per minute"); } } redisTemplate.opsForZSet().add("limit",UUID.randomUUID().toString(),currentTime); return Response.ok("Access succeeded"); }
The above code can achieve the effect of sliding window, and can guarantee at most M requests every N seconds, the disadvantage is that the zset data structure will be larger and larger. The implementation method is relatively simple.
Third: token bucket algorithm based on Redis
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.
Token bucket algorithm refers to the input rate and output rate, when the output rate is greater than the input rate, then the traffic limit is exceeded.
That is to say, every time we visit a request, we can get a token from Redis. If we get a token, it means that we have not exceeded the limit, and if we don't get it, the result is the opposite.
Relying on the above ideas, we can combine Redis List data structure very easily to do such code, just a simple implementation
Rely on List's leftPop to get tokens
//output token public Response limitFlow2(Long id){ Object result = redisTemplate.opsForList().leftPop("limit_list"); if(result == null){ return Response.ok("There are no tokens in the current token bucket"); } return Response.ok(articleDescription2); }
Then rely on Java's timed task, timed to the List rightPush token, of course, the token also needs to be unique, so I still use UUID to generate
//Add UUID to token bucket at rate of 10S only to ensure uniqueness @Scheduled(fixedDelay = 10_000,initialDelay = 0) public void setIntervalTimeTask(){ redisTemplate.opsForList().rightPush("limit_list",UUID.randomUUID().toString()); }
In summary, the code implementation is not difficult at the beginning, for these current limiting methods we can add the above code in AOP or filter, used to achieve the current limit of the interface, and ultimately protect your website.
Redis actually has many other uses, and its role is not just caching, distributed locking role. His data structures are not just String, Hash, List, Set, Zset. Interested parties can learn about his GeoHash algorithm later;BitMap, HLL and Bloom filter data (added after Redis 4.0, you can install redislabs/rebloom directly with Docker) structure.
Thank you for reading! About "Redis current limit how to achieve" this article is shared here, I hope the above content can have some help to everyone, so that everyone can learn more knowledge, if you think the article is good, you can share it to let more people see it!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.