dotnet-architecture / eShopOnContainers

Cross-platform .NET sample microservices and container based application that runs on Linux Windows and macOS. Powered by .NET 7, Docker Containers and Azure Kubernetes Services. Supports Visual Studio, VS for Mac and CLI based environments with Docker CLI, dotnet CLI, VS Code or any other code editor. Moved to https://github.com/dotnet/eShop.
https://dot.net/architecture
24.58k stars 10.36k forks source link

How to handle transactions among multiple microservices? #1173

Closed abolfazlmohammadiseif closed 4 years ago

abolfazlmohammadiseif commented 4 years ago

Hi I have multiple microservices. I update a record within microservice A then send an event to microservice B and do another update in microservice B. if the second update fails, I need the first one to be roll-backed. how should I handle such a transaction? Thanks in advance

mvelosop commented 4 years ago

Hi @abolfazlmohammadiseif, that's an interesting question!

It's hard to answer that question in general, because, as usual, it all depends on the context.

But, in general, I'd say that looks a lot like a distributed transaction, and a candidate for Two Phase Commit. If that's the case, then it's probably not a good idea to have those DB in separate microservices, because the compensatory actions for such scenario could get quite complicated.

You might find the following sections from the Microservices Architecure guide interesting for this topic:

Hope this helps.

mvelosop commented 4 years ago

Closing this issue now but feel free to comment, will reopen if needed.

abolfazlmohammadiseif commented 4 years ago

Closing this issue now but feel free to comment, will reopen if needed. Thank you very much. I'm working on your suggestions.