2 ms·
This is a hard problem to tackle. An obvious approach is 2-phase commit or N-phase commit. 2PC and NPC are generally not recommended because it sacrifices perf
by namelosw 7y ago
This is a hard problem to tackle.
An obvious approach is 2-phase commit or N-phase commit. 2PC and NPC are generally not recommended because it sacrifices performance too much.
* Pros: It is more like a transaction so that you don't have to write rollback logic.
* Cons: It seriously sacrifices system availability since the distributed transaction happens across networks.
A commonly recommended approach is Saga, it's basically compensation.
* Pros: Does not sacrifices system availability since it's an asynchronous/optimal design.
* Cons: Have to write a lot of compensation logic and call them correctly.
* Cons: There are a lot of operations that cannot be compensated. These operations should be arranged in the last steps.
* Cons: What happens if compensation fails?
* Cons: It's eventual consistency, it's not strong atomicity.
I have to admit, I don't try to tackle this problem unless it's really important for the business in a microservices environment.
If your business requires atomicity in most of the places, it's highly recommended to have a carefully designed monolith so you can easily benefit from RDBMS, with your domains modeled in different modules instead of services.
- c89X 7y agoThanks, really appreciate the pros and cons here. To be clear, and I understand the confusion: I'm actually building more or less a monolith with a RDBMS to encapsulate most of the business logic of my problem domain. However, for various parts of my application that seem high-complexity, high-impact and non-competetive edge (such as authentication and credit card handling) I want to outsource to external services (Auth0 and Stripe). This brings with it the challenge of managing state, and consistency across various external services (on top of the state I manage internally). The reason microservices is mentioned in the question, is because I believe these problems generalise from my specific case, to microservices.