design-patternsmicroservicesorchestration

Price and discount calculation in microservices


I would like to ask about your ideas for pricing in microservices architecture.

Let's assume that the product has its standard price, which may be lower depending on whether the client is business or individual, whether there is a promotion for a given product and what conditions a given promotion has. For example, 30% discount promotion if there are> 3 products from the "A" category in the cart.

As you can see, the price can be lowered depending on many conditions.

Knowing this and that our data is in many databases - how to approach the calculation of the current price for a given customer?

Do you have an idea, design patterns?

Thanks in advance!


Solution

  • Knowing this and that our data is in many databases

    There is a pattern called database per service. So it looks like you are using this pattern. And further this documentation says:

    Keep each microservice’s persistent data private to that service and accessible only via its API. A service’s transactions only involve its database.

    So if these conditions of lowering price are located in different databases:

    then it is possible to create API that provides a way to get data from these services. And then in your service you can set desired price based on this information

    UPDATE:

    According to pattern DatabasePerSerivce you should have separate API for each service. E.g. client service should have own API, promotion service should have own API. And your price service should get data from these services and make a desicion what price should be set.

    Your services should contain business logic that belongs them. How can is it possible to divide logic acrsoss services? It is possible to apply single responsibility principle of SOLID principles. Read more about "Pattern: Decompose by business capability" in microservices.