Determination method of delay message based on Redis ordered set

By using ordered collection Zset and compressed list/table data structure in Redis, the timeliness, message loss and performance problems of delayed messages in Redis and Pulsar are solved, and high-performance, low-latency message processing and orderly are achieved. reduces resource consumption.

CN119071355APending Publication Date: 2024-12-03BEIYIN FINANCIAL TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411065172.7
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-08-05
Publication Date
2024-12-03

AI Technical Summary

Technical Problem

The existing delayed message implementation solutions have problems such as timeliness, message loss risk, performance impact and memory overhead in Redis and Pulsar, especially the delayed message index queue reconstruction time overhead after high load and failover.

Method used

Using Redis ordered set Zset as the data structure of the delayed message queue, the score attribute of Zset is used to sort by time, and the messages are processed using timed task polling to avoid empty polling, reduce CPU and I/O resource consumption, and combine with compressed lists and table jump data structure optimize performance to ensure data consistency and high performance.

Benefits of technology

It realizes high-performance and low-latency latency message processing, reduces CPU and I/O resource consumption, ensures the order and consistency of messages, avoids message loss and performance impact, and simplifies the maintenance of message queues.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119071355A_ABST
    Figure CN119071355A_ABST
Patent Text Reader

Abstract

The invention discloses a method for determining a delay message based on a Redis ordered set, and the method comprises the steps: enabling a user to initiate a business request which will create the delay message at a client, and enabling the request to be loaded to an application cluster at a rear end through a gateway; and carrying out consumption processing on the delay message. The delay message queue realized by using the Zset uses a zrangebyscore command to effectively avoid empty polling and reduce unnecessary CPU (Central Processing Unit) and I / O (Input / Output) resource consumption.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of message sending and receiving, and in particular to a method for determining delayed messages based on a Redis ordered set. Background Art

[0002] Delayed messaging is a messaging mechanism that is usually used in scenarios where messages need to be sent at a specific time or after certain conditions are met. Delayed messaging allows messages to be sent at a certain point in the future, and is used to implement a series of functions such as message reminders and order timeout closure reminders. Based on different systems and architectures, the way to implement delayed messaging is also different, but the basic principles are similar. For example, in the implementation of some frameworks, the visibility timeout of the message can be set, so that after the message is received by the consumer, if it is not confirmed and processed within the timeout period, it will become visible again for other consumers to process.

[0003] There are two common delayed message implementation schemes:

[0004] 1. A mechanism based on Redis to monitor expired keys.

[0005] Redis provides the function of monitoring expired keys. Expiration monitoring can be enabled by setting the notify-keyspace-events directive in the redis.conf configuration file.

[0006] 2. Delayed messages based on Pulsar’s “any time”.

[0007] Delayed messages based on Pulsar will directly enter the topic specified by the client, and then create a time-based priority queue in the off-heap memory to maintain the index information of delayed messages. The message with the shortest delay time will be placed at the head, and the message with longer delay time will be placed at the back. When performing the consumption logic, it will determine whether there is any expired message that needs to be delivered. If there is, it will be taken out of the queue, and the corresponding message will be queried according to the index of the delayed message for consumption.

[0008] 1. A mechanism based on Redis to monitor expired keys.

[0009] Timeliness issue: Redis expiration events are not guaranteed to be triggered immediately when a key expires. Redis may send expiration events at any time after the key expires, depending on the internal scheduling and execution mechanism.

[0010] Risk of message loss: Before Redis 5.0, expiration events were sent through Pub / Sub mode and were not persisted. If the client disconnects while the message is being sent, the message may be lost.

[0011] Performance issues: If there is a lot of expired key data, it may affect the performance of Redis, especially under high load conditions.

[0012] 2. Delayed messages based on Pulsar’s “any time”.

[0013] Memory overhead: The queue that maintains the delayed message index is placed in the off-heap memory, and this queue is based on the subscription group dimension. For example, if there are N subscription groups for a Topic, then if your Topic uses delayed messages, N queues will be created. As the number of delayed messages increases and the time span increases, the memory usage of each queue will also increase.

[0014] The rebuilding time of the delayed message index queue after failover: For large-scale delayed messages with a long span of time, the rebuilding time may reach the hour level. Summary of the invention

[0015] In view of the above problems, the present invention is proposed to provide a method for determining delayed messages based on Redis ordered sets, which overcomes the above problems or at least partially solves the above problems.

[0016] According to one aspect of the present invention, a method for determining delayed messages based on a Redis ordered set is provided, the method comprising:

[0017] The user initiates a business request on the client that creates a delayed message, and the request is loaded through the gateway to the backend application cluster;

[0018] Perform consumption processing of delayed messages.

[0019] Optionally, the application cluster includes an application server, and application services call each other through Feign.

