2

Intro

Hey, my question is kind of hard to explain so I apologize in advance.

Question

I'm trying to implement microservices for our ecommerce and I'm having issues on how to respond to a request when the actual logic and data needs to be determined by other ( 2-3 ) services.

In order to make it easier to understand, I'll give an example.

Lets say User A is trying to buy a product. after clicking on "check out" button these steps should happen.

Flow

Request comes in:

  • Ecommerce service:
    1. Check if product has enough quantity in inventory.
    2. Publish an event indicating a new order has been created. order:created
  • Anti Fraud service:
    1. Receives order:created and checks whether the user is a fraud or not
    2. Publishes an event indicating the check was successful. check:succeed
  • Payment Service:
    1. Receives check:succeed and creates a url to the gateway.
    2. Sends the gateway url to the user, possibly publishing payment:created event. (( this is where my question is ))

Since all of these steps are asynchronous, how do I respond to the Request?

Possible Solution

After the user has requested to checkout, the ecommerce service creates an order and responds immediately with the orderId of newly created order, on client-side the user has to request periodically and check whether the status of order is PENDING PAYMENT, in order to achieve this, the payment service needs to publish payment:created after the order has been approved by the system and then ecommerce service can update the order.

My solution works, but I'm really new to microservices and I want to know if there's a better way to implement this.

I really appreciate if you read this far, Thank you for your time.

2
  • 1
    Not having much time, therefore just a comment: grab a copy of "Enterprise Integration Patterns" (which you should do anyway if you dabble in microservices) and look up request-response pattern. – mtj Nov 14 '20 at 6:41
  • @mtj Thank you for the suggestion, my Building Microservices: Designing Fine-Grained Systems book is on the way. I will definitely read Enterprise Integration Patterns as well. – typicallearner Nov 14 '20 at 9:11
2

Your solution is definitely good enough. It is same as the one recommended by book Microservices patterns. You would also need to implement a saga to roll-back each of the services in case of the order being rejected.

Another thing would be to think if the flow could be made in parallel. Unless there is data dependency between the services, it would make things faster if the Request service queried the 3 services at the same time, and then aggregated the results in the order they come in.

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.