Search code examples
microservices

Transaction management of the microservices


I am new one for the microservices. Can you please explain about the transactional management of the microservices and how is work the foreign keys between the different databases as an example client and order services has their own sevices and databases. If we use single database, order table has a foreign key of client. But databases are different how we put the foreignkeys between this tables? And how we done the transaction management between this services? Thank you


Solution

  • Distributed data management in the world of micro services is one of the most complex problems to solve. However there are patterns and guidelines that help in addressing the issues with distributed data. Some of them are listed below

    1. Define the Bounded context of the micro service - Each microservice must own its domain data and logic. This is where you need to break foreign key dependencies and identify which table you will own and which you will reference.

    2. Eventual consistency over ACID semantics - If there are two services Catalog and Basket. Catalog contains the product details and Basket contains the item purchased by a user currently. Any change in product price should be reflected in items in basket. However since the product data and Basket items are stored in two different databases the price update happens asynchronously. This could be via a publisher subscriber pattern. For example a price change event is published on message bus and services like Basket subscribes to it and updates itself when it receives the event.

    3. Read only data stores using materialized view - This is a form of CQRS pattern where data from multiple services is stored in a de-normalized form in read only data store. The data gets updated asynchronously using the approach mentioned above. The consumers gets fast access to data without having to query multiple services.

    So in summary for CAP theorem microservices world prefer high performance and scalability and availability over strong consistency. The consistency is resolved eventually using asynchronous communication.

    https://learn.microsoft.com/en-us/dotnet/architecture/microservices/architect-microservice-container-applications/distributed-data-management