microservicescqrseventual-consistency

Microservices, CQRS: Eventual consistency vs Strong consistency(Read after Write consistency)


Using CQRS and Event store the choreography between microservices delivers an Eventual consistency where in the changes in one microservice take a bit to propagate to the other downstream systems(essentially other microservices) which are related. What are the options if the data is so critical that both the microservices should have a strong consistency for the data? One option that i can think of is a write through Cache like a data grid but that would be very fragile specially in a distributed system.


Solution

  • In such scenario, think about C.A.P. Theorem. According to Wikipedia, "the CAP theorem states that in the presence of a network partition, one has to choose between consistency and availability. Note that consistency, as defined in the CAP theorem, is quite different from the consistency guaranteed in ACID database transactions."

    Since you have 2 microservices, so your system definitely needs to be partition tolerant and you are left with either A (Availability) or C (Consistency). If you want to go with C, then your system will suffer in availability terms. When a request comes into Microservice A, then you should not send back a success message to the client until A gets back a response from Microservice B that data has been successfully stored. This way you can achieve consistency by sacrificing availability.