2

I have 3 microservices exposing APIs for interaction.

The order microservice receives order from UI and makes an API call to Inventory microservice to update the inventory details to reduce the itemCount.

Once the above operation succeeds, the order microservice will make calls to Payment microservice to initiate payments. If payment succeeds, the order microservice will return success to UI. If payment fails, the order microservice has to rollback the itemCount by calling Inventory microservice.

I know that this is a typical Saga based pattern and can be achieved using messaging bus. But all the microservices in this case are capable of only handling API calls. Thinking about other patterns, I am not sure this scenario fits into a state pattern .

Note: For the sake of example, I have used Order,Payment and Inventory microservices. But typically my question is about orchestrating sequence of API calls without excessive if-else. Failure of one of the API call should rollback the other API call by calling appropriate API (eg: If PATCH call succeeds on service A , but not on service B, then the PATCH call on service A has to be rolled back on Service A)

Rockstart
  • 2,337
  • 5
  • 30
  • 59
  • 1
    You might want to look at Hangfire (https://hangfire.io). It provides durable mechanisms to declare workflows like this – Flydog57 Feb 10 '22 at 15:21

1 Answers1

1

The SAGA pattern is not exclusive to message bus and can be implemented via API calls as well.

The important thing with SAGA is to ensure that all the transactions can be rolled back to the previous state in a controlled manner via a compensation action. So you need an API call to reverse the transaction and in your case restore the items to the inventory.

Another alternative is 2-phase commit. So here your Inventory MS would effectively first Reserve the items, and once payment is successful, then you call it again to confirm and actually book them.

If it is not successful, or if not confirmed within xx minutes (or once every x hours), then you rollback the reservation.

Both patterns have their pros and cons, and the important thing is to select the one which suits your use case best.

jason.kaisersmith
  • 8,712
  • 3
  • 29
  • 51
  • In my case the dependent microservices Inventory and Payment do not support 2-phase commit. Do you have any reference links that show how SAGA can be achieved just using APIs? – Rockstart Feb 10 '22 at 06:38
  • Not that I can share. But the API needs to support compensation transactions, otherwise you can't do anything. – jason.kaisersmith Feb 10 '22 at 06:44