Volume 12 · Chapter 5 — Principal Software Architect Interview Mastery & Enterprise System Design Handbook
Distributed Transaction Design
5.1 Introduction
Distributed transactions - keeping data correct across multiple services or databases - are among the most probed interview topics because they force a clear understanding of consistency trade-offs. The key realization is that traditional ACID transactions do not span service boundaries at scale, so distributed systems trade immediate global consistency for availability and loose coupling, managed by patterns like the saga. This chapter covers the models and patterns and, crucially, when to use each.
5.2 ACID vs BASE
ACID (Atomicity, Consistency, Isolation, Durability) is the guarantee of a single-database transaction - strong and simple to reason about, but hard to provide across distributed systems. BASE (Basically Available, Soft state, Eventual consistency) is the pragmatic alternative for distributed systems: prioritize availability and accept that consistency is reached over time. The architect chooses per data set - ACID where correctness of the latest value is non-negotiable, BASE where scale and availability matter more.
5.3 Two-Phase Commit and Its Limits
Two-phase commit (2PC) coordinates a distributed transaction via a coordinator that first asks all participants to prepare, then tells them to commit only if all agreed. It provides strong consistency but at a heavy cost: it is slow (synchronous, multiple round trips), it couples participants tightly, and it blocks - if the coordinator fails after prepare, participants hold locks indefinitely. These properties make 2PC a poor fit for scalable microservices.
sequenceDiagram autonumber participant C as Coordinator participant A as Service A participant B as Service B C->>A: prepare C->>B: prepare A-->>C: ready (locks held) B-->>C: ready (locks held) Note over C,B: Coordinator crash here = participants block, holding locks C->>A: commit C->>B: commit
5.4 The Saga Pattern
The saga replaces the distributed ACID transaction with a sequence of local transactions, each publishing an event that triggers the next, and compensating actions that semantically undo prior steps on failure. It trades immediate consistency for availability and loose coupling - the standard choice for microservices. Choreography (services react to events) suits simple flows; orchestration (a coordinator drives steps) suits complex, long-running ones needing visibility.
sequenceDiagram autonumber participant O as Orchestrator participant Pay as Payment participant Inv as Inventory participant Ship as Shipping O->>Pay: charge Pay-->>O: ok O->>Inv: reserve Inv-->>O: ok O->>Ship: schedule Ship--xO: FAILED O->>Inv: compensate (release) O->>Pay: compensate (refund)
5.5 Supporting Patterns: Outbox, Idempotency, CQRS/ES
Sagas rely on reliable eventing: the transactional outbox makes a state change and its event atomic (solving the dual-write problem), and idempotent consumers make at-least-once delivery safe. CQRS and event sourcing often accompany them - the event log is a natural fit for the saga’s events and the audit trail. Together these are the toolkit for distributed data correctness without 2PC.
How do you handle distributed transactions in a microservices architecture?
I avoid distributed ACID transactions - they do not scale across services - and instead embrace eventual consistency managed by the saga pattern plus supporting mechanisms. A business operation that spans services becomes a sequence of local transactions, each committing in its own service and publishing an event that triggers the next step, with compensating actions that semantically undo prior steps if a later one fails (refund a charge, release a reservation). To make the eventing reliable I use the transactional outbox so each service’s state change and its outgoing event commit atomically in one local transaction, eliminating the dual-write problem where a state change succeeds but the event is lost or vice versa. Because delivery is at-least-once, I make every consumer idempotent - keyed by a business id - so redelivered messages do no harm. For simple two- or three-step flows I use choreography (services react to each other’s events); for complex, long-running, or business-critical flows that need visibility, timeouts, and clear compensation, I use an orchestration-style saga with an explicit coordinator tracking state. I design the user experience around the resulting eventual consistency - showing “processing” until the whole flow completes rather than pretending it is instant and globally consistent. I’d explicitly name the trade-off: I’m giving up immediate global consistency in exchange for availability, loose coupling, and scalability, which is the right call across service boundaries, while keeping any invariant that truly must be atomic inside a single aggregate/service so it stays strongly consistent. The combination - sagas with compensation, outbox, idempotency, and per-aggregate strong consistency - is how you get correct distributed data without the blocking, tight coupling, and poor scalability of two-phase commit.
When would you avoid two-phase commit, and why?
I avoid two-phase commit in scalable, loosely coupled distributed systems - which is most modern microservice architectures - because its costs outweigh its guarantee there. 2PC does provide strong, immediate consistency across participants, but it achieves that by being synchronous and blocking: the coordinator asks all participants to prepare and hold locks, then commits only if all agree, so it requires multiple round trips (slow, hurting latency and throughput), it tightly couples the participants and the coordinator (undermining service independence), and - most dangerously - it blocks on failure: if the coordinator crashes after participants have prepared, those participants hold their locks indefinitely, unable to safely proceed, which can cascade into wider unavailability. It also scales poorly as the number of participants grows and does not work well across heterogeneous systems or over unreliable networks. So I avoid it wherever availability, scalability, and loose coupling matter more than immediate global consistency, and I use the saga pattern (local transactions with compensation) with the outbox and idempotency instead, accepting eventual consistency. I would consider 2PC only in narrow cases: a small number of tightly coupled participants within a controlled boundary, short-lived transactions, and where the business genuinely cannot tolerate any eventual-consistency window and the scale is modest - for example some within-datastore or within-trusted-boundary scenarios. But for a distributed microservices platform at scale, the blocking, coupling, and availability risks make 2PC the wrong default, which is exactly why the industry moved to sagas and eventual consistency.
Explain the difference between choreography and orchestration sagas, and how you choose.
Both implement a saga - a sequence of local transactions with compensation - but they coordinate it differently. In a choreography saga there is no central coordinator: each service listens for the events of the previous step and reacts, publishing its own events for the next, so the workflow is distributed and emergent. It is elegant and decentralized for simple flows, avoids a single point of control, and keeps services loosely coupled, but as the flow grows the overall business process becomes implicit - smeared across many services - making it hard to understand, debug, monitor, and change, and compensation logic becomes scattered. In an orchestration saga a central orchestrator explicitly drives the steps, sending commands to each service and tracking the saga’s state, including timeouts and compensations. This makes the workflow explicit and visible in one place - far easier to monitor, reason about, modify, and handle complex branching and failure - at the cost of introducing a coordinator that must itself be made resilient and must not become a bottleneck or a hidden coupling point. I choose based on complexity and visibility needs: choreography for simple flows of two or three steps where the reactions are obvious and decentralization is a virtue; orchestration once the flow has multiple steps, branching, timeouts, or is business-critical and needs clear operational visibility - such as a payment-settlement or order-fulfillment saga. In an interview I’d note that the deciding factor is how much the process benefits from being explicit and centrally observable versus staying simple and decentralized, and that many real systems use choreography for local reactions and orchestration for the important end-to-end business processes.
5.6 Banking Transaction Scenario
A bank’s cross-account transfer illustrates the choice. It cannot use 2PC across the account services without blocking and coupling them, so it runs as an orchestrated saga: the debit commits locally and publishes an event via the outbox, the credit is applied as its own local transaction, and if the credit fails a compensating refund restores the source - all idempotent by transfer id. The event log doubles as the audit trail. Strong consistency is preserved within each account aggregate; eventual consistency spans the transfer, with the UX showing “processing” until both legs complete.
Chapter Revision Notes
- ACID transactions do not span services at scale; choose ACID vs BASE per data set.
- 2PC gives strong consistency but is slow, tightly coupled, and blocking - avoid it in scalable microservices.
- Sagas replace distributed transactions with local transactions plus compensation.
- Choreography for simple flows; orchestration for complex, long-running, visible ones.
- Sagas rely on the outbox (atomic state+event) and idempotent consumers.