Q22 · When would you use distributed transactions vs eventual consistency?
Back to 24 Architecture Questions.
The question is not a technology pick. The axis it turns on: whether the business can operate on a state that is briefly inconsistent. Two-phase commit and eventual consistency are two answers to the same failure: a step fails after another committed. One refuses the intermediate state outright; the other accepts it and cleans up by policy.
What the answer should contain
Know what a distributed transaction actually costs. Two-phase commit holds locks on every participant across network round-trips, makes the coordinator a failure domain, and turns one participant’s crash into everyone’s waiting. Throughput falls to the speed of the slowest, most locked participant. It is atomic, and the price is a system-wide pause on every failure.
Eventual consistency is not “no consistency”. It is a policy: the service that owns the fact commits alone, consumers converge, and the window of inconsistency is measured, bounded, and cleaned up by compensation where money is involved. The cost is designing cleanup instead of preventing the window: compensations, tokens that make a user’s next read see their own write, and a stated staleness tolerance per class.
The decision is the business’s, per invariant, not per system. “Can the order show before the charge confirms?” is a product question with a recorded answer. Most invariants tolerate seconds of ambiguity; the few that do not are usually money and stock, and they deserve a different pattern entirely.
For the invariants that cannot be briefly inconsistent, move the invariant, do not distribute it. The strongest pattern: give the fact one owner inside one service, where a plain local transaction holds it. Sell the seat atomically in one place, then converge everything else by events. Distributed transactions are usually the answer to an invariant that was allowed to span two services by accident, when it belonged to one owner.
The reservation pattern is the middle road. Reserve locally and atomically (one owner), confirm the rest asynchronously, release on timeout. The user-facing answer is correct at every instant; the distributed part is convergence, not correctness.
Ideas worth mentioning out loud
- “Two-phase commit prevents the intermediate state; eventual consistency prices it. The question is which invariants can afford the price.”
- The 2PC instinct is usually a boundary smell: if two services must commit atomically, ask why the fact lives in two places at all.
- Eventual is not an apology when it is a policy: lag measured, compensations designed, windows bounded. It is an apology exactly when none of those exist.
Follow-ups to expect
- When is 2PC actually justified? Small participant counts, one administrative domain, low contention, and a genuinely atomic business rule. Rare, but not never.
- How do you converge after a failure? Events from the log of the service that owns the fact, consumers replaying from their own cursor; the window is visible, not hidden.
- What breaks eventual? Charging without checking stock because “it usually converges in time”: the one invariant that cannot be briefly wrong, routed through the pattern that assumes it can.
Red flags in an answer
- 2PC as the default integration mechanism.
- “Eventually consistent” with no compensation, no lag metric, and no bound.
- The atomic invariant left spanning services, instead of moved to one owner.
- No recorded business decision about the acceptable windows.
Sources
- CAP Twelve Years Later: How the Rules Have Changed, Eric Brewer, IEEE Computer 2012: the theorem’s own author revising his position. Choose consistency or availability per operation, because both are expensive; the business-decides framing of this answer is Brewer’s, applied to a narrower question.
- A Critique of the CAP Theorem, Martin Kleppmann, 2015: the argument that CAP is too coarse to make consistency decisions with, which supports the per-invariant conclusion and undermines any answer that ends with the letters C, A, and P.