简体   繁体   中英

How to manage Data consistency between two microservices?

I have two microservices, eg M1 and M2. M1 is responsible for managing user transactions eg orders. When an order is completed, the summary data is sent to M2 via Message bus. M2 is reponsible for generating reports on orders. Our transactions completes without checking if the message was processed successfully or not by M2. The problem is that some orders does not appear in the reports as the messages are not processed successfully(because of any Random Issue). What is the best way to make the data consistent between the two services. I am implementing a mechanism to pull data from M1 and identifying the gaps using the reference numbers(Its is a sequencial number) which I know is not a good approch as I may not know the last reference number that I have is actually the last. Any suggestions or improvements will be highly appreciated. Thanks.

I have tried a pull data mechanism but I do not think that a good idea.

you may take a look at SAGA Pattern .
Saga goal is to form a Service span Transaction with capability of Compensate transaction.
also you can use two phase commit pattern 2PC but it is not recommended mostly since it is resource span transaction which means it occupies resource until end of transaction and thats not good unless you insist immediate transaction which is short enough to release the resources soon as possible.

back to saga, you may hear the term called routing Slip. the routing slip forms chained steps to fulfill the transaction over services. this routing slip can be implemented in two ways of choreography and orchestration. in case of failure happened in any steps the compensation will triggered for all steps that are done. this compensate may be a rollback or any other strategy that should take place. eg:

  1. order added
  2. the inventory item allocated
  3. shipping service proceeded with error and compensate take place as
  4. the allocated item in inventory released
  5. order removed or canceled

i use Masstransit .
read SAGA State Machine where the steps in transaction are coordinated and updates the state for the transaction
read Courier Routing Slip where the routing slips for transaction are defined.
watch Masstransit Serries tut by Chris Patterson

Also, please consider retry policy in case of failure occurred. it can be done with Masstransit configuration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM