当前位置:网站首页>Who can use redis expired monitoring to close orders and get out of here!
Who can use redis expired monitoring to close orders and get out of here!
2022-06-30 01:38:00 【Fat brother 1930】
Preface
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 .
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 .
PS:
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 .
边栏推荐
- Pytorch模型训练到哪里找预训练模型?
- [mrctf2020]ezpop-1 | PHP serialization
- Unity2D--给动画添加关键帧并绑定事件
- [binary tree] maximum binary tree II
- Varnish 基础概览2
- The first technology podcast month will begin soon
- [535. encryption and decryption of tinyurl]
- Geotools wkt coordinate system conversion
- Varnish foundation overview 1
- [Thesis Writing] English thesis writing guide
猜你喜欢

C语言 害死人不偿命的(3n+1)猜想

手势数字启蒙学习机

C语言 继续(3n+1)猜想

Sentinel source code analysis Part 7 - sentinel adapter module - Summary

cookie加密11

C语言 我要通过

The Web3 era is coming? Inventory of five Web3 representative projects | footprint analytics

ES6 one line code for array de duplication
![【图神经网络】图分类学习研究综述[2]:基于图神经网络的图分类](/img/5f/b23b64eed7f28ffd92c122b6859e2d.png)
【图神经网络】图分类学习研究综述[2]:基于图神经网络的图分类
![[recommendation system] concise principle and code implementation of user based collaborative filtering](/img/3b/d46e37e7ae245cf2931d32d1ebbe4c.png)
[recommendation system] concise principle and code implementation of user based collaborative filtering
随机推荐
C语言 写出这个数
C语言 说反话
“乘风破浪”的芒果超媒,能上岸吗?
传统微服务框架如何无缝过渡到服务网格 ASM
首届技术播客月开播在即
Cookie encryption 10
Can mango hypermedia, which "braves the wind and waves", go ashore?
ES6 one line code for array de duplication
Varnish 基础概览3
cookie加密11
Varnish 基础概览2
Application features and functions of painting Aquarium
Chiffrement des cookies 8
Design and implementation of spark offline development framework
JS content confusion, return content encryption
Sentinel source code analysis Chapter 9 - core process - lookprocesschain finding resource processing chain
Circular right shift of array elements in C language
Tools and life services
当大学毕业感到迷茫怎么办?
Cookie encryption 13