MQ on achieving eventual consistency principle to resolve distributed transactions

This article describes Ali cloud final official document on the realization of the principle of consistency in distributed transactions MQ

 

Concepts

  • Transaction message: message queue MQ provides similar functionality distributed transaction X / Open XA, the transaction message through a message queue MQ to achieve a final uniform distributed transaction.

  • Semi transaction message: temporarily unable to deliver the message, the sender has successfully send messages to the message queue MQ server, but the server does not receive the confirmation message of the secondary producers, in which case the message is marked "Temporarily not be delivered "state, the message in the message transaction states i.e. half.

  • Message Recall: due to network glitches, restart the application producers and other reasons, leading to secondary confirmation of a transaction message bar loss, message queue MQ server by scanning a message was found in a long-term "semi-transaction messages", you need to take the initiative final interrogation message producer status of the message (the Commit or Rollback), i.e., the query message back search process.

Applicable scene

Application scenario example transaction message:

Through the process in a single cart, the user entry in the shopping cart system, the single entry transactions in the trading system, the data between the two systems consistent with the need to maintain the final, then the message can be processed by the transaction. Under the system after a single transaction, the transaction sends a single message to the message queue MQ, shopping cart system under subscription MQ message queue trading single message, do the appropriate business processes, data update shopping cart.

Interactive process

Transaction message interaction flow as shown in FIG.

Transaction message

Transaction message transmission steps:

  1. Semi transaction message sender to send the message queue MQ server.

  2. After the end of the message queue MQ message persistence service successfully, returns an acknowledgment message Ack has been sent successfully to the sender, then the message is half transaction message.

  3. The sender begin local business logic.

  4. The sender submitted the results of the local terminal of the secondary transaction confirmation to the service (the Rollback or a Commit), then the server receives status half Commit transaction messages marked as delivered, the subscriber will eventually receive the message; server receives Rollback status is deleted and a half transaction message, the subscriber will not accept the message.

Transaction message back to check steps are as follows:

  1. In special cases off the network or the application is restarted, the secondary 4 submitted to confirm the final step of the service does not reach the end of a fixed time after the end of the service initiation message to the message back check.

  2. After the sender receives a message back to check, you need to check the final results of the implementation of the corresponding local transaction message.

  3. The second confirmation sender submitted the final state of the local transaction to check again obtained, the server still follow step 4 for half transaction message.

Precautions

  1. Group ID Group ID transaction message can not be shared with other types of messages. Different messages and other types of transaction messages have to check back mechanism, check back to the message queue MQ server queries the client will go according to Group ID.

  2. Through  when creating a transaction message Producer must specify the  implementation class, transaction processing back to check messages in exceptional cases. ONSFactory.createTransactionProducer LocalTransactionChecker

  3. After completion of the local transaction transaction message is sent, you can  return to a state following three methods: execute

    • TransactionStatus.CommitTransaction: Commit the transaction, allows subscribers to consume the message.

    • TransactionStatus.RollbackTransaction: Roll back the transaction, the message will be discarded allowed to consume.

    • TransactionStatus.Unknow: Temporarily unable to determine the status, the server message queue MQ message to the sender waits for a fixed time after the check back.

  4. To set the fastest time of the first message back to check each message in the following ways:

     
    1. Message message = new Message();
    2. // 在消息属性中添加第一次消息回查的最快时间,单位秒。例如,以下设置实际第一次回查时间为 120 秒 ~ 125 秒之间
    3. message.putUserProperties(PropertyKeyConst.CheckImmunityTimeInSeconds,"120");
    4. // 以上方式只确定事务消息的第一次回查的最快时间,实际回查时间向后浮动 0 秒 ~ 5 秒;如第一次回查后事务仍未提交,后续每隔 5 秒回查一次

Guess you like

Origin www.cnblogs.com/zhangfengshi/p/11665772.html