1

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 and micro_A have own separated database.

  • Business policies:

    • Every CUSTOMER must have an ACCOUNT.
    • The ACCOUNT has its own policies inside Micro_A (Aggregate Factory).
    • The CUSTOMER has its own policies inside Micro_B (Aggregate Factory).
  • 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 null AccountId at first. Second publish an event by Micro_B to catch it with 'Micro_A'. Third, Create an ACCOUNT and generate new AccountId and publish another event by 'Micro_A' that notify the Micro_B that account has been generated and give it the new generated AccountId. Forth, The Micro_B gets the AccountId and it updates its CUSTOMER record with proper value.

  • Another option is to using Rest Api or gRPC between them. in this way communication is synchronized. Also if an error occures on inserting CUSTOMER there is no compensation solution to revert ACCOUNT creation.


I am using 'dotnet' stack. If possible, please give examples of the tools you provide in your solution.

Thank you.

Ali Adlavaran
  • 3,697
  • 2
  • 23
  • 47

1 Answers1

2

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.

  1. Create Customer fires an event to SAGA state machine.
  2. SAGA state machine is initialized with customer information including information to register with an account also.
  3. The Saga State machine will execute routing slip which contains 2 steps(1-create customer in customer service,2- register account in account service)
  4. the first step of routing slip is to create the customer. if succeed then go to next step.
  5. we have customer id and register account in customer service with customer id as claim.
  6. if there is an error in registering an account the compensate method will triggered and perform your compensate policies.eg: delete the customer with specific Id

ofcorse the above scenario is general and not specific to exact implementation.
you can find awesome videos series on masstransit here

Rouzbeh Zarandi
  • 1,047
  • 2
  • 16
  • 34