Implement a distributed transaction-based message queue

Implement a distributed transaction-based message queue

Scenes:

Order system to produce orders, shopping cart system minus the cart business.

Realization of ideas:

  1. Order system open a transaction on a message queue (do not create an order).
  2. Order system to the message server sends a "message and a half", this message is not to say that half the message content is not complete, what it contains is complete message content, the only difference between a half and a common message is the message, before the transaction commits, for consumers , this news is not visible.
  3. Semi message sent successfully, the order system can perform local transactions, and order records in order to create a library and database transaction orders submitted to the library.
  4. Then according to the results of local affairs decided to commit or roll back the transaction message. If the order is successfully created, then commit the transaction news, shopping cart system ready to spend this message to continue the follow-up process. If the order creation fails, then roll back the transaction news, shopping cart system will not receive this message.

Orange and green are the two transactions.

problem:

Step 4 transaction commit failure; this time the order has been submitted to the local transaction system Seoul cart system does not receive the message, resulting in inconsistent data.

How to solve the message queue transaction commit process appears abnormal:

kafka directly thrown user discretion;

In the transaction implementation RocketMQ in, increasing the transaction reverse lookup mechanism to solve the problem of failure to submit transaction message, RocketMQ the Broker does not receive a request to submit or rollback, anti-Broker will go to regularly check on this transaction corresponds to a local producer state of affairs, and then decided to commit or roll back the transaction according to anti-check the results.

In order to support the transaction pegging mechanism, our business code needs to implement a reverse lookup interface of the local state of affairs inform RocketMQ local transaction success or failure.

Reverse Lookup logic of the present embodiment is simple only in the order according to the message ID, Query whether the order can be present in the library in order.

Can you send orders to a message queue in order to create complete before? So do not consider the case of failure to send the order to create a message

Consider a case: order creation success, not enough time message, the node suddenly loses power.

In another case the order is successfully created, minus cart (follow-up operation) fails to write the kind compensation order creation deleted.

Can this:

1. Turn on local affairs create orders, 2. message, 3. depending on whether the message successfully to decide whether to commit or roll back the local transaction. This eliminates the need transaction message can also solve the problem of this scenario?

If the local transaction commit failure message sent can not be withdrawn, lead to inconsistent data.

summary

Whether the message queue transaction or asynchronous transaction follows the affairs of the four properties: atomicity, consistency, isolation, durability.

More ways to implement distributed transaction: blog.csdn.net/ityqing/art...


**** If the code word is not easy to have to help you Give me a concern ****

**** love life love technology QQ group: 894 109 590 ****

Guess you like

Origin juejin.im/post/5dcbd8fa6fb9a060150afe07