当前位置:网站首页>Leader: who can use redis expired monitoring to close orders and get out of here!
Leader: who can use redis expired monitoring to close orders and get out of here!
2022-06-30 18:49:00 【Java R & D Corps】
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-exchangeandx-dead-letter-routing-key, Set the switch in the first step as the dead letter switch of the service queueCreate 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、pulsarAnd other message queues with scheduled delivery function .When it is not convenient to obtain a professional message queue, you can consider using redisson delayqueueBased on redis Delay queue scheme , But for redis Design compensation protection mechanism in case of crash .When you can't use redisson delayqueueYou 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 .
Reference resources :
https://juejin.cn/post/6987233263660040206
https://juejin.cn/post/6844904158227595271
source :cnblogs.com/Finley/p/16395466.html
END
Recommended reading
END
One click generation Springboot & Vue project !【 Bi set artifact 】
Java Visual programming tool series ( One )
Java Visual programming tool series ( Two )
By the way Recommend a GitHub project , This GitHub Thousands of commonly used technology books have been sorted out PDF, Most of the core technical books can be found here ,
GitHub Address :https://github.com/javadevbooks/books
The ebook has been updated , You can download what you need , Remember to order one star, Ongoing update ..

By the way Recommend a GitHub project , This GitHub Thousands of commonly used technology books have been sorted out PDF, Most of the core technical books can be found here ,
GitHub Address :https://github.com/javadevbooks/books
The ebook has been updated , You can download what you need , Remember to order one star, Ongoing update ..
边栏推荐
- 抖音最新Xbogus,signature生成js逆向分析
- Compilation problems and solutions of teamtalk winclient
- 手机股票账号开户安全吗?是靠谱的吗?
- 如何做好软件系统的需求调研,七种武器让你轻松搞定
- GameFi链游系统开发NFT技术
- 使用excel快速生成sql语句
- 麻烦问下 Flink支持同步数据到 sqlserver么
- 漏洞复现----35、uWSGI PHP 目录遍历漏洞 (CVE-2018-7490)
- Digital intelligent supplier management system solution for coal industry: data driven, supplier intelligent platform helps enterprises reduce costs and increase efficiency
- LeetCode动态规划经典题(一)
猜你喜欢

depends工具查看exe和dll依赖关系

电子元器件招标采购商城:优化传统采购业务,提速企业数字化升级

countdownlatch 和 completableFuture 和 CyclicBarrier

MySQL advanced - Architecture

What if icloud photos cannot be uploaded or synchronized?

ONEFLOW source code parsing: automatic inference of operator signature

剑指 Offer 17. 打印从1到最大的n位数
Do you really understand the persistence mechanism of redis?

Switching routing (VLAN) experiment

ForkJoinPool
随机推荐
hdfs上的数据导入到clickhouse用什么方式最快呢?spark通过jdbc导入,还是hdfs
Geoffrey Hinton: my 50 years of in-depth study and Research on mental skills
Redis - persistent RDB and persistent AOF
Openlayers roller shutter map
Only black-and-white box test is required for test opening post? No, but also learn performance test
挑选智能音箱时,首选“智能”还是“音质”?这篇文章给你答案
医院在线问诊小程序源码 互联网医院源码 智慧医院源码
系统集成项目管理工程师认证高频考点:编制项目范围管理计划
MySQL cannot find mysql Temporary solution of sock file
Multipass Chinese document - setting graphical interface
「杂谈」对数据分析未来的几点思考
剑指 Offer 17. 打印从1到最大的n位数
VScode 状态条 StatusBar
LeetCode动态规划经典题(一)
电子元器件行业在线采购系统精准匹配采购需求,撬动电子产业数字化发展
漏洞复现----38、ThinkPHP5 5.0.23 远程代码执行漏洞
Development and construction of NFT mining tour gamefi chain tour system
MySQL advanced - Architecture
「经验」我对用户增长的理解『新用户篇』
Sword finger offer 16 Integer power of numeric value