Distributed transaction processing method and device based on message queue and compensation strategy

By adopting distributed transaction processing methods based on message queue and compensation strategies in distributed systems, the problem of insufficient performance bottlenecks and fault tolerance in the face of network partition and service node failures in the distributed system is solved, and the final consistency of data and system performance improvement is achieved.

CN119988141APending Publication Date: 2025-05-13SHENZHEN COOCAA NETWORK TECH CO LTD
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202510111178.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-01-23
Publication Date
2025-05-13

AI Technical Summary

Technical Problem

When existing distributed transaction processing methods face network partition and service node failures, there are problems of performance bottlenecks and insufficient fault tolerance.

Method used

A distributed transaction processing method based on message queue and compensation policy is adopted to ensure the final consistency of transactions by encapsulating transaction requests into transaction messages, distributing them to message queues, executing asynchronously and recording results, and rolling back or compensation operations based on global state and compensation policy.

Benefits of technology

The ultimate consistency of distributed system data is achieved, the system's performance and fault tolerance are improved, resource consumption and response time are reduced, and the modern distributed system's needs for high availability and high throughput are met.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119988141A_ABST
    Figure CN119988141A_ABST
Patent Text Reader

Abstract

The invention discloses a distributed transaction processing method and device based on a message queue and a compensation strategy, and the method comprises the steps: initiating a transaction request crossing multiple services or databases, and packaging the transaction request into one or more transaction messages; carrying out message distribution, and distributing the messages to corresponding service nodes through message queues; each service node asynchronously obtains the transaction message from the message queue, and controls each service node to execute a corresponding local transaction operation according to the received transaction message; and carrying out rollback or compensation operation on failed or partially successful transactions on the basis of the transaction global state updated by the operation execution result and a preset intelligent compensation strategy so as to ensure the final consistency of the transactions. According to the method, transaction initiation, message distribution, transaction execution, state confirmation, message compensation, transaction confirmation and the like are adopted, so that the final consistency of the distributed system data can be realized through a message queue and a message compensation mechanism, and convenience is provided for the use of a user.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of Internet technology, and in particular to a distributed transaction processing method, device, intelligent terminal and storage medium based on message queue and compensation strategy. Background Art

[0002] With the development of Internet technology, distributed systems play an increasingly important role in processing large-scale data and high-concurrency requests. In such systems, transaction consistency is a key factor in ensuring data accuracy and system reliability.

[0003] Conventional distributed transaction processing methods, such as two-phase commit (2PC) and three-phase commit (3PC), can ensure the principle and consistency of transactions, but they have performance bottlenecks and insufficient fault tolerance when facing distributed-specific problems such as network partitions and service node failures.

[0004] Therefore, the existing technology still needs to be improved and developed. Summary of the invention

[0005] The technical problem to be solved by the present invention is that, in view of the problems and defects of the above-mentioned prior art, a distributed transaction processing method, device, intelligent terminal and storage medium based on message queue and compensation strategy are provided. The present invention provides a method for ultimately achieving final data consistency based on message queue and message compensation mechanism, which provides convenience for users.

[0006] The technical solution adopted by the present invention to solve the problem is as follows:

[0007] A distributed transaction processing method based on message queues and compensation strategies, comprising:

[0008] Initiate a transaction request across multiple services or databases according to the distributed transaction processing requirements, encapsulate the transaction request into one or more transaction messages, and attach necessary transaction metadata;

[0009] Distribute the encapsulated transaction messages to the corresponding service nodes through the message queue;

[0010] Each service node asynchronously obtains transaction messages from the message queue, controls each service node to perform corresponding local transaction operations according to the received transaction messages, and records the operation execution results;

[0011] The message queue updates the global status of the transaction according to the operation execution results of all service nodes;

[0012] Based on the global transaction status updated by the operation execution results and the preset intelligent compensation strategy, rollback or compensation operations are performed on failed or partially successful transactions to ensure the eventual consistency of the transaction.

[0013] The distributed transaction processing method based on message queue and compensation strategy, wherein the steps of initiating a transaction request across multiple services or databases according to the distributed transaction processing requirements, encapsulating the transaction request into one or more transaction messages, and attaching necessary transaction metadata include:

[0014] According to the distributed transaction processing requirements, control the user terminal or system component to initiate a transaction request across multiple services or databases;

[0015] The transaction request is encapsulated into one or more transaction messages, and necessary transaction metadata is attached, wherein the transaction metadata includes: transaction ID, operation type, and participating nodes.

[0016] The distributed transaction processing method based on message queue and compensation strategy, wherein the step of distributing the encapsulated transaction message to the corresponding service node through the message queue includes:

[0017] Send the packaged transaction message to a message queue, which is responsible for storing and distributing transaction messages;

[0018] The message queue stores the encapsulated transaction messages persistently to ensure that the messages are not lost in the event of a system failure.

[0019] The message queue distributes the transaction message to the corresponding service node according to the participating node information in the transaction message.

[0020] The distributed transaction processing method based on message queue and compensation strategy, wherein each service node asynchronously obtains transaction messages from the message queue, controls each service node to perform corresponding local transaction operations according to the received transaction messages, and records the operation execution results, comprises the following steps:

[0021] Each service node asynchronously obtains transaction messages from the message queue.

[0022] Control each service node to perform corresponding local transaction operations according to the received transaction message;

[0023] And control each service node to record the operation results, including success, failure or abnormal status.

[0024] The distributed transaction processing method based on message queue and compensation strategy, wherein the message queue updates the global state of the transaction according to the operation execution results of all service nodes, comprises:

[0025] Each service node feeds back the operation execution result to the message queue;

[0026] The message queue updates the global status of the transaction based on feedback from all service nodes.