[0020] Optionally, the application server determines the delay time of sending the delayed message according to the service type, and sets the parameter to the score of Zset, and sets the key and value of the delayed message for consumption processing of the delayed message.

[0021] Optionally, the consumption processing of the delayed message specifically includes:

[0022] During the scheduled task polling process, the specified key in Redis Zset is queried, and the data set with a score between 0 and the current time is traversed for consumption. The Zset data structure can ensure sorting by score, so that the earliest data is at the front of the queue to ensure that it is consumed first and deleted from Zset after successful consumption.

[0023] Optionally, the delayed message as a whole is developed based on the Redis framework Zset ordered set.

[0024] Optionally, the data structure is:

[0025] When the number of Zset elements is less than 128 and the length of each element is less than 64KB, Redis uses a compressed list as the underlying data structure;

[0026] When the element data or length exceeds the limit, Redis uses a combination of hash tables and skip lists as the underlying data structure.

[0027] Optionally, the compression list includes:

[0028] A data structure allocated in continuous memory that stores an ordered set of elements, each of which includes a score and a value.

[0029] Optionally, the skip list is a probabilistic balanced tree, which achieves fast data access and order maintenance through a multi-layer linked list structure;

[0030] Each layer of the skip list maintains an ordered linked list, and the number of nodes in each layer gradually increases, achieving fast data insertion and deletion operations.

[0031] The present invention provides a method for determining delayed messages based on a Redis ordered set, the method comprising: a user initiates a service request that will create a delayed message on a client, the request is loaded to a back-end application cluster via a gateway, and consumption processing of the delayed message is performed. The delayed message queue implemented using Zset uses the zrangebyscore command to effectively avoid empty polling and reduce unnecessary CPU and I / O resource consumption.

[0032] The above description is only an overview of the technical solution of the present invention. In order to more clearly understand the technical means of the present invention, it can be implemented according to the contents of the specification. In order to make the above and other purposes, features and advantages of the present invention more obvious and easy to understand, the specific implementation methods of the present invention are listed below. BRIEF DESCRIPTION OF THE DRAWINGS

[0033] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the accompanying drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other accompanying drawings can be obtained based on these accompanying drawings without paying creative work.

[0034] Figure 1 An architectural diagram of a method for determining delayed messages based on a Redis ordered set provided in an embodiment of the present invention. DETAILED DESCRIPTION

[0035] The exemplary embodiments of the present disclosure will be described in more detail below with reference to the accompanying drawings. Although the exemplary embodiments of the present disclosure are shown in the accompanying drawings, it should be understood that the present disclosure can be implemented in various forms and should not be limited by the embodiments set forth herein. On the contrary, these embodiments are provided to enable a more thorough understanding of the present disclosure and to fully convey the scope of the present disclosure to those skilled in the art.

[0036] The terms "comprises" and "having" and any variations thereof in the description embodiments, claims and drawings of the present invention are intended to cover non-exclusive inclusions, for example, including a series of steps or units.

[0037] The technical solution of the present invention is further described in detail below in conjunction with the accompanying drawings and embodiments.

[0038] Delayed message: Delayed message is developed based on the Redis framework Zset ordered set.

[0039] Data structure: When the number of Zset elements is less than 128 and the length of each element is less than 64KB, Redis uses a compressed list (ziplist) as the underlying data structure. When the element data or length exceeds the above limits, Redis uses a combination of a hash table (dict) and a skip list (skiplist) as its underlying data structure.

[0040] Ziplist: A ziplist is a contiguous memory-allocated data structure that stores an ordered collection of elements, each consisting of its score and value. Ziplists are efficient in memory usage and performance, especially for small data sets.

[0041] Skiplist: Skiplist is a probabilistic balanced tree that achieves fast data access and order maintenance through a multi-layer linked list structure. Each layer of the skiplist maintains an ordered linked list, and the number of nodes in each layer gradually increases, thereby achieving fast data insertion and deletion operations.

[0042] Atomic operations: All Zset operations are atomic, ensuring data consistency.

[0043] Cluster support: In a Redis cluster environment, Zset can be distributed on different nodes to support large-scale data sets.

[0044] Monitoring and maintenance: Redis provides monitoring tools and commands to facilitate monitoring and maintenance of Zset status.

[0045] Through these technical solutions, the use of Redis's Zset ordered set implementation can provide a high-performance, low-latency delayed message sending solution while ensuring the orderliness and consistency of data.

[0046] Project architecture: The project architecture is built based on the Spring CloudAlibaba microservice framework.

[0047] Service registration and discovery: Microservice registration and discovery are achieved by using the Nacos service governance framework.

[0048] Api Gateway: encapsulates the internal structure of the service and provides a unified entry service to the outside world.

