Real-World Production Deep Dives: Distributed Transactions

โš ๏ธ Not yet source-checked. These case studies summarize public engineering posts and talks, and some details may be simplified or wrong. Before you repeat a specific number or design in an interview, check it against the company's own write-up. Found an error, or a source that confirms a section? Add it to the tracking issue. Sourced fixes are the most welcome contribution here.

Deep-dive architectural dissections of how Google and Uber/Netflix execute distributed transactions, comparing Google's two-phase locking snapshot isolation (Percolator & Spanner) with the asynchronous Saga Pattern.


๐Ÿข Deep Dive 1: How Google Uses Distributed Transactions (Percolator: Distributed ACID on Bigtable)

1. Production Context & Scale

2. The Core Problem at Google Scale

3. Google's Architecture: The Percolator Two-Phase Protocol

[ Percolator Client (Transaction Coordinator) ]
             โ”‚
      โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
      โ–ผ                                 โ–ผ
[ Primary Lock Row ]           [ Secondary Lock Rows ]
  โ”œโ”€โ”€ Step 1: Prewrite Primary   โ”œโ”€โ”€ Step 2: Prewrite Secondaries
  โ”‚   (Acquires data lock)       โ”‚   (Point to Primary Lock)
  โ”‚                              โ”‚
  โ–ผ                              โ–ผ
[ Commit Primary Row ] โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€> [ Commit Secondary Rows ]
(Sets commit timestamp)          (Removes locks; writes commit record)

A. The Timestamp Oracle (TSO)

B. Primary Lock and Decentralized Coordination

Instead of relying on an external transaction coordinator that could become a single point of failure:

  1. Prewrite Phase:
    • The client designates one cell as the Primary.
    • It writes the data and places a lock on the primary cell with its start_timestamp.
    • It then writes data and locks on all Secondary cells. Crucially, each secondary lock contains an explicit pointer to the primary lock cell!
  2. Commit Phase:
    • The client gets a commit_timestamp from the TSO.
    • It commits the Primary cell by converting the lock into a permanent commit record. The transaction is now officially committed!
    • The client asynchronously converts the secondary locks into commit records.

C. Crash Recovery without Central Locks

What happens if the client crashes halfway through?


๐Ÿš™ Deep Dive 2: How Uber & Netflix Use Sagas (Cadence / Temporal Orchestration)

1. Production Context & Scale

2. The Core Problem: Why Two-Phase Commit (2PC) Fails in Microservices

3. Uber's Architecture: Orchestrated Saga Pattern with Temporal

[ Rider Clicks "Book Trip" ]
             โ”‚
             โ–ผ
[ Temporal / Cadence Saga Orchestrator ]
  โ”œโ”€โ”€ Manages State Machine & Persistent Execution History
  โ”‚
  โ”œโ”€โ”€ Step 1: Dispatch DispatchService.assignDriver() โ”€โ”€> [ OK ]
  โ”œโ”€โ”€ Step 2: Dispatch PaymentService.authorizePayment() โ”€> [ OK ]
  โ”œโ”€โ”€ Step 3: Dispatch NotificationService.sendSMS() โ”€โ”€โ”€> [ FAILED! ]
  โ”‚
  โ–ผ (Compensation Triggered in Reverse Order!)
[ Compensating Transaction Flow ]
  โ”œโ”€โ”€ Step 2Comp: PaymentService.refundAuthorization() โ”€โ”€> [ REVERSED ]
  โ””โ”€โ”€ Step 1Comp: DispatchService.releaseDriver() โ”€โ”€โ”€โ”€โ”€โ”€โ”€> [ REVERSED ]
             โ”‚
             โ–ผ
[ User Notified: "Booking Failed; No Charges Incurred" ]

A. Sagas: Local Transactions & Compensations

B. Orchestration vs Choreography


โš–๏ธ Comparative Architecture Matrix: 2PC vs Saga

Architectural Dimension Two-Phase Commit (Google Spanner / Percolator) Saga Pattern (Uber Temporal / Netflix)
Consistency Guarantee Strict ACID (Immediate Consistency) Eventual Consistency (BASE)
Locking Semantics Holds distributed database locks across phase No cross-service locks; local commits only
Failure Recovery Rollback leaves database as if nothing happened Compensating transactions clean up committed data
Latency & Throughput High latency; sensitive to slowest participant High throughput; decoupled asynchronous execution
Best Used For Financial balances, stock trading, internal DBs Multi-microservice business workflows, orders

๐ŸŽฏ System Design Interview Takeaways

  1. Never Propose 2PC for Microservices: If an interviewer asks how to handle a distributed transaction across independent microservices (e.g., Order Service -> Payment Service -> Inventory Service), explicitly reject 2PC due to lock contention and propose the Saga Pattern.
  2. Orchestrated vs Choreographed Sagas: Recommend Orchestrated Sagas for complex multi-step workflows to maintain visibility, centralize timeout management, and prevent event-loop deadlocks.
  3. Idempotency in Compensating Actions: Explain that all compensating transactions must be strictly idempotent. If a network glitch occurs during a refund step, the orchestrator will retry the refund call, which must not result in double refunds!