[0027] The distributed transaction processing method based on message queue and compensation strategy, wherein the transaction global state updated based on the operation execution result and the preset intelligent compensation strategy, performs a rollback or compensation operation on the transaction failed or partially successful to ensure the final consistency of the transaction, includes the following steps:

[0028] Based on the preset intelligent compensation strategy, the compensation operation is triggered through the message queue for the failed or partially successful transactions in the global status of the transaction updated by the operation execution result;

[0029] According to the triggered compensation operation, the failed or partially successful transactions in the executed transaction operations are rolled back or compensated to ensure the eventual consistency of the transaction.

[0030] The distributed transaction processing method based on message queue and compensation strategy, wherein the transaction global state updated based on the operation execution result and the preset intelligent compensation strategy, performs a rollback or compensation operation on the transaction failed or partially successful to ensure the final consistency of the transaction, and further includes:

[0031] When all compensation operations for failed or partially successful transactions are completed, the final status of the transaction is confirmed;

[0032] The final status confirmation result of the transaction is sent to the transaction initiator terminal or related system components for corresponding notification.

[0033] A distributed transaction processing device based on a message queue and a compensation strategy, wherein the device comprises:

[0034] A transaction initiation module is used to initiate a transaction request across multiple services or databases according to the distributed transaction processing requirements, encapsulate the transaction request into one or more transaction messages, and attach necessary transaction metadata;

[0035] The message distribution module is used to distribute the packaged transaction messages to the corresponding service nodes through the message queue;

[0036] The transaction execution module is used to control each service node to asynchronously obtain transaction messages from the message queue, and control each service node to perform corresponding local transaction operations according to the received transaction messages, and record the operation execution results;

[0037] A status confirmation module, used to control the message queue to update the global status of the transaction according to the operation execution results of all service nodes;

[0038] The message compensation module is used to roll back or compensate for failed or partially successful transactions based on the global transaction status updated by the operation execution results and the preset intelligent compensation strategy to ensure the eventual consistency of the transaction;

[0039] The transaction completion notification module is used to confirm the final status of the transaction when all compensation operations for failed or partially successful transactions are completed; and send the final status confirmation result of the transaction to the transaction initiator terminal or related system components for corresponding notification.

[0040] An intelligent terminal includes a memory and one or more programs, wherein the one or more programs are stored in the memory and are configured to be executed by one or more processors, including the method for executing any one of the methods described above.

[0041] A computer-readable storage medium, wherein when instructions in the storage medium are executed by a processor of an electronic device, the electronic device is enabled to execute any one of the methods described above.

[0042] Beneficial effects of the present invention: The present invention provides a distributed transaction processing method, device, intelligent terminal and storage medium based on message queue and compensation strategy. The main workflow of the present invention includes: transaction initiation, message distribution, transaction execution, status confirmation, message compensation, transaction confirmation, etc. The present invention can achieve the final consistency of distributed system data through message queue and message compensation mechanism, which provides convenience for users. BRIEF DESCRIPTION OF THE DRAWINGS

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

[0044] Figure 1 It is a flow chart of a distributed transaction processing method based on a message queue and a compensation strategy provided in Example 1 of the present invention.

[0045] Figure 2 It is a flow chart of a distributed transaction processing method based on a message queue and a compensation strategy provided in Example 2 of the present invention.

[0046] Figure 3A principle block diagram of an embodiment of a distributed transaction processing device based on a message queue and a compensation strategy provided by the present invention.

[0047] Figure 4 It is a block diagram of the internal structure principle of the intelligent terminal provided by an embodiment of the present invention. DETAILED DESCRIPTION

[0048] In order to make the purpose, technical solution and advantages of the present invention clearer and more specific, the present invention is further described in detail below with reference to the accompanying drawings and examples. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not used to limit the present invention.

[0049] Conventional distributed transaction processing methods, such as two-phase commit (2PC) and three-phase commit (3PC), can ensure the principle and consistency of transactions, but they have performance bottlenecks and insufficient fault tolerance when facing distributed-specific problems such as network partitions and service node failures. In addition, these methods often require complex coordination and synchronization mechanisms when processing cross-service or cross-database transactions, resulting in large resource consumption and long response time, making it difficult to meet the requirements of modern distributed systems for high availability and high throughput.

[0050] The present invention provides a distributed transaction processing method based on message queue and compensation strategy, such as Figure 1 As shown, a distributed transaction processing method based on a message queue and a compensation strategy according to Embodiment 1 of the present invention comprises the following steps:

[0051] Step S100: Initiate a transaction request across multiple services or databases according to the distributed transaction processing requirements, encapsulate the transaction request into one or more transaction messages, and attach necessary transaction metadata;

[0052] In the embodiment of the present invention, a distributed transaction refers to a transaction executed on multiple services (such as microservices) or databases. Since these services may be distributed in different physical or logical locations, the interaction between them needs to deal with complexity and consistency issues.

[0053] The present invention needs to identify which services or databases need to participate in the transaction according to specific business requirements, and will initiate a transaction request across multiple services or databases according to distributed transaction processing requirements.

[0054] In the embodiment of the present invention, the transaction request is converted into a transaction message in a transmittable and reliable manner, and the transaction message can be received and processed by multiple services.

[0055] The present invention encapsulates the transaction request into one or more transaction messages. Such encapsulation may include request parameters, operation type, target service information, etc., making the message more descriptive and processable.

[0056] Regarding the addition of necessary transaction metadata, specifically, metadata is data about data. In an embodiment of the present invention, transaction metadata may include information such as transaction ID, status of participant services, timestamp, operation sequence, etc. These metadata enable each service to understand and manage the execution status of the transaction, ensuring coordination and consistency in the distributed system.

