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

How redis restricts the number of IP visits

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

Sometimes we need to limit the frequency of visits to an api or page, such as how many times a single ip or a single user can visit in a minute. Requirements like this can easily be implemented in Redis. So how does redis limit the number of IP visits? Let's take a look at it with the editor.

Strategy 1:

Save a count value (int) in redis, key is the number of times user:$ip,value is accessed by that ip, and the first time you set key, set expires.

Before count plus 1, determine whether the key exists. If not, there are two situations: 1, the ip has not been accessed; 2, the ip has been visited, but the key has expired. Then you need to set up expires again at this time.

If the user visits, determine whether the value of count is greater than the upper limit, if it is lower than the upper limit, the request will be processed, otherwise the request will be rejected.

Strategy 2:

Consider this situation, assuming that the user is only allowed 100 visits in 60 seconds, if one user visits once in the first second, 99 times in the 59th second, and then 100 times in the 61st second.

If it is handled according to policy 1, 100 requests are accepted between 1 and 60 seconds, and 100 requests are received in 61 seconds, so the request for the ip is no longer processed during the period of 62-120.

It seems like no problem, but if you think about it, the request for 99,100,199 was accepted between 59 and 61 seconds, with an interval of only 3 seconds. In that case, there is a problem with the original design.

Solution: you can use redis's list (two-way queue) data structure. Key is user:$ip, that is, each ip sets a two-way queue. Each time the request arrives, make the following judgment:

1. If the number of elements in the list is less than 100, the timestamp of the arrival of the request is Lpush to the list.

2. If there are more than 100 elements in the list, the Lindex (- 1) is taken out, that is, the earliest timestamp of the rightmost request. If the difference between the earliest timestamp and the current timestamp is more than 60 seconds, the first request has expired and the first request will be dequeued from the Rpop. Then stamp the current time into the team Lpush.

The above is a brief introduction to how redis restricts the number of IP visits. Of course, you have to use the above differences in detail before you understand it. If you want to know more, welcome to follow the industry information channel!

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.

Share To

Database

Wechat

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

12
Report