[0049] Load balancing: Use OpenFiegn to implement client-based load balancing.

[0050] The system mainly includes two cluster services, microservice cluster and Redis cluster.

[0051] The microservice cluster mainly completes the construction of system-related functions. After the microservice cluster service is started, the service will be registered to the Nacos registration center. The Nacos registration center consists of three nodes, which can ensure that the Nacos service is still available after any service goes down.

[0052] The Redis cluster consists of 3 machines and 6 nodes, including 3 master nodes. Each master node has a slave node. In order to ensure high performance, Redis stores data in memory, thereby ensuring high throughput and low latency.

[0053] Main process:

[0054] 1. Delay message sending

[0055] The user initiates a business request on the client that creates a delayed message. The request is loaded through the gateway to the backend application cluster. The application cluster consists of three machines, and the application services call each other through Feign. The application server determines the delay time of the delayed message according to the business type, and sets this parameter to the score of Zset. At the same time, the key and value of the delayed message are set for consumption processing of the delayed message.

[0056] 2. Delayed message consumption

[0057] During the scheduled task polling process, the specified key in Redis Zset is queried, and the data set with a score between 0 and the current time is traversed for consumption. The Zset data structure can ensure sorting by score, so that the earliest data is at the front of the queue, thereby ensuring that it is consumed first and deleted from Zset after successful consumption.

[0058] Delayed messages use the Redis framework’s Zset ordered set data structure to ensure high performance and flexibility of delayed messages while making them easy to monitor and maintain.

[0059] The project is developed based on Spring CloudAlibaba and uses Nacos as the registration and discovery center to facilitate distributed expansion in the future.

[0060] In a distributed system, Redis Zset can act as a central component to coordinate delayed task processing on different nodes.

[0061] Beneficial effects:

[0062] 1. High performance: Redis is a memory-based data storage system with very fast operation speed. The Zset data structure is used to give full play to the high performance of Redis.

[0063] 2. Order: Zset can sort elements according to their scores, which means that orderly message processing can be achieved, for example, by sorting them in the order in which they should be consumed.

[0064] 3. Reduce empty polling: Compared with traditional delayed message queues, the delayed message queue implemented using Zset uses the zrangebyscore command to effectively avoid empty polling and reduce unnecessary CPU and I / O resource consumption.

[0065] The above specific implementation methods further illustrate the objectives, technical solutions and beneficial effects of the present invention in detail. It should be understood that the above are only specific implementation methods of the present invention and are not intended to limit the scope of protection of the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present invention should be included in the scope of protection of the present invention.

Claims

1. A method for determining delayed messages based on Redis ordered sets, characterized in that: The determination method comprises: The user initiates a business request on the client that creates a delayed message, and the request is loaded through the gateway to the backend application cluster; Perform consumption processing of delayed messages.

2. According to a method for determining delayed messages based on a Redis ordered set according to claim 1, it is characterized in that: The application cluster includes application servers, and application services make calls between services through Feign.

3. According to a method for determining delayed messages based on a Redis ordered set according to claim 2, it is characterized in that: The application server determines the time for delaying the sending of the delayed message according to the service type, and sets the parameter to the score of Zset, and sets the key and value of the delayed message for consumption processing of the delayed message.

4. According to a method for determining delayed messages based on Redis ordered sets according to claim 1, it is characterized in that: The consumption process of the delayed message specifically includes: During the scheduled task polling process, the specified key in Redis Zset is queried, and the data set with a score between 0 and the current time is traversed for consumption. The Zset data structure can ensure sorting by score, so that the earliest data is at the front of the queue to ensure that it is consumed first and deleted from Zset after successful consumption.

5. According to a method for determining delayed messages based on Redis ordered sets according to claim 1, it is characterized in that: The delayed message as a whole is developed based on the Redis framework Zset ordered set.

6. According to a method for determining delayed messages based on Redis ordered sets according to claim 4, it is characterized in that: The data structure is: When the number of Zset elements is less than 128 and the length of each element is less than 64KB, Redis uses a compressed list as the underlying data structure; When the element data or length exceeds the limit, Redis uses a combination of hash tables and skip lists as the underlying data structure.

7. A method for determining delayed messages based on a Redis ordered set according to claim 6, characterized in that: The compression list includes: A data structure allocated in continuous memory that stores an ordered set of elements, each of which includes a score and a value.

8. According to a method for determining delayed messages based on Redis ordered sets according to claim 6, it is characterized in that: The skip list is a probabilistic balanced tree that achieves fast data access and orderly maintenance through a multi-layer linked list structure; Each layer of the skip list maintains an ordered linked list, and the number of nodes in each layer gradually increases, achieving fast data insertion and deletion operations.