[0057] In this step embodiment, by using transaction messages and metadata, it is possible to ensure consistency even in a distributed system, that is, all service data is either successfully updated or not updated. In addition, each step of the transaction is encapsulated and recorded, and the execution status of the transaction can be tracked through metadata, which is particularly important when problems occur.

[0058] Furthermore, the step S100 specifically includes:

[0059] S101. According to the distributed transaction processing requirements, control the user terminal or system component to initiate a transaction request across multiple services or databases;

[0060] In this step embodiment, regarding the distributed transaction processing requirements, specifically in a distributed system, business operations often need to span multiple services or databases, and these services may exist in different servers or networks, forming a distributed architecture. In this architecture, if an operation needs to ensure that all services involved are executed successfully, distributed transaction processing is required.

[0061] Regarding the control of user terminals or system components, specifically, the user terminal may be an application program interface, and the system component may be a backend service or microservice. The present invention needs to design appropriate interfaces and logic to ensure that these requests can be effectively triggered when operations need to be performed across multiple services.

[0062] S102: Encapsulate the transaction request into one or more transaction messages, and attach necessary transaction metadata, wherein the transaction metadata includes: transaction ID, operation type, and participating nodes.

[0063] In this step, the distributed transaction request is converted into a message that can be transmitted in the network. These messages can be delivered through message queues, HTTP requests or other communication mechanisms. And add necessary transaction metadata including: transaction ID: uniquely identifies the ID of the transaction, which is used for subsequent tracking and management; operation type: describes the operation of this transaction, such as creating an order, updating inventory, etc., to help each service understand the specific operation it needs to perform; participating nodes: list the information of all services or databases that will participate in the transaction, so that each service is clear about its role and responsibilities.

[0064] It can be seen that in the embodiment of the present invention, by controlling the user terminal or system component to initiate a transaction request, the integrity of the request can be guaranteed. This facilitates the subsequent steps to detect a service failure, and the entire transaction can be rolled back through the coordination mechanism, thereby maintaining data consistency.

[0065] In addition, the present invention enables the system to manage and monitor transactions more easily by encapsulating requests and attaching metadata. The present invention can track the status of each step according to the transaction ID to ensure that information is not lost when processing complex operations. In addition, the use of message transmission allows different services to maintain a certain degree of independence. Each service only needs to pay attention to the messages it receives without directly calling other services. This design makes the system easier to maintain and more scalable.

[0066] The present invention can also improve performance and reliability because by asynchronously processing transaction requests, a response can be returned immediately after receiving the request without waiting for all services to complete the operation. This approach improves the user experience and allows the system to flexibly handle failures when they occur.

[0067] Step S200: Distribute the encapsulated transaction message to the corresponding service node through the message queue;

[0068] In the previous step, the transaction request has been encapsulated into a message, containing the necessary data and meta information. This message is now ready to be sent over the network to the services that need to participate in the transaction.

[0069] This step distributes the encapsulated transaction message. This process involves sending the encapsulated message to the target service node, which ensures that each participating service can receive the relevant request and start executing its corresponding operation.

[0070] The present invention distributes to corresponding service nodes through message queues, which are an asynchronous communication mechanism for sending messages between different applications or services. It can help decouple, buffer and persist messages to manage requests safely and efficiently.

[0071] The present invention uses message queues to effectively separate different services. Services do not need to call each other directly, so that the components of the system can be independently upgraded and expanded, reducing the tight coupling between services. In addition, message distribution supports asynchronous processing, and the business logic can respond to user requests quickly without waiting for all services to complete their operations. This asynchronous feature improves the user experience of the system. This can also improve reliability, because if a service fails when processing a request, the message queue can store unprocessed messages for retrying later, which can effectively avoid request loss.

[0072] Furthermore, the step S200 specifically includes:

[0073] S201, sending the packaged transaction message to a message queue, where the message queue is responsible for storing and distributing the transaction message;

[0074] In this step, the previously encapsulated transaction message (including business operation information and metadata) is sent to the message queue. This queue acts as an intermediary, responsible for storing and distributing these messages to various services.

[0075] Among them, the role of the message queue, which can be RabbitMQ, Apache Kafka, etc., is responsible for receiving, storing and forwarding messages to the service modules that need to consume these messages.

[0076] S202, the message queue stores the encapsulated transaction message persistently to ensure that the message will not be lost when the system fails;

[0077] In the embodiment of the present invention, the encapsulated transaction message is stored persistently, which means that the message is saved in a queue in a safe way to prevent the message from being lost in the event of a system failure or crash. This usually includes writing the message to a database or disk storage to ensure data security.

[0078] In this way, persistence ensures that even in the event of hardware failure, software crash, or network problem, messages can be reprocessed after recovery, thus ensuring the reliability of the system.

[0079] S203: The message queue distributes the transaction message to corresponding service nodes according to the participating node information in the transaction message.

[0080] In this step, the message queue distributes the message to the corresponding service node according to the participating node information specified in the transaction message, and ensures that each service can receive the message it needs to process through a specific algorithm (such as polling, random distribution, label mechanism, etc.).

[0081] Participating node information: This information helps the message queue identify the target services, such as inventoryService, paymentService, and orderService, so that the message can be delivered accurately.

[0082] The present invention sends the encapsulated transaction message to the message queue and realizes the persistent storage and targeted distribution of the message, thereby ensuring data reliability and enhancing flexibility, maintainability and scalability. This design makes full use of the advantages of the message queue and helps to build a stable and efficient distributed system.

[0083] Step S300: Each service node asynchronously obtains a transaction message from a message queue, controls each service node to perform a corresponding local transaction operation according to the received transaction message, and records the operation execution result;

[0084] In this step, each service node obtains messages from the message queue asynchronously, that is, the service will not wait for other services to complete processing. Each service obtains and processes messages according to its own processing speed, thereby improving the responsiveness of the overall system.

