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_B
andmicro_A
have own separated database.Business policies:
- Every
CUSTOMER
must have anACCOUNT
. - The
ACCOUNT
has its own policies insideMicro_A
(Aggregate Factory). - The
CUSTOMER
has its own policies insideMicro_B
(Aggregate Factory).
- Every
I have
CreateCustomer
named 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
CUSTOMER
with nullAccountId
at first. Second publish an event byMicro_B
to catch it with 'Micro_A'. Third, Create anACCOUNT
and generate newAccountId
and publish another event by 'Micro_A' that notify theMicro_B
that account has been generated and give it the new generatedAccountId
. Forth, TheMicro_B
gets theAccountId
and it updates itsCUSTOMER
record with proper value.Another option is to using
Rest Api
orgRPC
between them. in this way communication is synchronized. Also if an error occures on insertingCUSTOMER
there is no compensation solution to revertACCOUNT
creation.
I am using 'dotnet' stack. If possible, please give examples of the tools you provide in your solution.
Thank you.