I have two microservices, e.g M1 and M2. M1 is responsible for managing user transactions e.g 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.
CodePudding user response:
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 :
- order added
- the inventory item allocated
- shipping service proceeded with error and compensate take place as
- the allocated item in inventory released
- 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.