[0085] In the embodiment of the present invention, regarding message acquisition, each service node monitors the message queue regularly or according to events, and once a new message is available, it will asynchronously acquire and start processing. This approach ensures independence and flexibility between services.

[0086] In this step, each service node is controlled to perform corresponding local transaction operations. Specifically, local transaction operations refer to executing specific local transactions according to the message content (such as operation type and data details) once the service node successfully obtains the transaction message. This involves database operations such as inserting, updating or deleting records.

[0087] Regarding the operation execution result recording in this step, specifically, after each service node completes the local transaction operation, it is necessary to record the execution result of the operation, including whether the operation is successful, error information (if any), and related timestamps. Such records are helpful for subsequent transaction management and fault recovery.

[0088] For example, taking the order process of an e-commerce platform as an example, the specific execution process is as follows:

[0089] Message publishing: After a user places an order on the shopping platform, a transaction message is sent to the message queue, which contains the user ID, order details, payment information, etc.

[0090] Asynchronous processing of service nodes: Setting: inventoryService: responsible for updating inventory. paymentService: responsible for processing payments. orderService: responsible for creating orders.

[0091] Asynchronously obtain and process messages: inventoryService: Regularly monitor the message queue to detect new transaction messages.

[0092] After receiving the message, parse the product information.

[0093] Update the inventory and record the result. If successful, record "Inventory update successful"; if failed, record the error message.

[0094] paymentService: After detecting the same message, parse the payment information.

[0095] To process the payment, call the payment gateway’s API.

[0096] Record the successful payment status or error information (such as payment failure, etc.).

[0097] orderService: After receiving the message, it creates the order and saves it to the database.

[0098] Records the success or failure of order creation.

[0099] The embodiment of the present invention enables each service to work independently through asynchronous processing without waiting for responses from other services, thereby improving the throughput and user experience of the entire system. In addition, each service records the results of the operation execution, which facilitates subsequent error handling and tracking. If an error occurs in the operation of a service, the fault can be quickly located and handled.

[0100] The present invention obtains transaction messages asynchronously, and each service node performs local transaction operations according to the message content, which not only improves the performance and fault tolerance of the system, but also ensures the decoupling and flexibility between services. This design is crucial for building an efficient and scalable distributed system, making it more manageable and controllable when processing complex transactions.

[0101] Furthermore, the step S300 specifically includes:

[0102] S301, each service node asynchronously obtains a transaction message from a message queue.

[0103] In this step, each service node independently obtains transaction messages from the message queue asynchronously. At this time, the service node does not need to block other operations, but can start processing immediately when a new message arrives. This method supports concurrent processing of multiple services and improves the efficiency of the overall system.

[0104] In the embodiment of the present invention, the service node is set to a monitoring mode to continuously check whether there are new transactions in the message queue to be processed. This allows each node to maintain flexibility in processing logic and to respond quickly to transaction requests.

[0105] It can be seen that the present invention enables multiple services to process messages at the same time through an asynchronous mechanism, significantly improving the response speed and processing capacity of the system. For example, when multiple users place orders at the same time, inventory, payment and order services can process their respective transactions at the same time. In addition, each service node can dynamically obtain tasks as needed, better manage and utilize system resources, and avoid resource waste caused by passive waiting. In addition, the present invention interacts between services through message queues, reducing the dependency of direct calls, which enables each service to be independently deployed and upgraded, enhancing the overall flexibility of the system.

[0106] S302, controlling each service node to perform corresponding local transaction operations according to the received transaction message;

[0107] In this step, when the service node obtains the transaction message, it will execute the relevant local transaction according to the information provided in the message, such as updating inventory, processing payment or recording order. This operation usually interacts directly with the database of each node.

[0108] The present invention enables service nodes to ensure that their local states are consistent with the business state of the entire system through clear operation types and data in the message content.

[0109] In this way, this step binds the specific operations of each service to its responsibilities, clearly defines the role of each service in the business process, and is very conducive to the understanding and maintenance of the system. In addition, each service node is responsible for handling specific local transactions, and any errors only affect the status of the service and will not spread to other services, thereby improving the robustness of the system.

[0110] Furthermore, when a problem occurs in a service, since its operation is independent, the present invention can quickly locate the relevant service for debugging without analyzing the behavior of the entire system.

[0111] S303, and control each service node to record the operation result, including success, failure or abnormal status.

[0112] In the embodiment of the present invention, each service node will record the result of the operation after completing the local transaction, including whether the operation is successful, the reason for failure or abnormal status. This record is stored in the service log system or a database table related to the business.

[0113] Recording operation results can not only be used to monitor the performance and stability of services, but also support subsequent auditing and analysis work, which plays a key role in data consistency and fault recovery. In addition, the present invention retains detailed operation records, which can help track problems, analyze the causes of failures, and evaluate the performance of the system under various loads. Moreover, by recording the results of successful and failed operations, transaction compensation strategies can be better implemented in subsequent steps. When the operation of a service fails, retry logic or compensation transactions can be implemented in subsequent steps by recording information to ensure the effectiveness of the entire business process.

[0114] It can be seen that through the design of the above three steps, the present invention realizes an efficient, reliable and traceable transaction processing mechanism. Each service node not only improves the performance and robustness of the system by asynchronously obtaining messages, executing local transactions and recording operation results, but also provides strong support for subsequent monitoring, debugging and fault recovery. This design pattern helps to build an efficient, flexible and scalable distributed system.

[0115] Step S400: the message queue updates the global status of the transaction according to the operation execution results of all service nodes;

[0116] In the embodiment of the present invention, each service node feeds back the operation execution result to the message queue; the message queue updates the global state of the transaction according to the feedback from all service nodes.

[0117] In the embodiment of the present invention, each service node feeds back the operation execution result to the message queue. Specifically, after completing its local transaction operation, each service node of the present invention returns the execution result (such as success or failure status, operation log, error information, etc.) as a feedback message to the message queue. This feedback enables the message queue to understand the status of each operation.

[0118] The feedback message contains key information, such as the service node identifier (service name or ID), related transaction ID, operation status (success or failure), current timestamp, etc. This information makes subsequent processing clearer and more orderly.

[0119] The message queue then updates the global state of the transaction based on the feedback from all service nodes. Specifically, after receiving feedback from each service node, the message queue of the embodiment of the present invention will update the global state of the transaction based on these results. This involves marking the entire transaction as successful, failed, or pending, depending on the results of the feedback. This state determines whether the transaction is completed and how to handle subsequent steps.

[0120] In the embodiment of the present invention, the message queue maintains the consistency of global transactions by uniformly managing and coordinating the feedback of each service node. This mechanism ensures that the entire system can accurately reflect the current business status when processing complex business.

[0121] It can be seen that the feedback mechanism of the embodiment of the present invention records the operation results of each service node, making the operation process of the system transparent and easily tracking the execution progress and status of transactions. And by centrally managing the global state, the message queue can effectively coordinate transactions between different services to ensure that the system can reflect the current consistent state at any time. This is especially important for highly distributed environments.

[0122] Step S500: Based on the global transaction state updated by the operation execution result and the preset intelligent compensation strategy, a rollback or compensation operation is performed on the failed or partially successful transaction to ensure the eventual consistency of the transaction.

[0123] In the embodiment of this step, based on the preset intelligent compensation strategy, the compensation operation is triggered through the message queue for the failed or partially successful transactions in the global state of the transaction updated by the operation execution result; according to the triggered compensation operation, the failed or partially successful transactions in the executed transaction operations are rolled back or compensated to ensure the eventual consistency of the transaction.

[0124] Specifically, the setting of the preset intelligent compensation strategy in the present invention can define the compensation strategy during the system design phase to clarify the compensation measures to be taken when the transaction processing fails or partially succeeds. The compensation strategy of the present invention is based on the business needs, the scope of operation impact and the differences in abnormal situations to formulate a suitable rollback or compensation plan.

[0125] The embodiment of the present invention adopts intelligent decision making, that is, the compensation strategy of the embodiment of the present invention has intelligent characteristics, and selects the most appropriate compensation operation according to the real-time feedback status and execution history. For example, some strategies may select the corresponding number of retries or delay time according to the specific cause of failure.

[0126] In the embodiment of the present invention, the compensation operation is triggered for the failed or partially successful transaction through the role of the message queue, that is, once the global status of the transaction is updated to failure or partial success, a compensation request is sent through the message queue to notify the relevant service node to perform the compensation operation. For example, a message can be sent to a service that is specifically responsible for handling the compensation mechanism.

[0127] In this way, the present invention allows the system to respond quickly when problems occur in the core business process, maintaining the health of the system and the consistency of the business, thereby minimizing the impact on user experience.

[0128] Regarding rollback or compensation operations, specifically, when the service node receives a compensation request, it will roll back or compensate the executed transaction operations according to the pre-set compensation strategy. For example, if a payment fails, you can choose to restore the inventory quantity; if a refund operation is partially successful, you may need to initiate an additional refund operation.

[0129] By implementing rollback or compensation operations, the system state will return to the expected final consistency. Even if some steps fail, the overall business process can still remain healthy and in line with business logic.

[0130] In this way, the present invention, through the compensation mechanism, can maintain the final consistency of the entire transaction even when an operation fails, and avoid data inconsistency and business logic confusion. And the preset compensation strategy can be adjusted according to different business scenarios, so that the system can take appropriate countermeasures in various situations, enhancing the flexibility and reliability of the system. Moreover, by immediately triggering the compensation operation through the message queue, it can quickly respond to errors in execution, reduce the island state of the system, and improve the efficiency of operations. Furthermore, for users, timely remediation and recovery can significantly enhance users' trust and satisfaction with the system and reduce negative experiences caused by errors.

[0131] The following is a specific application example to further describe the distributed transaction processing method based on message queues and compensation strategies in the embodiment of the present invention:

[0132] The distributed transaction processing method based on message queue and compensation strategy of this specific application embodiment includes the following steps:

[0133] S1. Initiate transaction requests across multiple services or databases. For example, an online e-commerce platform needs to handle a complete shopping process. After a user places an order, multiple services are involved, including inventory management (inventoryService), payment processing (paymentService), and order management (orderService).

[0134] Regarding the encapsulation of transaction requests, the present invention first receives the user's order request, encapsulates the request into a transaction request, including the required product information, user payment information, etc., and adds necessary transaction metadata, such as transaction ID, user ID and timestamp. These metadata are used to track the status of the transaction.

[0135] S2. Then the message is distributed. The present invention distributes through the message queue: the encapsulated transaction message is distributed to three service nodes (inventoryService, paymentService and orderService) through the message queue, and each service node receives the transaction related information it is responsible for processing.

[0136] The benefit of doing this is decoupling, because the various services interact with each other through message queues, which reduces direct dependencies and improves the flexibility of the system.

[0137] S3. Each service node asynchronously obtains the transaction message and performs the operation; specifically, in the embodiment of the present invention, each service node asynchronously obtains the transaction message from the message queue. For example:

[0138] InventoryService deducts inventory based on the message;

[0139] PaymentService processes the payment based on the message;

[0140] orderService creates an order record.

[0141] Recording of operation execution results: After executing an operation, each service node records the execution result (such as success or failure status) and feeds back through the message queue.

[0142] In this way, each node processes transactions independently and in parallel, improving the overall efficiency of the system.

