microservicescoupling

Coupling in microservices architecture


When working on an application in microservices architecture I stumbled upon issues concerning coupling between services.

This a typical application for ordering products. It seams reasonable to have a service that will operate as a product catalog. Our JavaScript client can ask this service for available products and show them in browser. When user clicks on a product we have to create an order. We would like to manage orders in another service. So an order is created - it means that user X ordered product Y. On the technical level we are simply persisting user id and product id in a database.

To sum up we have:

Products service

Product class:
Product ID, Product Name

Orders service

Order class:
Order ID, Product ID, User ID, Order date

Now let's imagine following scenario - in JavaScript client we would like to list all products that user have ordered. Orders service provides a list of products ids for a given user. But user cares about product name, not the id. Unfortunately Orders service doesn't know anything about products names.

We could tackle this in couple of ways:

Could any of these approaches be considered best practice? Or are there different solutions?


Solution

  • In the eShopOnContainers example( https://github.com/dotnet-architecture/eShopOnContainers ), they add the following fields at the time an order item is created:

            public void AddOrderItem(int productId, string productName, decimal unitPrice, decimal discount, string pictureUrl, int units = 1)
    

    This duplicates the catalog information, however as this is a point in time snapshot of the record. This is far safer than linking to the original data.

    At this point in the user journey you are in the domain of an order, if a user is viewing an order you can provide a link to the catalogue. However, the order service should be able to operate independently. Linking back to the catalogue for the information on products prevents the Order service owning it's own data.

    A nightmare scenario being that a price changes in the catalogue... What would happen to the historic orders?