I know eventual consistency, but I would like to know it by simple and practical problem.
Assumptions:
Assuming these tables:
Table microservice_A.ACCOUNT { int AccountId, string AccountName } Table microservice_B.CUSTOMER { int CustomerId, int AccountId, string CustomerName }My assumption here is that the two have a one-to-one relationship:
micro_B micro_A ---------- --------- |CUSTOMER|-1----------1-|ACCOUNT| ---------- ---------Every the
micro_Bandmicro_Ahave own separated database.Business policies:
- Every
CUSTOMERmust have anACCOUNT. - The
ACCOUNThas its own policies insideMicro_A(Aggregate Factory). - The
CUSTOMERhas its own policies insideMicro_B(Aggregate Factory).
- Every
I have
CreateCustomernamed use case that is one of the software requirement and it cause to create a customer with a valid account.
The Question
1- Which microservice is responsible for the implementation of CreateCustomer?
----- I think it is micro_B.
2- How does work the CreateCustomer, What are the best practices here?
One option is to insert a
CUSTOMERwith nullAccountIdat first. Second publish an event byMicro_Bto catch it with 'Micro_A'. Third, Create anACCOUNTand generate newAccountIdand publish another event by 'Micro_A' that notify theMicro_Bthat account has been generated and give it the new generatedAccountId. Forth, TheMicro_Bgets theAccountIdand it updates itsCUSTOMERrecord with proper value.Another option is to using
Rest ApiorgRPCbetween them. in this way communication is synchronized. Also if an error occures on insertingCUSTOMERthere is no compensation solution to revertACCOUNTcreation.
I am using 'dotnet' stack. If possible, please give examples of the tools you provide in your solution.
Thank you.
you have to implement SAGA pattern to handle your transaction events among services.
you can use Masstransit to implement SAGA in your project.
read MassTransit Saga State Machine and read MassTransit Courier.
question-1: since you have mentioned the word CreateCustomer, it implies that the customer service is responsible to start and coordinate the transaction and i assume that someone as the operator (not the customer/user himself) creates the customers. but if user/customer himself registers with account and in follow customer profile is created then i think it could be the account service.
question-2: First i have to say that the MassTransit State Machine does coordinate and manage the transactions and the MassTransit Courier forms a Routing Slip with capability of compensate the transaction.
Second regardless of which service is responsible to start the transaction, i think its better to insert customer record then hold the customer Id with the account.(eg: user login with account and get his/her customerId as a claim so you could retrieve relevant data). here i assume that the customer service is responsible to coordinate the transaction.
ofcorse the above scenario is general and not specific to exact implementation.
you can find awesome videos series on masstransit here