当前位置:网站首页>Common implementation schemes of delay message
Common implementation schemes of delay message
2022-04-23 22:01:00 【InfoQ】
Preface
Implementation scheme
1. Scheme based on external storage
be based on database ( Such as MySQL)
CREATE TABLE `delay_msg` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`delivery_time` DATETIME NOT NULL COMMENT ' Delivery time ',
`payloads` blob COMMENT ' The message content ',
PRIMARY KEY (`id`),
KEY `time_index` (`delivery_time`)
)
- Implement a simple ;
- B+Tree The index is not suitable for a large number of writes in the message scenario ;
be based on RocksDB
- RocksDB LSM The tree is very suitable for a large number of message scenarios ;
- The implementation scheme is heavy , If you adopt this scheme , You have to implement it yourself RocksDB Data disaster recovery logic ;
be based on Redis
- Messages Pool All delay messages are stored , The structure is KV structure ,key For message ID,value For a specific message( Choose here Redis Hash The structure is mainly because hash Structure can store a large amount of data , When there is a lot of data, it will be carried out gradually rehash Capacity expansion , And for the HSET and HGET The time complexity of commands is O(1))
- Delayed Queue yes 16 An ordered queue ( The queue supports horizontal expansion ), The structure is ZSET,value by messages pool Chinese News ID,score For expiration time **( It is divided into multiple queues to improve the scanning speed )**
- Worker Represents a processing thread , Scan through scheduled tasks Delayed Queue Messages due in
- Redis ZSET It is very suitable for implementing delay queue
- Performance issues , although ZSET Insert is a O(logn) The operation of , however Redis Based on memory operation , And many internal performance optimizations have been made .
Defects and improvement of timed thread inspection
2. Open source MQ Implementation scheme in
RocketMQ
- Level The number is fixed , Every Level Have your own timer , It's not expensive
- take Level Put the same message into the same Queue in , Ensure the same Level The order of the message ; Different Level Put it in a different place Queue in , Ensure the accuracy of delivery time ;
- By supporting only fixed Level, The sorting of messages with different delays is changed into fixed Level Topic Additional write operation
- Level Configuration modification is too expensive , Fix Level inflexible
- CommitLog Because of the existence of delayed messages
Pulsar
- ** Memory overhead :** The queue that maintains the delayed message index is placed in out of heap memory , And this queue is based on subscription group (Kafka Consumer groups in ) For the dimension of , Like you Topic Yes N Subscription groups , So if you this Topic Delayed messages are used , Will be created N individual queue ; And with the increase of delayed messages , Increase in time span , The memory usage of each queue will also increase .( Yes , Under this scheme , Supporting arbitrary delayed messages may make this defect more serious )
- ** The rebuild time cost of delaying the message index queue after failover :** For large-scale delay messages with long span time , Reconstruction time may be at the hour level .( Excerpt from Pulsar The official account of the public )
- Storage overhead : The time span of delayed messages will affect Pulsar Space recycling of consumed message data in . For example , Yours Topic If the business requires to support a one month delay message , Then you sent a message with a delay of one month , So you this Topic The middle and bottom storage will retain message data for a whole month , Even this month 99% The normal news has been consumed .
QMQ
- The time wheel algorithm is suitable for delay / Timing message scenario , Eliminate the sorting of delayed messages , Insert and delete operations are O(1) Time complexity of ;
- Through multi-level time wheel design , It supports delay messages with large time span ;
- Load by delay , There will only be messages to be consumed recently in memory , Longer latency messages are stored on disk , Memory friendly ;
- Delay messages are stored separately (schedule log), It will not affect the space recycling of normal messages ;
summary
版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/113/202204232156502028.html
边栏推荐
- [※ leetcode refers to offer 48. The longest substring without repeated characters (medium)]
- FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ‘:app:stripDe
- Hystrix断路器开启条件和流程以及默认备选处理
- Getting started with detectron2
- C # problem of updating data: dynamic SQL generation is not supported for multiple base tables
- Ribbon 服务调用
- Implementation of service fusing
- Correction of date conversion format error after Oracle adds a row total
- [leetcode sword finger offer 58 - I. flip word order (simple)]
- How to make Jenkins job run automatically after startup
猜你喜欢
DW basic course (II)
Tsinghua University | webface260m: benchmark for million level deep face recognition (tpami2022)
How to make Jenkins job run automatically after startup
Based on jsplumb JS to achieve multi list one to many connection effect
服务间通信方式
[leetcode sword finger offer 10 - II. Frog jumping steps (simple)]
Opening conditions and process of hystrix circuit breaker and default alternative treatment
[※ leetcode refers to offer 48. The longest substring without repeated characters (medium)]
Introduction to tensorrt
Pipes and xargs
随机推荐
[leetcode refers to the maximum profit of offer 63. Stock (medium)]
Ribbon组件基本介绍和使用
21. Basic usage of MariaDB
[leetcode sword finger offer 58 - I. flip word order (simple)]
Deep understanding of modern mobile GPU (continuously updating)
[※ leetcode refers to offer 46. Translate numbers into strings (medium)]
Yolov5 NMS source code understanding
将本地上的图片转换成Base64编码
April 24, 2022 Daily: current progress and open challenges of applying deep learning in the field of Bioscience
手撕《Google SRE Book》
通过OpenFeign传递对象类型参数
YOLOv5 Unable to find a valid cuDNN algorithm to run convolution
Oracle intercepts special characters
Hystrix components
MySQL back to table
Hystrix简介和服务端熔断的实现
Automatic heap dump using MBean
OpenFeign之响应处理
Two Stage Detection
清华大学 | WebFace260M:百万级深度人脸识别的基准(TPAMI2022)