[0143] S4. The message queue updates the global status of the transaction. Specifically, the message queue of the present invention updates the global status of the transaction based on the feedback of all service nodes. For example:

[0144] If the operations of both paymentService and inventoryService succeed, but orderService fails, the global status will be marked as "partially successful".

[0145] In this way, by centrally managing the transaction status through the message queue, you can clearly understand the execution results of the entire transaction, making subsequent processing more convenient.

[0146] S5. Perform rollback or compensation operations based on the global state and compensation strategy; specifically, regarding the initiation of compensation operations, for transactions marked as "failed" or "partially successful", the present invention performs remediation according to a preset intelligent compensation strategy. For example:

[0147] If the paymentService fails, the present invention may try to re-initiate the payment request;

[0148] If the inventory has been deducted but the payment fails, the inventory needs to be restored.

[0149] This can ensure eventual consistency because through the compensation mechanism, even if some service operations fail, the system can still ensure the eventual consistency of the transaction and reduce the risk of data inconsistency.

[0150] The whole process includes:

[0151] User places an order: The order request is encapsulated into a transaction message.

[0152] Message distribution: distributed to various services through message queues.

[0153] Service execution: The service node processes messages asynchronously and records the execution results.

[0154] Status update: The message queue updates the global status of the transaction.

[0155] Compensation processing: Roll back or compensate failed or partially successful transactions based on the global status and compensation strategy.

[0156] Through the above specific application embodiments, the present invention not only realizes cross-service transaction management, but also enhances the decoupling and flexibility of the system through the introduction of message queues. At the same time, the intelligent compensation strategy ensures that in the complex transaction processing process, even if the system faces partial failure, it can still effectively restore and maintain data consistency. This efficient processing mechanism is crucial in the modern microservice architecture, enabling the system to better cope with complex and dynamic business needs.

[0157] In a further embodiment of the present invention, after step S500, it also includes step S600: when all compensation operations for failed or partially successful transactions are completed, the final status of the transaction is confirmed; and the final status confirmation result of the transaction is sent to the transaction initiator terminal or related system components for corresponding notification, so as to remind the transaction initiator in time.

[0158] like Figure 2 As shown, a distributed transaction processing method based on a message queue and a compensation strategy according to a specific application embodiment 2 of the present invention comprises the following steps:

[0159] S21, start, and enter S22;

[0160] S22, system A sends a preapred (ready) message, which is sent successfully, and goes to step S23;

[0161] In the embodiment of the present invention, the transaction initiation includes:

[0162] 1) Transaction request: A user or system component initiates a transaction request across multiple services or databases; 2) Message encapsulation: A transaction request is encapsulated into one or more messages and attached with necessary transaction metadata, such as transaction ID, operation type, participating nodes, etc.

[0163] In the embodiment of the present invention, message distribution includes:

[0164] 2.1) Message sending: The encapsulated message is sent to a central message queue, which is responsible for storing and distributing transaction messages.

[0165] 2.2) Message storage: The message queue stores transaction messages persistently to ensure that the messages are not lost in the event of a system failure.

[0166] 2.3) Message acquisition: The message queue distributes the message to the corresponding service node based on the participating node information in the transaction message.

[0167] 2.4) Asynchronous processing: Each service node asynchronously obtains messages from the message queue and performs corresponding transaction operations.

[0168] S23, local transaction execution, if the execution is successful, go to step S24, if the execution fails, go to step S25;

[0169] In the embodiment of the present invention, each service node performs a local transaction operation according to the received message, and each service node records the operation result, including success, failure or abnormal status.

[0170] S24, sending a confirmation message to MQ (message queue), and then proceeding to step S26;

[0171] S25, sending a rollback message to MQ (message queue), and then proceeding to step S26;

[0172] S26, the message queue processes the confirmation message. If the execution of system B fails, the process goes to step S27; if the execution of system B succeeds, the process goes to step S28;

[0173] S27, the message queue automatically retries, if the retry succeeds, it goes to step S28, if the retry fails, it goes to step S29;

[0174] S28, the local transaction of system B is executed successfully, and the process goes to step S30;

[0175] S29: Check the transaction status and proceed to S30.

[0176] In the embodiment of the present invention, each service node feeds back the operation result to the message queue; and the message queue updates the global state of the transaction according to the feedback from all service nodes.

[0177] S30, perform message compensation, if it is executed according to the predetermined intelligent compensation strategy, then go to step S31, if manual intervention is required, then go to step S32;

[0178] S31, execute according to the predetermined intelligent compensation strategy, and after the execution is completed, enter step S33;

[0179] In the embodiment of the present invention, if a transaction fails or partially succeeds, the intelligent compensation strategy determines the best compensation operation based on historical data and the current system status. In addition, regarding the message queue triggering the compensation operation, the executed operation will be rolled back or compensated to ensure the final consistency of the transaction.

[0180] S32, receiving a manual intervention instruction for execution, and after the manual intervention instruction is executed, proceeding to step S33;

[0181] In the embodiment of the present invention, once all compensation operations are completed, the final state of the transaction is confirmed, and then the result notification is performed, and the final result of the transaction is notified to the transaction initiator or related system components.

[0182] S33, end.

[0183] Exemplary Devices

[0184] like Figure 3 As shown, an embodiment of the present invention provides a distributed transaction processing device based on a message queue and a compensation strategy, the device comprising:

[0185] The transaction initiation module 310 is used to initiate a transaction request across multiple services or databases according to the distributed transaction processing requirements, and encapsulate the transaction request into one or more transaction messages and attach necessary transaction metadata;

[0186] The message distribution module 320 is used to distribute the packaged transaction message to the corresponding service node through the message queue;

[0187] The transaction execution module 330 is used to control each service node to asynchronously obtain transaction messages from the message queue, and control each service node to execute the corresponding local transaction operation according to the received transaction message, and record the operation execution result;

