drizzie
drizzie

Reputation: 3361

How to handle failures in eventual consistency between aggregates in DDD?

Lets say I'm implementing Domain Driven Design using C# and Entity Framework.

My code is structured such that each aggregate has its own dbcontext in EF to respect the principle of transactional boundaries around my aggregates.

Aggregate 1, InventoryAggregate, and Aggregate 2, OrderAggregate, are being updated by some business process, AddItemToOrder.

After OrderAggregate adds the item, it fires a domain event, ItemAddedToOrder that is listened to by InventoryAggregate, who then performs some business process, SubtractQuantityFromInventory.

InventoryAggregate fails to subtract the inventory and it fires a domain event, NotEnoughInventory, listened to by OrderAggregate.

OrderAggregate then attempts to remove the item from the order but fails.

Now there is an item in the order that should not be because we don't actually have enough inventory to sell the item.

How should this be handled?

Upvotes: 0

Views: 737

Answers (1)

Eben Roux
Eben Roux

Reputation: 13256

What you are describing is a Process Manager. You probably need some sort of OrderProcess or QuoteProcess AR that handles the state for you process. If you need to perform some business validation such as checking inventory first then you need to have a process manager so that you only create the actual Order once you have established that it can indeed be submitted.

The rules around what to do with certain items may not be as simple as removing the item even though, in your case, it may be what you need to do. You may need to present the data to the user with one or more options. One may be to remove the item from before submitting the order where another may be to place it on a back order.

Upvotes: 1

Related Questions