Search code examples
microservicesdddd

Microservice and service collaboration


In the context of a Microservice architecture, a single business operation can require collaboration between two or more services.

Suppose we have an Order Management Service and a Product Catalog Service. When the user adds an order item to an order, the Order Management Service will persist a OrderItem object which have the following attributes among many others :

OrderItem 
+ Id
+ ProductId
+ ProductName 

In order for the Order Management Service to fill the ProductName attribute, we have 4 choices as I see it :

Choice 1 : ProductName is given by the client app as it probably already has this data from previous requests

Choice 2 : If the architecture uses an Api Gateway, the gateway will be responsible for retrieving the ProductName from the Product Catalog Service then provide it to the Order Management Service.

Choice 3 : The Order Management Service will call directly the Product Catalog Service and asks him for the ProductName givent a product id.

Choice 4 : The Order Management Service has a duplicate (but not exhaustive) product informations in its own database and these datas will be updated each time an update event is received from the Product Catalog Service.

Among these 4 choices, the n°1 seems not ok to me as we can't trust the client to give us a correct and updated value of ProductName.

I would like to have your opinion about what you think the best choice is and why !

Thanks !

Riana


Solution

  • Choice 1 : ProductName is given by the client app as it probably already has this data from previous requests

    Like you said, it is not the best idea because the client may have stale information. Maybe acceptable if the product information changes infrequently and/or you have a secondary verification at order processing.

    Choice 2 : If the architecture uses an Api Gateway, the gateway will be responsible for retrieving the ProductName from the Product Catalog Service then provide it to the Order Management Service.

    IMHO, this is not a good idea. By doing so your domain/business logic will leak into the API Gateway. The gateway now knows the relationship between Orders and Products. This API gateway configuration/scripting will need to be maintained and introduces additional coupling.

    Choice 3 : The Order Management Service will call directly the Product Catalog Service and asks him for the ProductName givent a product id.

    This is my preferred choice. Though I do not recommend "direct" synchronous calls. Perhaps a retrieval of the ProductName via a messaging mechanism (message queue, event bus). Chained synchronous calls will reduce the availability of your services. You can find more information at Microservice Messaging Pattern.

    Choice 4 : The Order Management Service has a duplicate (but not exhaustive) product informations in its own database and these datas will be updated each time an update event is received from the Product Catalog Service.

    Data duplication is generally frowned upon unless there is a really good reason for it. In this case I don't see any. Why bother splitting the databases into two for each of the services yet duplicate the data between them? Also, to have the data updated each time an update event is received indicates that some kind of event/messaging infrastructure is available, in that case, why not just use messaging?

    This duplication may be justifiable for high volume, low latency look ups, but it is a slippery slope that may end up with duplicated data all over your services. Imagine the repercussions of a length or type/format change of the ProductName string...