Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Domain Driven Design - Atomic transaction across multiple bounded context

In DDD, I understand that Events can decouple the Bounded Contexts when they communicate with each others. Assume an atomic transaction contains two database operations on seperated bounded contexts A and B. When operation on A finishes it sends and event which is handled by B which finishes second operation. However, how does operation on A rolls back if operation on B failed?

For example, I am currently designing a system using Domain Driven Design. It contains a Membership and an Inventory bounded contexts. In order to decouple the contexts, I use Events: when an order is being paid, Inventory context will reduce the quantity of the sold product, and send an Product_Sold event. the event is then handled by Membership context which then substracts the user's balance based on the price of the sold product.

However if the user balance update failed due to database failure, how does Inventory context know it so that it can roll back the previously reduced product quantity?

like image 511
Tyler Shao Avatar asked Oct 31 '25 13:10

Tyler Shao


1 Answers

There's actually a pattern for this called Saga.

http://vasters.com/clemensv/2012/09/01/Sagas.aspx

http://nservicebus.com/Sagas.aspx

like image 52
plalx Avatar answered Nov 04 '25 21:11

plalx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!