[0188] A status confirmation module 340 is used to control the message queue to update the global status of the transaction according to the operation execution results of all service nodes;

[0189] The message compensation module 350 is used to roll back or compensate for failed or partially successful transactions based on the global transaction status updated by the operation execution result and the preset intelligent compensation strategy to ensure the eventual consistency of the transaction;

[0190] The transaction completion notification module 360 ​​is used to confirm the final status of the transaction when all compensation operations for the failed or partially successful transaction are completed; and send the final status confirmation result of the transaction to the transaction initiator terminal or related system components for corresponding notification, as described above.

[0191] Based on the above embodiments, the present invention further provides an intelligent terminal, whose principle block diagram can be shown as follows: Figure 4 As shown. The intelligent terminal includes a processor, a memory, a network interface, a display screen, and a database connected through a system bus. Among them, the processor of the intelligent terminal is used to provide computing and control capabilities. The memory of the intelligent terminal includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and a computer program. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The network interface of the intelligent terminal is used to communicate with an external terminal through a network connection. When the computer program is executed by the processor, a distributed transaction processing method based on a message queue and a compensation strategy is implemented. The database of the intelligent terminal is used to store a distributed transaction processing program based on a message queue and a compensation strategy.

[0192] Those skilled in the art will understand that Figure 4 The principle block diagram shown in the figure is only a block diagram of a partial structure related to the scheme of the present invention, and does not constitute a limitation on the smart terminal to which the scheme of the present invention is applied. The specific smart terminal may include more or fewer components than those shown in the figure, or combine certain components, or have a different arrangement of components.

[0193] In one embodiment, a smart terminal is provided, comprising a memory and one or more programs, wherein the one or more programs are stored in the memory and are configured to be executed by one or more processors, and the one or more programs include instructions for performing the following operations:

[0194] Initiate a transaction request across multiple services or databases according to the distributed transaction processing requirements, encapsulate the transaction request into one or more transaction messages, and attach necessary transaction metadata;

[0195] Distribute the encapsulated transaction messages to the corresponding service nodes through the message queue;

[0196] Each service node asynchronously obtains transaction messages from the message queue, controls each service node to perform corresponding local transaction operations according to the received transaction messages, and records the operation execution results;

[0197] The message queue updates the global status of the transaction according to the operation execution results of all service nodes;

[0198] Based on the global state of the transaction updated by the operation execution result and the preset intelligent compensation strategy, the failed or partially successful transactions are rolled back or compensated to ensure the eventual consistency of the transactions, as described above.

[0199] The steps of initiating a transaction request across multiple services or databases according to the distributed transaction processing requirements, encapsulating the transaction request into one or more transaction messages, and attaching necessary transaction metadata include:

[0200] According to the distributed transaction processing requirements, control the user terminal or system component to initiate a transaction request across multiple services or databases;

[0201] The transaction request is encapsulated into one or more transaction messages, and necessary transaction metadata is attached, wherein the transaction metadata includes: transaction ID, operation type, and participating nodes.

[0202] The step of distributing the encapsulated transaction message to the corresponding service node through the message queue includes:

[0203] Send the packaged transaction message to a message queue, which is responsible for storing and distributing transaction messages;

[0204] The message queue stores the encapsulated transaction messages persistently to ensure that the messages are not lost in the event of a system failure.

[0205] The message queue distributes the transaction message to the corresponding service node according to the participating node information in the transaction message.

[0206] The steps of each service node asynchronously obtaining a transaction message from a message queue, controlling each service node to perform a corresponding local transaction operation according to the received transaction message, and recording the operation execution result include:

[0207] Each service node asynchronously obtains transaction messages from the message queue.

[0208] Control each service node to perform corresponding local transaction operations according to the received transaction message;

[0209] And control each service node to record the operation results, including success, failure or abnormal status.

[0210] The step of updating the global status of the transaction according to the operation execution results of all service nodes includes:

[0211] Each service node feeds back the operation execution result to the message queue;

[0212] The message queue updates the global status of the transaction based on feedback from all service nodes.

[0213] The steps of performing a rollback or compensation operation on a failed or partially successful transaction based on the transaction global state updated by the operation execution result and the preset intelligent compensation strategy to ensure the final consistency of the transaction include:

[0214] Based on the preset intelligent compensation strategy, the compensation operation is triggered through the message queue for the failed or partially successful transactions in the global status of the transaction updated by the operation execution result;

[0215] According to the triggered compensation operation, the failed or partially successful transactions in the executed transaction operations are rolled back or compensated to ensure the eventual consistency of the transaction.

[0216] Wherein, the step of performing a rollback or compensation operation on a failed or partially successful transaction based on the transaction global state updated by the operation execution result and the preset intelligent compensation strategy to ensure the final consistency of the transaction also includes:

[0217] When all compensation operations for failed or partially successful transactions are completed, the final status of the transaction is confirmed;

[0218] The final status confirmation result of the transaction is sent to the transaction initiator terminal or related system components for corresponding notification, as described above.

[0219] Those skilled in the art can understand that all or part of the processes in the above-mentioned embodiment methods can be completed by instructing the relevant hardware through a computer program, and the computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to memory, storage, database or other media used in the embodiments provided by the present invention can include non-volatile and / or volatile memory. Non-volatile memory can include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM) or flash memory. Volatile memory can include random access memory (RAM) or external cache memory. As an illustration and not limitation, RAM is available in many forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate SDRAM (DDRSDRAM), enhanced SDRAM (ESDRAM), synchronous link (Synchlink) DRAM (SLDRAM), memory bus (Rambus) direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM).

