当前位置:网站首页>Redis + Lua implementation of distributed interface current limiting
Redis + Lua implementation of distributed interface current limiting
2022-06-25 00:45:00 【Bulst】
Preface
redis + lua Script has become the mainstream current limiting scheme for individual projects .
redis With its characteristics, it has become the leader of middleware , The latest official test data :
- The speed of reading is zero 110000 Time /s
- The speed of writing is 81000 Time /s.
lua:
- Reduce network overhead : Use Lua Script , No need to Redis Send multiple requests , Only once , Reduce network transmission
- Atomic manipulation :Redis Will the whole Lua The script executes as a command , atom , Don't worry about concurrency
- Reuse :Lua Once the script is executed , It will be kept forever Redis in ,, Other clients can be reused
operation
Add the following comments at the interface where current restriction is required (@RedisLimit), On the basis of the original , Current limiting can be achieved without adding any dependencies .
@RedisLimit(name = " Order spike ", prefix = "seckill", key = "distributed", count = 1, period = 1, limitType = LimitType.IP, msg = " There are many people waiting in line at present , Please try again later !")
@GetMapping("/limit/distributed/{id}")
public ResponseEntity<Object> limitDistributed(@PathVariable("id") String id) {
return ResponseEntity.ok(" Successfully purchased :" + id + " individual ");
}
Introduce
/** * Resource name */
String name() default "";
/** * Prefix */
String prefix() default "";
/** * resources key */
String key() default "";
/** * Maximum visits */
int count();
/** * Time , Second level */
int period();
/** * type */
LimitType limitType() default LimitType.CUSTOMER;
/** * Prompt information */
String msg() default " The system is busy , Please try again later ";
function
- The default is based on the global interface QPS As a flow control indicator
- Independent IP Switch freely with the global interface
- You can customize the time and within the specified time QPS
- According to the key Value for later data monitoring and statistics
principle
Take the flow as the cut-off point 、 Sliding time window as the core algorithm .
lua Script to ensure its atomic operation
Core code ( Part of the show )
lua Script
redis.replicate_commands();
local listLen,time
listLen = redis.call('LLEN', KEYS[1])
if listLen and tonumber(listLen) < tonumber(ARGV[1]) then
local a = redis.call('TIME');
redis.call('LPUSH', KEYS[1], a[1]*1000000+a[2])
else
time = redis.call('LINDEX', KEYS[1], -1)
local a = redis.call('TIME');
if a[1]*1000000+a[2] - time < tonumber(ARGV[2])*1000000 then
return 0;
else
redis.call('LPUSH', KEYS[1], a[1]*1000000+a[2])
redis.call('LTRIM', KEYS[1], 0, tonumber(ARGV[1])-1)
end
end
return 1;
Tangent point processing
Long number = redisTemplate.execute(SECKILL_SCRIPT, keys, count, period);
if(number != null && number.intValue() == 1){
return pjp.proceed();
}
边栏推荐
- Thingsboard - rest API obtains and refreshes tokens
- Uniapp encapsulated incentive advertisement, screen insert advertisement and banner advertisement
- Some examples of MgO operating database in go
- Virtual machine - network configuration
- Domain Driven Design and coding
- A website for programmers with a monthly salary of 30K
- Infotnews | is the development of domestic NFT limited to digital collections?
- Paper review: U2 net, u-net composed of u-net
- 移动安全工具-apktool
- The acceleration of 100 km is only 5.92 seconds, and the willanda high-performance version leads with the strength of high-energy products
猜你喜欢

Single blind box removal, social blind box and friend blind box program source code
Design and practice of vivo server monitoring architecture

Interesting checkbox counters
Microsoft won the title of "leader" in the magic quadrant of Gartner industrial Internet of things platform again!

在企业级开发过程中我发现有位同事用select * from where 条件 for update
5-minute NLP: summary of 3 pre training libraries for rapid realization of NER

Use of JMeter

Databinding quick start (still using findviewbyid?)
![[interview question] what is a transaction? What are dirty reads, unrepeatable reads, phantom reads, and how to deal with several transaction isolation levels of MySQL](/img/95/02a58c9dc97bd8347b43247e38357d.png)
[interview question] what is a transaction? What are dirty reads, unrepeatable reads, phantom reads, and how to deal with several transaction isolation levels of MySQL

Working principle analysis of kubernetes architecture core components
随机推荐
Design and practice of vivo server monitoring architecture
VNC viewer remote connection raspberry pie without display
软件测试与游戏测试文章合集录
Transition from digitalization to intelligent manufacturing
Discrete mathematics and its application detailed explanation of exercises in the final exam of spring and summer semester of 2018-2019 academic year
【Redis实现秒杀业务④】一人一单,不可重复购买
Wallpaper applet wechat applet
[interview question] what is a transaction? What are dirty reads, unrepeatable reads, phantom reads, and how to deal with several transaction isolation levels of MySQL
【Redis实现秒杀业务③】超卖问题之乐观锁具体实现
Outer screen and widescreen wasted? Harmonyos folding screen design specification teaches you to use it
移动安全工具-apktool
How to reduce the font size of custom controls (optimize the round dot progress bar)
Go crawler framework -colly actual combat (III) -- panoramic cartoon picture capture and download
D omit parameter name
无人驾驶: 对多传感器融合的一些思考
Xcode preview displays a bug in the content of the list view and its solution
不重要的token可以提前停止计算!英伟达提出自适应token的高效视觉Transformer网络A-ViT,提高模型的吞吐量!...
More pictures | explain the Nacos parameters in detail!
The problem of multiple callback of video ads stimulated by applets (offcolse problem)
Source code analysis the problem that fragments cannot be displayed in the custom ViewGroup