1

I'm building a e-commerce with DDD and Event Sourcing, CQRS. My ideia is separate each AR in a microservice.

In my AR ShoppingCart I need a VO Item with productId and a Price, because price doesn't change after add to the cart.

I have another AR Product that control the price.

My problem is, how get the Price from AR Product without a synchronous request to the Product since I'm using a event architecture?

2 Answers2

0

Fundamentally, what you are trying to do is copy information from one aggregate root to the other.

There are two approaches you might take.

One is to think in terms of a cache - we pass to the shopping cart an instance of a domain service that knows how to take a correlation id (product code?) and get a cached copy of a price. So we have a background process that copies pricing information from the pricing micro service to the shopping cart micro service, and then the autonomous shopping cart relies on its locally cached copy of the price.

Important note: there's nothing wrong with including timeliness metadata in the cache, so that the sharping cart can include intelligence about whether or not the cached information is "too old".

The other is more direct - have a method by which you can send a command with the price to the shopping cart, and build some orchestration logic that observes which shopping carts need prices, then send send a command to the cart with the appropriate price.

VoiceOfUnreason
  • 52,766
  • 5
  • 49
  • 91
0

If you have two microservices, you can have each microservice publish a stream of events. Your ShoppingCart microservice can consume PriceChanged events from your Product microservice and maintain a local cache of the last price per Product. When you add a Product to a ShoppingCart, you would reference the local cache of prices.

This same approach of listening to events as a means of communication scales up from inter-Aggregate to inter-BoundedContext or inter-Microservice and even inter-System. Depending on your sensitivity to price changes, you might have to employ other approaches as described, but I assume you have some tolerance to eventual consistency given your choice in the CQRS+ES pattern.

CPerson
  • 1,222
  • 1
  • 6
  • 16