2

I’m trying to think what’s the best approach when I need data present in different services in a microservices-based architecture. In an e-commerce context, when a user creates an order the OrderService needs to confirm if the sent list of products actually exists and infer if the total price is indeed correct (data validation).

Considering I’m using CQRS, I’ve read that the command side should not query read sides since the read sides are for clients only.

Some people say I should create an event to make ProductService check for the products and publish an event to confirm they do exist and the total price is correct, which the OrderService will listen to and proceed from there.

I’ve also read that the command side should keep a copy of any needed data for operation purposes. If so, should I keep products data replica inside my command side of OrderService? Making the orders service handle every product change events isn’t too out of its scope? It just feels like too much of an overhead, but people say it’s normal in microservices. And what if I’m using event sourcing? Will I have my event store and some SQL product tables to maintain inside the command side?

Plus, after that, I still have to post an event of OrderCreated so the StockService can listen and publish an event of StockAvailable/OutOfStock. In this specific context, the flow of creating an order to be able to pay the order goes through several publications of async events. Wouldn’t it cause a possible bad user experience? I’ve personally never waited on an e-commerce website in order to be able to pay for the products.

I know, CAP theorem, but still! What’s the best approach to have a decent order confirmed-payment flow?

2
  • There is no single best approach. There are good and bad ways to solve specific problems. Every advice you read on some website and even here can be good and bad depending on the context and needs and specific problem to be solved. A micro service architecture solves a specific set of problems in exchange for another set of problems like additional complexity, eventual consistency, service orchestration, etc. Maybe someone will post an answer and it might be a good way to solve your issue, but in reality you should ask yourself if you really need this complex micro service architecture. – Rik D Mar 23 at 20:14
  • @RikD well, I think this is just an example of a common problem in every microservices architecture? Very rarely a service only needs its own data to operate, and there are definitely situations that demand some sort of stimulus/immediate response for user experience sake. I do need to use microservices, I'm conducting a study for my master thesis. I mentioned possible solutions but I'm not happy with any of them... – Leandro Costa Mar 23 at 20:58
1

In some cases, there could be need to orchestrate a number of events.

  1. Get Products (Query)
  2. Item Exists (Validate)
  3. Infer Total Price (Calculation)
  4. Order (Command)

In that case, simply create a service that aggregates/orchestrates the appropriate steps whether they be queries or commands.

There is still separation of commands and queries, and if there was a need just to query products that could still happen.

2
  • As far as I know about best practices in a microservices context, services should not query each other directly. Are you suggesting I create a service that does those steps in a request/response fashion? My project is for an academic context, I need to avoid conflicts with best practices as much as I can. – Leandro Costa Mar 24 at 14:34
  • @Leandro - Yes, the service that is created is an aggregator/orchestrator type of service, that it is calls other micro-services to complete the result. This is an acceptable pattern. The GetProducts and Order Micro service(s) still exist. – Jon Raynor Mar 30 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.