I always think about this scenario where we need to perform a task conditionally and the task requires calling another microservice. What I am not able to understand is which microservice's responsibility is to evaluate the condition?
Let us take an example:
There is a dedicated inventory microservice. The system has certain conditions that decide whether inventory is applicable to a product or not. The conditions look like:
- The inventory is not applicable to the whole system.
- The inventory is not applicable to a particular sales channel like the app.
- The inventory is not applicable to a particular product. (Like services, non-tangible products).
The services that require inventory information may be, for example,
- Shopping Cart microservice requiring services of the Inventory microservice for reservations
- Catalog microservice requiring in-stock or out-of-stock status of the products
- Order microservice needs to decrement the stock using the Inventory microservice
The question is, what among the below is a better design solution?
- Every microservice evaluates all the conditions and decides whether it needs to hit the inventory microservice or not, thereby saving unnecessary calls to inventory microservice.
- Every microservice hits the inventory microservice and the inventory microservice will decide whether to perform the operation or skip it depending on the conditions. This allows coding the "conditions evaluation" at a single place and cleaner code.