当前位置:网站首页>Leader: who uses redis expired monitoring to close orders and get out of here!
Leader: who uses redis expired monitoring to close orders and get out of here!
2022-07-04 04:24:00 【Java collection】
I read teacher a Niu's masterpiece a few days ago Leader : Who uses the scheduled task to close the order , Get out of here ![1] It is found that the scheme has several flaws , Here are some suggestions for discussion .
In the electricity supplier 、 Payment and other fields , There are often such scenes , The user gave up payment after placing an order , The order will be closed after the specified time period , Careful, you must have found something like a treasure 、 A certain East has such logic , And the time is accurate , Error in 1s Inside ; How did they achieve it ?
There are several general implementation methods :
Use rocketmq、rabbitmq、pulsar Wait for the delayed delivery function of the message queue
Use redisson Provided DelayedQueue
There are some schemes that are widely circulated but have fatal defects , Don't use it to implement deferred tasks
Use redis Expired monitoring for
Use rabbitmq The dead letter line
Use non persistent time wheels
redis Overdue monitoring
stay Redis The official Manual of keyspace-notifications: timing-of-expired-events It is clearly stated in :
Basically expired events are generated when the Redis server deletes the key and not when the time to live theoretically reaches the value of zero
redis The implementation of automatic expiration is : Scheduled task offline scan and delete some expired keys ; Lazy checking for expiration when accessing keys and deleting expired keys .redis There is no guarantee that it will be deleted and sent an expiration notice at the set expiration time . actually , It is also common that the expiration notification is several minutes later than the set expiration time .
In addition, the key space notification is sent and forgotten (fire and forget) Strategy , Delivery is not guaranteed like message queuing . When a client subscribes to events, it will lose all the events distributed to it during disconnection .
This is a more “LOW” Solutions for , Please do not use .
Another big guy did the test Don't rely too much on Redis Expired monitoring for [2], If you are interested in it, you can check it by yourself .
rabbitmq Dead letter
Dead letter (Dead Letter) yes rabbitmq A mechanism provided . When a message meets one of the following conditions, it will become dead letter :
Message denied confirmation ( Such as channel.basicNack) And at this point requeue Property is set to false.
The message's lifetime in the queue exceeds the set TTL Time
The number of messages in the message queue has exceeded the maximum queue length
If dead letter queue is configured , Dead letter will be rabbitmq Put it in the dead letter queue .
stay rabbitmq The process of creating a dead letter queue in the :
Create a switch as a dead letter switch
Configure in the business queue x-dead-letter-exchange and x-dead-letter-routing-key, Set the switch in the first step as the dead letter switch of the service queue
Create a queue on the dead letter switch , And listen to this queue
Dead letter queue is designed to store messages that are not normally consumed , Easy to check and re deliver . The dead letter queue also does not guarantee the delivery time , Before the first message becomes a dead letter , The following messages will not be delivered as dead letters even if they are expired .
To solve this problem ,rabbit The official launch of the delayed delivery plug-in rabbitmq-delayed-message-exchange , It is recommended to use the official plug-in for delay messages .
Here's a digression , Use redis Overdue monitoring or rabbitmq The dead letter queue uses Middleware in a way unexpected to the designer , There are some hidden dangers in this unexpected behavior , Such as lack of consistency and reliability assurance , Low throughput 、 Resource leakage, etc . A famous example is that many people use redis Of list As message queue , So that the last author can't read and write disque And finally evolved into redis stream. Try not to abuse middleware at work , Do professional things with professional components
Time wheel
Time wheel is an excellent data structure for timed tasks , However, the vast majority of time wheel implementations are pure memory without persistence . After the process running the time wheel crashes, all the tasks in it will disappear , So I advise you to use it carefully .
redisson delayqueue
redisson delayqueue It's based on redis zset Implementation of delay queue based on .delayqueue One of them is called timeoutSetName Ordered set of , Of the elements score Is the delivery timestamp .
delayqueue Can use regularly zrangebyscore Scan messages that have reached the delivery time , Then move them to the ready message list .
delayqueue Guarantee redis Messages are not lost without crashing , Try it when you don't have a better solution .
When the database index is well designed , The overhead of regularly scanning the database for outstanding orders is not as large as expected . In the use of redisson delayqueue The method of scanning the database can be used as the compensation mechanism when waiting for timed task middleware , Avoid task loss caused by middleware failure .
Conclusion
First of all, it is recommended to use rocketmq、pulsar And other message queues with scheduled delivery function .
When it is not convenient to obtain a professional message queue, you can consider using redisson delayqueue Based on redis Delay queue scheme , But for redis Design compensation protection mechanism in case of crash .
When you can't use redisson delayqueue You can consider using the time wheel . Because the time wheel restart is far better than redis Restart frequently , Protection mechanisms such as regular library scanning are more important .
Never use redis Overdue monitoring implements scheduled tasks .
author :Finley
https://www.cnblogs.com/Finley/p/16395466.html
official account “Java selected ” The published content indicates the source of , All rights reserved ( Those whose copyright cannot be verified or whose source is not indicated all come from the Internet , Reprinted , The purpose of reprinting is to convey more information , The copyright belongs to the original author . If there is any infringement , Please contact the , The author will delete the first time !
Many people have asked recently , Is there any readers Communication group ! The way to join is simple , official account Java selected , reply “ Add group ”, You can join the group !
Java Interview questions ( Wechat applet ):3000+ The road test questions , contain Java Basics 、 Concurrent 、JVM、 Threads 、MQ series 、Redis、Spring series 、Elasticsearch、Docker、K8s、Flink、Spark、 Architecture design, etc , Brush questions online at any time !
------ Special recommendation ------
Special recommendation : Focus on the most cutting-edge information and technology sharing , Official account for preparing for overtaking on curves and various open source projects and efficient software ,「 Big coffee notes 」, Focus on finding good things , It's worth your attention . Click the official account card below to follow .
If the article helps , Click to see , Forward! !
边栏推荐
- 普源DS1000Z系列数字示波器在通信原理实验中的应用方案
- C语言单向链表练习
- JS实现文字滚动 跑马灯效果
- Common methods of threads
- [book club issue 13] multimedia processing tool ffmpeg tool set
- Three years of graduation, half a year of distance | community essay solicitation
- hbuildx中夜神模拟器的配置以及热更新
- VIM mapping command
- Parameterization of controls in katalon
- 资深开发人员告诉你,怎样编写出优秀的代码?
猜你喜欢

Leetcode skimming: binary tree 07 (maximum depth of binary tree)

R语言中如何查看已安装的R包

Two commonly used graphics can easily realize data display

Leetcode skimming: binary tree 09 (minimum depth of binary tree)

分布式系统:what、why、how

干货!基于GAN的稀有样本生成

ModStartBlog 现代化个人博客系统 v5.2.0 源码下载

Parameterization of controls in katalon

I was tortured by my colleague's null pointer for a long time, and finally learned how to deal with null pointer
![[Logitech] m720](/img/bb/44144a1c3907808398c05b3b36962c.png)
[Logitech] m720
随机推荐
96% of the collected traffic is prevented by bubble mart of cloud hosting
Use NRM and NVM to manage your NPM source and node versions
Redis:集合Set类型数据的操作命令
Perf simple process for multithreaded profile
TCP-三次握手和四次挥手简单理解
Two commonly used graphics can easily realize data display
Leetcode brush questions: binary tree 05 (flip binary tree)
RHCSA 08 - automount配置
Operation of ES6
*. No main manifest attribute in jar
苹果CMS仿西瓜视频大气响应式视频模板源码
Redis cluster view the slots of each node
毕业三年,远程半年 | 社区征文
[csrf-01] basic principle and attack and defense of Cross Site Request Forgery vulnerability
[Huawei cloud IOT] reading notes, "Internet of things: core technology and security of the Internet of things", Chapter 3 (I)
How to telecommute more efficiently | community essay solicitation
Katalon使用script实现查询List大小
ModStartBlog 现代化个人博客系统 v5.2.0 源码下载
R语言dplyr中的Select函数变量列名
C语言双向链表初版