Because the official account has changed the push rules, please click “Looking” and add “Star” to get exciting technology sharing as soon as possible
Click to follow #Internet Architect Public Account, and get a full set of architect information here
0,2T architect learning materials dry goods points
Previous: ChatGPT research framework (80-page PPT, with download)
Hello, I’m an Internet Architect!
ChatGpt realizes SMS sending Redis current limiting function
The content and source code of this article are all from ChatGpt, please feel the power of ChatGpt, it is better than what I wrote!
Redis current limiting usage
Redis can be used to implement current limiting, among which the current limiting method based on the token bucket algorithm is more commonly used. The following is an implementation example of Redis-based token bucket current limiting:
-
First, you need to set up an ordered collection in Redis to store the token information of the token bucket, such as the number of tokens, the expiration time of tokens, etc.
-
When there is a request for current limiting, first obtain the number of tokens in the token bucket from Redis, and if the number of tokens is insufficient, return the rate limiting failure.
-
If the token quantity is sufficient, consume a token from the token bucket, and update the token quantity and expiration time in the token bucket.
-
Return current limiting success.
Here is an example Python-based implementation:
import redis import time class RedisRateLimiter: def __init__(self, redis_conn, key, capacity, rate, default_timeout): self.redis_conn = redis_conn self.key = key self. capacity = capacity self.rate = rate self.default_timeout = default_timeout def is_allowed(self): # Get the number of tokens and expiration time in the current token bucket count, timestamp = self.redis_conn.zscore(self.key, 'tokens'), self.redis_conn.zscore(self.key, 'timestamp') if count is None or timestamp is None: # Create a new token bucket if it does not exist self.redis_conn.zadd(self.key, { 'tokens': self.capacity, 'timestamp': time.time() }) count, timestamp = self. capacity, time. time() # Calculate how many tokens there should be delta = time.time() - timestamp new_tokens = delta * self.rate if new_tokens > 0: # Update the number of tokens and expiration time in the token bucket self.redis_conn.zadd(self.key, { 'tokens': min(count + new_tokens, self.capacity), 'timestamp': time.time() }) # Determine whether the number of tokens is sufficient count = self.redis_conn.zscore(self.key, 'tokens') if count is None: return False elif count > 0: # Consume a token self.redis_conn.zadd(self.key, { 'tokens': count - 1 }) return True else: return False
In this implementation, redis_conn
is the Redis
connection object, key is the key name of the ordered collection storing token bucket information, and capacity
is the token Bucket capacity, rate is the number of tokens generated per second, default_timeout
is the default expiration time of the token bucket. The is_allowed()
method is used to determine whether the current request is allowed to pass. If it is allowed to pass, it returns True
, otherwise it returns False
.
Java-based Redis current limiting usage
Ok, here is an example of Java-based Redis throttling usage:
-
First, you need to add Redis’s Java client dependencies, such as Jedis or Lettuce. Taking Jedis as an example, the following dependencies can be added to the Maven project:
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.6.3</version> </dependency>
-
Implement the current limiting logic of the token bucket algorithm in Java code. The following is an implementation example of token bucket current limiting based on Jedis:
import redis.clients.jedis.Jedis; public class RedisRateLimiter { private Jedis jedis; private String key; private int capacity; private double rate; private int defaultTimeout; public RedisRateLimiter(Jedis jedis, String key, int capacity, double rate, int defaultTimeout) { this.jedis = jedis; this.key = key; this. capacity = capacity; this.rate = rate; this.defaultTimeout = defaultTimeout; } public boolean isAllowed() { // Get the number of tokens and expiration time in the current token bucket Double count = jedis.zscore(key, "tokens"); Double timestamp = jedis.zscore(key, "timestamp"); if (count == null || timestamp == null) { // If the token bucket does not exist, create a new token bucket jedis.zadd(key, timestamp, String.valueOf(System.currentTimeMillis() / 1000)); jedis.zadd(key, count, String.valueOf(capacity)); count = Double. valueOf(capacity); timestamp = Double.valueOf(System.currentTimeMillis() / 1000); } // Calculate how many tokens there should be double delta = System.currentTimeMillis() / 1000 - timestamp; double newTokens = delta * rate; if (newTokens > 0) { // Update the token quantity and expiration time in the token bucket jedis.zadd(key, timestamp + delta, String.valueOf(System.currentTimeMillis() / 1000)); jedis.zadd(key, count + newTokens, String.valueOf(capacity)); } // Determine whether the number of tokens is sufficient count = jedis.zscore(key, "tokens"); if (count == null) { return false; } else if (count > 0) { // consume a token jedis.zadd(key, count - 1, "tokens"); return true; } else { return false; } } }
In this implementation, jedis
is the Jedis
client instance, key
is the key name of the ordered collection storing token bucket information, capacity
is the capacity of the token bucket, rate
is the number of tokens generated per second, and defaultTimeout
is the default expiration time of the token bucket. The isAllowed()
method is used to determine whether the current request is allowed to pass. If it is allowed to pass, it returns true
, otherwise it returns false
.
Based on the above method, when the SMS is sent, the current is limited. Limit the occurrence frequency of each number and limit the system processing capacity per minute.
What are the disadvantages of this current limiting algorithm
The main disadvantages of this current limiting algorithm (token bucket algorithm) are as follows:
-
It is not very friendly to the processing of sudden traffic in a short period of time. Since the token bucket algorithm generates tokens at a certain rate, when the request traffic exceeds this rate, there will not be enough tokens in the token bucket, and the request will be limited. If a large number of requests arrive at the same time in a short period of time, many requests will be throttled, and the tokens in the token bucket are not enough to deal with this situation.
-
The token bucket algorithm needs to constantly update the token quantity and expiration time in the token bucket. In this way, frequent write operations need to be sent to the Redis server, which increases the burden on the system, and may cause performance problems under high concurrency conditions.
-
If the Redis server fails, the number of tokens in the token bucket and the expiration time may be inaccurate, thus affecting the current limiting effect.
-
The token bucket algorithm can only judge whether the request is allowed to pass during current limiting, but cannot count the specific number of requests. If you need to count the number of requests, you need to combine other algorithms, such as counter algorithms.
To sum up, in practical application of the token bucket algorithm, it is necessary to select an appropriate current limiting algorithm according to the specific scenario, so as to avoid the adverse effects caused by the above shortcomings.
1.Alibaba Open Source Intranet High Concurrency Programming Manual.pdf
2、2T Architect learning materials dry goods sharing
3.10000 + TB resources, Ali cloud disk, awesome! !
4, basically covers all the core knowledge points of Spring
· END ·
Finally, pay attention to the official account Internet Architect, and reply in the background: 2T, you can get the Java series interview questions and answers I compiled, which are very complete.
If this article is helpful or inspiring to you, please help scan the QR code above and pay attention. Your support is the biggest motivation for me to keep writing.
Ask for one-click three consecutive likes, reposts, and watching.