Search code examples
microservices

How to handle REST call from UI to API in microservices


We have started implementing our monolith to microservices architecture. We have figured out even driven message queues are durable than REST APIs. However, we are stuck with a basic question.

For example from our website, a user creates an order this goes as an HTTP request to our API gateway and request is pushed to queues. Some operation continues and the result is pushed back to queue.

Now I cant hold this rest API request for long. we are having no clue on implementing this. Any help is greatly appreciated. How can we turn sync HTTP request API call to async response?


Solution

  • User interactions should not be going to queue. User request for a product, it has to be fulfilled. No two ways about it. Queue on the other hand can help you manage backend flows where user is not actively involved. Now order is tricky, there is lot that happens in the backend when user places an order. But all you need to is to generate an order id and rest of the things can be delayed.

    Now for designs, we can place order first and then see how we can fulfil this, if we can good, else we write back to the user. It is not a very good experience for the user if sees his orders being cancelled, but if you can ensure less cancellation this works well. You generated orderid on the fly and push the request with orderID to be processed later.

    To keep it simple, you need to think of critical things you need to do for an order. (This needs to be trimmed down to bare minimum) and you do it sync or if you put it to queue but make sure you ensure maxTimeInQueue and pop out these request and check on the status to respond back to the user. While placing an order you get to a phase where you accept payment details, and this is one place that give you some time to do quick check on things like inventory, etc. Again you need to look into your own flow, start with sync calls and then start eliminating calls that are not required and trim down extra ms on certain calls. Queues can be avoided as long as user is the loop.

    Technically all your request goes into a queue, if http request goes into a queue from where they are picked up. You can leverage that if you really need to use queues by removing stuff which make processing slow, but having timeouts, where you fail fast and by re looking at the order(flow) journeys