In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the implementation methods of Redis current limiting". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "what are the implementation methods of Redis current limiting"!
1. Simple current limiting
basic principle
How to organize unplanned requests to stress the system when it has limited capacity. First let's look at some simple current-limiting strategies to prevent violent attacks. For example, if you want to access IP, you can only access it 10 times without 5s, and intercept more than that.
As shown above, sliding windows are generally used to count the number of visits within an interval time.
Use zset to record IP access times. Each IP is saved by key. Score saves the current timestamp. Value is uniquely implemented by timestamp or UUID.
code implementation
public class RedisLimiterTest { private Jedis jedis; public RedisLimiterTest(Jedis jedis) { this.jedis = jedis; } /** * @param ipAddress Ip Address * @param period within a specified period of time, in seconds * @param maxCount Maximum number of times allowed * @return */ public boolean isIpLimit(String ipAddress, int period, int maxCount) { String key = String.format("ip:%s", ipAddress); //millisecond timestamp long currentTimeMillis = System.currentTimeMillis(); Pipeline pipe = jedis.pipelined(); // redis transaction, guarantee atomicity pipe.multi(); //store data, value and score both use millisecond timestamps pipe.zadd(key, currentTimeMillis, "" + UUID.randomUUID()); //remove all elements of window interval pipe.zremrangeByScore(key, 0, currentTimeMillis - period * 1000); //Get the number of behaviors in the time window Response count = pipe.zcard(key); //Set zset expiration time to prevent cold users from occupying memory continuously. Allow 1s here. pipe.expire(key, period + 1); //Submit a transaction pipe.exec(); pipe.close(); //Compare whether the quantity exceeds the standard return count.get() > maxCount; } public static void main(String[] args) { Jedis jedis = new Jedis("localhost", 6379); RedisLimiterTest limiter = new RedisLimiterTest(jedis); for (int i = 1; i this.capacity) { //the remaining space must not be higher than the capacity this.leftQuota = this.capacity; } } boolean watering(int quota) { makeSpace(); if (this.leftQuota >= quota) { //Determine whether the remaining space is sufficient this.leftQuota -= quota; return true; } return false; } } //all funnels private Map funnels = new HashMap(); /** * @param capacity funnel capacity * @param leakingRate quota/s */ public boolean isIpLimit(String ipAddress, int capacity, float leakingRate) { String key = String.format("ip:%s", ipAddress); Funnel funnel = funnels.get(key); if (funnel == null) { funnel = new Funnel(capacity, leakingRate); funnels.put(key, funnel); } return ! funnel.watering(1); //requires 1 quota } public static void main(String[] args) throws Exception{ FunnelLimiterTest limiter = new FunnelLimiterTest(); for (int i = 1; i cl.throttle key:xxx 15 30 60 1
15 : 15 capacity This is funnel capacity
30 60 : 30 operations / 60 seconds This is the leakage rate
1 : need 1 quota (optional, default is also 1)
> cl.throttle laoqian:reply 15 30 601) (integer) 0 # 0 indicates permission, 1 indicates rejection 2) (integer) 15 #hopper capacity3) (integer) 14 #hopper remaining space left_quota4) (integer) -1 #if rejected, how long does it take to try again (hopper has space, unit seconds)5) (integer) 2 #How long after the funnel is completely empty (left_quota==capacity, in seconds)
If the current-limiting instruction is rejected, it needs to be discarded or retried. cl.throttle instruction is very thoughtful, even the retry time is calculated for you, directly take the fourth value of the return result array to sleep, if you don't want to block the thread, you can also asynchronously schedule the task to retry.
At this point, I believe that everyone has a deeper understanding of "what are the implementation methods of Redis current limiting". Let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!
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.