Architecture question - product-based basket rules
Hi,
Suppose that there is a requirement to restrict the users from adding products to the basket based on some product-based rules. For example, product B cannot be added to the basket unless product A is also in the basket, or product A cannot be added unless the user has placed at least X orders in the past. These rules would differ per product. Essentially, each product would be associated with a set of requirements.
The basket service needs to enforce these rules, but where would these rules be defined? In the catalog service or in the basket service?
If they are defined in the catalog service, then they will need to be synced with the basket service. On the other hand, if these rules are defined in the basket service, then when a new product needs to be defined in the catalog, both services would need to be updated.
Thanks!
Put the logic and related data in the microservice/Bounded-Context where you need it. In this case, probably the Basket. The product has a single identity, but its data/logic/behavior must be split across multiple microservices. Same thing for a user. User registry data can be in the Membership/Identity system, but data related to payment discounts, like “status: Gold, Silver, etc.” Must be place in the payment microservice.
So, your second hypothesis is right, when a new product is created/updated, the UI will talk/update against multiple microservices in a “star” communication fashion, the UI talking to multiple
Another example, a user identity is the same, but the user data/logic must be split in multiple entities like: RegisteredUser, Customer, Buyer, etc. depending on what process you are doing in different and autonomous microservices.
Basically, try to Place data/attributes where it is going to be used, not in a central place with “everything” per “traditional entity”.
As exception (try to minimize it), sometimes, a single attribute can be redundant in several microservices. Choose on microservices to be the “master/update” place, and use eventual consistency to propagate that attribute to other microservices by using integration events and a publish/subscription approach. But try to minimize that by placing the attributes where they are used, as I was saying in the first place.
Hope it helps! :)
It makes sense now. Thank you, this was very helpful!