Distributed transaction
One logical operation that changes state across multiple networked participants.
CMPE273 · Module 9
Distributed transactions, sagas, and idempotency explained through online orders, safe retries, and an AI booking agent.
One logical operation that changes state across multiple networked participants.
A workflow of committed local transactions with compensating actions for business recovery.
A new business action that offsets an earlier committed action; it may fail or require intervention.
Repeating the same logical operation preserves the intended effect of a single execution.
Save a state change and its outgoing event in the same local database transaction.
flowchart LR C[Checkout] --> T[Orders DB transaction] T --> O[Order row] T --> L[Line items] C --> P[Remote payment service]
The order and line items share a local commit boundary. Rolling back that transaction does not undo a payment at another service.
flowchart LR O[Order O-42] --> I[Inventory: reserved] I --> P[Payment: charged USD 80] P --> D[Delivery: rejected address] D --> R[Recovery needed]
Delivery failure leaves both a reservation and a charge. Throwing an exception in checkout cannot erase those committed effects.
flowchart TD C[Coordinator] -->|1. Prepare| A[Inventory DB] C -->|1. Prepare| B[Payment ledger DB] A -->|Prepared vote| V[Collect all votes] B -->|Prepared vote| V V --> D[Durably record decision] D -->|2. Commit or abort| A D -->|2. Commit or abort| B
All participants must support the protocol. After prepare, a participant cannot safely guess the decision when the coordinator is unreachable.
flowchart LR I[Reserve stock] --> P[Charge USD 80] P --> D[Book delivery] D -->|Rejected| R[Refund USD 80] R --> S[Release stock] S --> C[Order cancelled]
Successful earlier steps remain committed until separate compensation transactions run. The customer may see recovery pending before cancellation completes.
flowchart TD W[Durable orchestrator] --> I[Inventory] W --> P[Payment] W --> D[Delivery] E[OrderCreated event] --> H[Inventory handler] H --> R[StockReserved event] R --> Q[Payment handler]
The upper blocks show explicit commands from a coordinator. The lower blocks show the start of an event chain without a central workflow driver.
flowchart LR C[Checkout] -->|Charge key O-42-pay| P[Payment commits] P --> X[Response lost] X --> T[Checkout times out] T -->|Same key| R[Lookup or safe retry] R --> P
The network failure hides the result, not necessarily the effect. Reusing the operation identity lets the payment service recognize a repeat.
flowchart LR A[Attempt 1: O-42-pay] --> G[Idempotency gate] B[Attempt 2: O-42-pay] --> G G --> L[One ledger charge] G --> R[Recorded result returned]
The gate deduplicates by authenticated caller, operation, and key. It must protect concurrent requests as well as later retries.
flowchart LR R[Request] --> T[Begin local transaction] T --> K[Claim unique key] K --> B[Update ledger] B --> S[Save result] S --> C[Commit together]
A crash before commit leaves neither a new ledger effect nor a completed key record. After commit, a retry retrieves the result instead of applying another effect.
flowchart LR T[Local transaction] --> O[Order row] T --> E[Outbox row] E --> R[Relay] R --> B[Broker] B --> C[Consumer transaction] C --> I[Unique inbox event ID] C --> S[Local state change]
The outbox closes the state-versus-publication gap. A relay crash after publishing can cause redelivery; the inbox and business update prevent a repeated local effect.
flowchart LR A[Order A] --> I[Inventory atomic reservation] B[Order B] --> I I --> W[One reservation succeeds] I --> F[Other request: out of stock]
The inventory service protects the last item locally. A later compensation cannot justify overselling during concurrent sagas.
flowchart LR A[AI proposes trip] --> H[Human approves exact proposal] H --> G[Tool gateway validates approval] G --> W[Durable booking saga] W --> T[Hotel and payment tools] T --> R[Results and reconciliation] R --> W
The model proposes actions, while the gateway checks authority and the workflow owns durable execution. A repeated model turn must not create another booking for the same approved action.
Learn distributed transactions, sagas, and idempotency by making an online order reliable.
Inside one database, related changes can commit or roll back together.
A workflow can fail after earlier services have committed successfully.
Discuss: What should the customer see while recovery is still running?
A coordinator asks participating transaction managers to prepare, then records a commit or abort decision.
Each service commits locally; failures trigger explicit compensating actions.
A refund offsets a charge, but does not erase history or reverse every consequence.
Discuss: Should a non-refundable ticket be purchased before or after reversible reservations?
Orchestration uses an explicit workflow owner; choreography lets services react to events.
The payment may have committed even when checkout never received the response.
One logical operation keeps one stable key across network retries and worker restarts.
Commit duplicate detection, the local business change, and the result in one transaction.
An outbox prevents a committed order from silently losing its outgoing event.
Sagas do not isolate concurrent workflows from each other.
Choose from business requirements and participant capabilities.
In pairs, trace O-42 through failures and prove that recovery preserves the intended effects.
A travel agent books a hotel, payment, and transport using a durable saga.
Canvas remains the official source for project requirements, deadlines, submissions, and grading.