Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

There are several ways to realize current limit in redis.

2025-01-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

How many ways can redis achieve current limit? In view of this problem, this article introduces the corresponding analysis and answers in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

There are many ways to achieve current limit, Redis has a very powerful function, I use Redis to practice three ways of implementation, can be relatively simple to achieve. Redis can not only do current limit, but also do data statistics, nearby people and other functions.

The first: the operation of setnx based on Redis

When we use Redis's distributed lock, we all know that we rely on the instructions of setnx. During the operation of CAS (Compare and swap), we also set the expiration time (expire) for the specified key. The main purpose of our current limit is to have and only N requests per unit time to access my code program. So relying on setnx can easily do this function.

For example, if we need to limit 20 requests within 10 seconds, we can set the expiration time 10 when we setnx. When the number of setnx requests reaches 20, the current limit will be achieved.

Of course, this approach has many disadvantages. For example, when counting 1-10 seconds, it is impossible to count within 2-11 seconds. If we need to count M requests within N seconds, then we need to keep N key in our Redis and so on.

The second kind: Redis-based data structure zset

In fact, the most important thing involved in current restriction is the sliding window, which also mentions how 1-10 becomes 2-11. In fact, both the starting value and the end value are + 1.

And we can easily implement this function if we use the list data structure of Redis.

We can make the request into an zset array. When each request comes in, the value remains unique and can be generated by UUID, while the score can be represented by the current timestamp, because score can be used to calculate the number of requests within the current timestamp.

And the zset data structure also provides a range method that allows us to easily get the number of requests in two timestamps.

Third: token bucket algorithm based on Redis

When it comes to current limitation, we have to mention the token bucket algorithm. Token bucket algorithm is also called bucket algorithm, which can be explained by du Niang.

The token bucket algorithm refers to the input rate and the output rate. When the output rate is greater than the input rate, then the traffic limit is exceeded.

In other words, each time we visit a request, we can get a token from Redis. If we get a token, it means we are within the limit, while if we do not get it, the result is the opposite.

With the above ideas, we can easily do this code in combination with Redis's List data structure.

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 ("no token in the current token bucket");} return Response.ok (articleDescription2);}

Then rely on the scheduled task of Java to regularly rightPush tokens in List. Of course, tokens also need to be unique, so I still use UUID to generate them here.

/ / add UUID to the token bucket at the rate of / / 10s, only to ensure the uniqueness @ Scheduled (fixedDelay = 10000token initialDelay = 0) public void setIntervalTimeTask () {redisTemplate.opsForList () .rightPush ("limit_list", UUID.randomUUID () .toString ());}

To sum up, it is not very difficult to implement the code at the beginning. For these current-limiting methods, we can add the above code to AOP or filter to limit the flow of the interface and ultimately protect your website.

This is the answer to several questions about how to achieve current restriction in redis. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about 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: 281

*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.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report