Tyler Shao
Tyler Shao

Reputation: 156

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?

Upvotes: 4

Views: 1749

Answers (2)

Mequrel
Mequrel

Reputation: 727

As you use events to communicate between contexts, simply publish the Product_NotSold and roll back the transaction when you get this event.

However, you cannot provide 'atomic' transaction in this way. It more a long running process (a.k.a. saga). If you really want atomicity, you need to use two-phase commit and abandon events.

Upvotes: 1

plalx
plalx

Reputation: 43718

There's actually a pattern for this called Saga.

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

http://nservicebus.com/Sagas.aspx

Upvotes: 5

Related Questions