[0220] In summary, the present invention provides a distributed transaction processing method, device, intelligent terminal and storage medium based on message queues and compensation strategies. In the present invention, through the compensation mechanism, even when a certain operation fails, the system can still maintain the final consistency of the entire transaction, avoiding data inconsistency and business logic confusion. And the preset compensation strategy can be adjusted according to different business scenarios, so that the system can take appropriate countermeasures in various situations, enhancing the flexibility and reliability of the system. Moreover, by immediately triggering the compensation operation through the message queue, it is possible to quickly respond to errors in execution, reduce the island state of the system, and improve the efficiency of operations. Furthermore, for users, timely remediation and recovery can significantly enhance users' trust and satisfaction with the system, reduce negative experiences caused by errors, and provide convenience for users.

Claims

1. A distributed transaction processing method based on message queue and compensation strategy, characterized in that: include: Initiate a transaction request across multiple services or databases according to the distributed transaction processing requirements, encapsulate the transaction request into one or more transaction messages, and attach necessary transaction metadata; Distribute the encapsulated transaction messages to the corresponding service nodes through the message queue; Each service node asynchronously obtains transaction messages from the message queue, controls each service node to perform corresponding local transaction operations according to the received transaction messages, and records the operation execution results; The message queue updates the global status of the transaction according to the operation execution results of all service nodes; Based on the global transaction status updated by the operation execution results and the preset intelligent compensation strategy, rollback or compensation operations are performed on failed or partially successful transactions to ensure the eventual consistency of the transaction.

2. The distributed transaction processing method based on message queue and compensation strategy according to claim 1, characterized in that: The steps of initiating a transaction request across multiple services or databases according to the distributed transaction processing requirements, encapsulating the transaction request into one or more transaction messages, and attaching necessary transaction metadata include: According to the distributed transaction processing requirements, control the user terminal or system component to initiate a transaction request across multiple services or databases; The transaction request is encapsulated into one or more transaction messages, and necessary transaction metadata is attached, wherein the transaction metadata includes: transaction ID, operation type, and participating nodes.

3. The distributed transaction processing method based on message queue and compensation strategy according to claim 1, characterized in that: The step of distributing the encapsulated transaction message to the corresponding service node through the message queue includes: Sending the packaged transaction message to a message queue, where the message queue is used to store and distribute transaction messages; The message queue stores the encapsulated transaction messages persistently to ensure that the messages are not lost in the event of a system failure. The message queue distributes the transaction message to the corresponding service node according to the participating node information in the transaction message.

4. The distributed transaction processing method based on message queue and compensation strategy according to claim 1, characterized in that: The steps of each service node asynchronously obtaining a transaction message from a message queue, controlling each service node to perform a corresponding local transaction operation according to the received transaction message, and recording the operation execution result include: Each service node asynchronously obtains transaction messages from the message queue. Control each service node to perform corresponding local transaction operations according to the received transaction message; And control each service node to record the operation results, including success, failure or abnormal status.

5. The distributed transaction processing method based on message queue and compensation strategy according to claim 1, characterized in that: The message queue updates the global state of the transaction according to the operation execution results of all service nodes, including: Each service node feeds back the operation execution result to the message queue; The message queue updates the global status of the transaction based on feedback from all service nodes.

6. The distributed transaction processing method based on message queue and compensation strategy according to claim 1, characterized in that: The steps of performing a rollback or compensation operation on a failed or partially successful transaction based on the transaction global state updated by the operation execution result and the preset intelligent compensation strategy to ensure the final consistency of the transaction include: Based on the preset intelligent compensation strategy, the compensation operation is triggered through the message queue for the failed or partially successful transactions in the global status of the transaction updated by the operation execution result; According to the triggered compensation operation, the failed or partially successful transactions in the executed transaction operations are rolled back or compensated to ensure the eventual consistency of the transaction.

7. The distributed transaction processing method based on message queue and compensation strategy according to claim 1, characterized in that: The step of performing a rollback or compensation operation on a failed or partially successful transaction based on the transaction global state updated based on the operation execution result and the preset intelligent compensation strategy to ensure the final consistency of the transaction also includes: When all compensation operations for failed or partially successful transactions are completed, the final status of the transaction is confirmed; The final status confirmation result of the transaction is sent to the transaction initiator terminal or related system components for corresponding notification.

8. A distributed transaction processing device based on message queue and compensation strategy, characterized in that: The device comprises: A transaction initiation module is used to initiate a transaction request across multiple services or databases according to the distributed transaction processing requirements, encapsulate the transaction request into one or more transaction messages, and attach necessary transaction metadata; The message distribution module is used to distribute the packaged transaction messages to the corresponding service nodes through the message queue; The transaction execution module is used to control each service node to asynchronously obtain transaction messages from the message queue, and control each service node to perform corresponding local transaction operations according to the received transaction messages, and record the operation execution results; A status confirmation module, used to control the message queue to update the global status of the transaction according to the operation execution results of all service nodes; The message compensation module is used to roll back or compensate for failed or partially successful transactions based on the global transaction status updated by the operation execution results and the preset intelligent compensation strategy to ensure the eventual consistency of the transaction; The transaction completion notification module is used to confirm the final status of the transaction when all compensation operations for failed or partially successful transactions are completed; and send the final status confirmation result of the transaction to the transaction initiator terminal or related system components for corresponding notification.

9. An intelligent terminal, characterized in that: The device comprises a memory and one or more programs, wherein the one or more programs are stored in the memory and are configured to be executed by one or more processors, and the one or more programs include being used to execute the method according to any one of claims 1 to 7.

10. A computer-readable storage medium, characterized in that: When the instructions in the storage medium are executed by a processor of an electronic device, the electronic device is enabled to execute the method as described in any one of claims 1 to 7.

Citation Information

Cited By

  • Message-oriented middleware high-availability data transmission method and system

    CN121603544A