A brief discussion on three implementation methods of Redis current limiting
This article will introduce to you three ways to implement current limiting in Redis. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
[Related recommendations: Redis video tutorial]
In the face of more and more high-concurrency scenarios, the current limit display Particularly important.
Of course, there are many ways to implement current limiting. Redis has very powerful functions. I have used Redis to practice three implementation methods, which can be implemented in a relatively simple way. Redis can not only do current limiting, but also can do data statistics, nearby people and other functions, which may be written later.
The first one: Redis-based setnx operation
When we use Redis's distributed lock, everyone knows that it relies on With the setnx instruction, during the CAS (Compare and swap) operation, the expiration practice (expire) is also set for the specified key. Our main purpose of limiting the current is to have and only N quantity within the unit time. request to be able to access my code program. So relying on setnx can easily achieve this function.
For example, if we need to limit 20 requests within 10 seconds, then we can set the expiration time to 10 during setnx. When the number of requested setnx reaches 20, the current limiting effect will be achieved. The code is relatively simple and will not be shown.
For the specific usage of setnx, please refer to my other blog A series of problems caused by Redis distributed lock under RedisTemplate
Of course, there are many disadvantages to this approach, such as when counting 1-10 seconds , it is impossible to count the M requests within 2-11 seconds. If we need to count M requests within N seconds, then we need to keep N keys in Redis and other issues
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, the starting value and the end value are both 1.
And if we use the list data structure of Redis, we can easily implement this function
We can make the request into a zset array. When each request comes in, the value remains unique. Generated with UUID, and score can be represented by the current timestamp, because score can be used to calculate the number of requests within the current timestamp. The zset data structure also provides the range method so that we can easily get the number of requests within 2 timestamps
The 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是限流的时间 System.out.println(count); if (count != null && count > 5) { return Response.ok("每分钟最多只能访问5次"); } } redisTemplate.opsForZSet().add("limit",UUID.randomUUID().toString(),currentTime); return Response.ok("访问成功"); }
The above code can achieve the effect of sliding windows , and can guarantee at most M requests every N seconds. The disadvantage is that the data structure of zset will become larger and larger. The implementation method is relatively simple.
The third type: Redis-based token bucket algorithm
When it comes to current limiting, I have to mention it Token bucket algorithm. For details, please refer to Du Niang’s explanation Token Bucket Algorithm
The token bucket algorithm mentions input rate and output rate. When the output rate is greater than the input rate, the traffic limit is exceeded.
That is to say, every time we access a request, we can get a token from Redis. If we get the token, it means that the limit has not been exceeded. If we cannot get it, the result will be the opposite.
Relying on the above ideas, we can easily implement such code by combining the List data structure of Redis. It is just a simple implementation
Relying on the leftPop of the List to obtain the token
// 输出令牌 public Response limitFlow2(Long id){ Object result = redisTemplate.opsForList().leftPop("limit_list"); if(result == null){ return Response.ok("当前令牌桶中无令牌"); } return Response.ok(articleDescription2); }
Then rely on Java's scheduled tasks to rightPush the token into the List regularly. Of course, the token also needs to be unique, so I still use UUID to generate it.
// 10S的速率往令牌桶中添加UUID,只为保证唯一性 @Scheduled(fixedDelay = 10_000,initialDelay = 0) public void setIntervalTimeTask(){ redisTemplate.opsForList().rightPush("limit_list",UUID.randomUUID().toString()); }
In summary, the code implementation is not started at all. It's difficult. For these current limiting methods, we can add the above code to AOP or filter to limit the current flow of the interface and ultimately protect your website.
Redis actually has many other uses. Its role is not only caching and distributed locking. Its data structures are not just String, Hash, List, Set, and Zset. Those who are interested can follow up on his GeoHash algorithm; BitMap, HLL and Bloom filter data (added after Redis 4.0, you can use Docker to install redislabs/rebloom directly) structure.
For more programming-related knowledge, please visit: Programming Teaching! !
The above is the detailed content of A brief discussion on three implementation methods of Redis current limiting. For more information, please follow other related articles on the PHP Chinese website!

Redis is a memory data structure storage system, mainly used as a database, cache and message broker. Its core features include single-threaded model, I/O multiplexing, persistence mechanism, replication and clustering functions. Redis is commonly used in practical applications for caching, session storage, and message queues. It can significantly improve its performance by selecting the right data structure, using pipelines and transactions, and monitoring and tuning.

The main difference between Redis and SQL databases is that Redis is an in-memory database, suitable for high performance and flexibility requirements; SQL database is a relational database, suitable for complex queries and data consistency requirements. Specifically, 1) Redis provides high-speed data access and caching services, supports multiple data types, suitable for caching and real-time data processing; 2) SQL database manages data through a table structure, supports complex queries and transaction processing, and is suitable for scenarios such as e-commerce and financial systems that require data consistency.

Redisactsasbothadatastoreandaservice.1)Asadatastore,itusesin-memorystorageforfastoperations,supportingvariousdatastructureslikekey-valuepairsandsortedsets.2)Asaservice,itprovidesfunctionalitieslikepub/submessagingandLuascriptingforcomplexoperationsan

Compared with other databases, Redis has the following unique advantages: 1) extremely fast speed, and read and write operations are usually at the microsecond level; 2) supports rich data structures and operations; 3) flexible usage scenarios such as caches, counters and publish subscriptions. When choosing Redis or other databases, it depends on the specific needs and scenarios. Redis performs well in high-performance and low-latency applications.

Redis plays a key role in data storage and management, and has become the core of modern applications through its multiple data structures and persistence mechanisms. 1) Redis supports data structures such as strings, lists, collections, ordered collections and hash tables, and is suitable for cache and complex business logic. 2) Through two persistence methods, RDB and AOF, Redis ensures reliable storage and rapid recovery of data.

Redis is a NoSQL database suitable for efficient storage and access of large-scale data. 1.Redis is an open source memory data structure storage system that supports multiple data structures. 2. It provides extremely fast read and write speeds, suitable for caching, session management, etc. 3.Redis supports persistence and ensures data security through RDB and AOF. 4. Usage examples include basic key-value pair operations and advanced collection deduplication functions. 5. Common errors include connection problems, data type mismatch and memory overflow, so you need to pay attention to debugging. 6. Performance optimization suggestions include selecting the appropriate data structure and setting up memory elimination strategies.

The applications of Redis in the real world include: 1. As a cache system, accelerate database query, 2. To store the session data of web applications, 3. To implement real-time rankings, 4. To simplify message delivery as a message queue. Redis's versatility and high performance make it shine in these scenarios.

Redis stands out because of its high speed, versatility and rich data structure. 1) Redis supports data structures such as strings, lists, collections, hashs and ordered collections. 2) It stores data through memory and supports RDB and AOF persistence. 3) Starting from Redis 6.0, multi-threaded I/O operations have been introduced, which has improved performance in high concurrency scenarios.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
