# Distributed Transactions — 26-Point Revision Notes (Enhanced)

Below are the same 26-point, compact revision notes, keeping the important design judgments, failure modes, production examples, and leadership thinking — now enhanced with more detail and ASCII diagrams.

## Distributed Transactions — 26-Point Revision Notes

1 Start with the right mental model
┌─────────────────────────────────────────────────────────────────────────────┐
│                    DISTRIBUTED TRANSACTION ≠                              │
│                    "MANY DBs BEHAVING LIKE ONE DB"                        │
├─────────────────────────────────────────────────────────────────────────────┤
│                                                                           │
│   Distributed Transaction =                                               │
│   ┌─────────────────────────────────────────────────────────────────┐    │
│   │  Workflow coordination                                          │    │
│   │       +                                                         │    │
│   │  Failure handling                                               │    │
│   │       +                                                         │    │
│   │  Retry safety                                                   │    │
│   │       +                                                         │    │
│   │  Compensation                                                   │    │
│   │       +                                                         │    │
│   │  Reconciliation                                                 │    │
│   └─────────────────────────────────────────────────────────────────┘    │
│                                                                           │
│   ┌─────────────┐   ┌─────────────┐   ┌─────────────┐                   │
│   │  Order DB   │   │  Payment DB │   │ Inventory DB│                   │
│   │  (Service A)│   │  (Service B)│   │  (Service C)│                   │
│   └─────────────┘   └─────────────┘   └─────────────┘                   │
│         │                  │                  │                          │
│         └──────────────────┼──────────────────┘                          │
│                            │                                             │
│                    Each service has its own                              │
│                    commit boundary                                       │
│                                                                           │
└─────────────────────────────────────────────────────────────────────────────┘

Core question: What happens if step 2 succeeds and step 3 fails?

2 There is no natural global commit authority
        ┌─────────────────────────────────────────────────────────────────┐
        │          NO SINGLE DB TRANSACTION CONTROLS ALL SERVICES        │
        ├─────────────────────────────────────────────────────────────────┤
        │                                                                 │
        │   ┌─────────────┐   ┌─────────────┐   ┌─────────────┐         │
        │   │  Order DB   │   │  Payment DB │   │ Inventory DB│         │
        │   │   COMMIT ✓  │   │   COMMIT ✓  │   │   COMMIT ✗  │         │
        │   └─────────────┘   └─────────────┘   └─────────────┘         │
        │         │                  │                  │                 │
        │         └──────────────────┼──────────────────┘                 │
        │                            │                                    │
        │                    ┌───────▼───────┐                           │
        │                    │   No single   │                           │
        │                    │ coordinator   │                           │
        │                    └───────────────┘                           │
        │                                                                 │
        │   To get global atomicity:                                     │
        │   ┌───────────────────────────────────────────────────────────┐│
        │   │  Option 1: Introduce coordination (2PC)                  ││
        │   │  Option 2: Accept partial progress (Saga + compensation) ││
        │   └───────────────────────────────────────────────────────────┘│
        │                                                                 │
        └─────────────────────────────────────────────────────────────────┘

Key insight: No single DB transaction can control all services. You must explicitly design for coordination or recovery.

3 The real goal is business correctness
        ┌─────────────────────────────────────────────────────────────────┐
        │              BUSINESS CORRECTNESS > DATA CORRECTNESS          │
        ├─────────────────────────────────────────────────────────────────┤
        │                                                                 │
        │   Wrong Starting Question:                                     │
        │   ┌───────────────────────────────────────────────────────────┐│
        │   │ "How do I make all databases commit together?"           ││
        │   └───────────────────────────────────────────────────────────┘│
        │                                                                 │
        │   Right Starting Question:                                     │
        │   ┌───────────────────────────────────────────────────────────┐│
        │   │ "What business outcome must never be wrong?"             ││
        │   └───────────────────────────────────────────────────────────┘│
        │                                                                 │
        │   Data consistency ≠ Business correctness:                    │
        │   ┌───────────────────────────────────────────────────────────┐│
        │   │  Data consistency: all DB updates commit together        ││
        │   │  Business correctness: customer sees a valid outcome     ││
        │   └───────────────────────────────────────────────────────────┘│
        │                                                                 │
        │   Example:                                                     │
        │   ┌───────────────────────────────────────────────────────────┐│
        │   │  Shipping being delayed 2 minutes → acceptable           ││
        │   │  Payment charged + order missing → NOT acceptable        ││
        │   └───────────────────────────────────────────────────────────┘│
        │                                                                 │
        └─────────────────────────────────────────────────────────────────┘

Design principle: Design from business invariants backward.

4 Data atomicity ≠ business atomicity
        ┌─────────────────────────────────────────────────────────────────┐
        │          DATA ATOMICITY vs BUSINESS ATOMICITY                 │
        ├─────────────────────────────────────────────────────────────────┤
        │                                                                 │
        │   DATA ATOMICITY:                                              │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │  All database updates commit together in one operation │  │
        │   │  ┌─────────┐  ┌─────────┐  ┌─────────┐               │  │
        │   │  │  Order  │  │Payment  │  │Inventory│   ALL or NONE │  │
        │   │  │  Insert │  │  Insert │  │  Update │               │  │
        │   │  └─────────┘  └─────────┘  └─────────┘               │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        │   BUSINESS ATOMICITY:                                          │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │  Customer sees a valid business outcome                 │  │
        │   │  ┌──────────────────────────────────────────────────┐  │  │
        │   │  │  Order created + Payment charged                 │  │  │
        │   │  │      OR                                          │  │  │
        │   │  │  Order created + Payment pending (reconciled)   │  │  │
        │   │  └──────────────────────────────────────────────────┘  │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        │   Not every step needs strict all-or-nothing behavior         │
        │                                                                 │
        └─────────────────────────────────────────────────────────────────┘

Trade-off: Shipping being delayed 2 minutes may be acceptable. Payment charged + order missing is not.

5 Define the strict transaction boundary carefully
        ┌─────────────────────────────────────────────────────────────────┐
        │              TRANSACTION BOUNDARY DECISIONS                    │
        ├─────────────────────────────────────────────────────────────────┤
        │                                                                 │
        │   STRONG CONSISTENCY REQUIRED:                                 │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │  ✓ Money movement and transfers                        │  │
        │   │  ✓ IPO allotment                                        │  │
        │   │  ✓ Lien marking                                         │  │
        │   │  ✓ Irreversible external commitments                    │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        │   EVENTUAL CONSISTENCY ACCEPTABLE:                            │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │  ✓ Notifications and alerts                            │  │
        │   │  ✓ Analytics and reporting                             │  │
        │   │  ✓ Recommendations                                     │  │
        │   │  ✓ Cosmetic UI updates                                 │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        │   Golden Rule:                                                 │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │  Don't let a confirmation email failure block a         │  │
        │   │  financial workflow!                                    │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        └─────────────────────────────────────────────────────────────────┘
6 Four major building blocks
        ┌─────────────────────────────────────────────────────────────────┐
        │                  FOUR BUILDING BLOCKS                          │
        ├─────────────────────────────────────────────────────────────────┤
        │                                                                 │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │  Strong atomicity        → 2PC                         │  │
        │   ├─────────────────────────────────────────────────────────┤  │
        │   │  Distributed workflow   → Saga                         │  │
        │   ├─────────────────────────────────────────────────────────┤  │
        │   │  Reliable event publish → Outbox                       │  │
        │   ├─────────────────────────────────────────────────────────┤  │
        │   │  Crash/retry safety     → Idempotency + Reconciliation│  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │  Mature systems use different patterns for different   │  │
        │   │  boundaries. There is no single "company-wide          │  │
        │   │  transaction pattern" that fits everything.            │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        └─────────────────────────────────────────────────────────────────┘
7 2PC — what it tries to achieve
        ┌─────────────────────────────────────────────────────────────────┐
        │                     TWO-PHASE COMMIT (2PC)                     │
        ├─────────────────────────────────────────────────────────────────┤
        │                                                                 │
        │   Phase 1: PREPARE (Voting)                                    │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │                                                         │  │
        │   │                 Coordinator                             │  │
        │   │                      │                                  │  │
        │   │          ┌───────────┼───────────┐                     │  │
        │   │          ▼           ▼           ▼                     │  │
        │   │      ┌─────────┐ ┌─────────┐ ┌─────────┐              │  │
        │   │      │  Order  │ │ Payment │ │Inventory│              │  │
        │   │      │PREPARE? │ │PREPARE? │ │PREPARE? │              │  │
        │   │      └────┬────┘ └────┬────┘ └────┬────┘              │  │
        │   │           │           │           │                    │  │
        │   │           ▼           ▼           ▼                    │  │
        │   │         YES          YES          YES                   │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        │   Phase 2: COMMIT (Decision)                                   │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │                                                         │  │
        │   │                 Coordinator                             │  │
        │   │                      │                                  │  │
        │   │          ┌───────────┼───────────┐                     │  │
        │   │          ▼           ▼           ▼                     │  │
        │   │      ┌─────────┐ ┌─────────┐ ┌─────────┐              │  │
        │   │      │  Order  │ │ Payment │ │Inventory│              │  │
        │   │      │  COMMIT │ │  COMMIT │ │  COMMIT │              │  │
        │   │      └─────────┘ └─────────┘ └─────────┘              │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        │   All YES → COMMIT                                             │
        │   Any NO  → ROLLBACK                                           │
        │                                                                 │
        └─────────────────────────────────────────────────────────────────┘

Main benefit: Genuine all-or-nothing atomicity.

8 Why 2PC hurts availability
        ┌─────────────────────────────────────────────────────────────────┐
        │              WHY 2PC HURTS AVAILABILITY                        │
        ├─────────────────────────────────────────────────────────────────┤
        │                                                                 │
        │   Problem 1: Blocking Locks                                    │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │  Participants hold locks/prepared state while waiting   │  │
        │   │  ┌─────────┐  ┌─────────┐  ┌─────────┐                │  │
        │   │  │  Order  │  │ Payment │  │Inventory│   ← Locks held │  │
        │   │  │  LOCKED │  │  LOCKED │  │  LOCKED │   ← Resources  │  │
        │   │  └─────────┘  └─────────┘  └─────────┘   ← Blocked    │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        │   Problem 2: Slowest Participant Wins                          │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │  Transaction duration = MAX(all participant times)     │  │
        │   │  Slowest participant slows everything                  │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        │   Problem 3: Coordinator Failure = Blocked Participants       │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │                                                         │  │
        │   │  Coordinator ──► CRASHES!                               │  │
        │   │       │                                                 │  │
        │   │       ▼                                                 │  │
        │   │  ┌─────────┐  ┌─────────┐  ┌─────────┐                │  │
        │   │  │  Order  │  │ Payment │  │Inventory│  ← WAITING     │  │
        │   │  │ WAITING │  │ WAITING │  │ WAITING │  ← FOREVER     │  │
        │   │  └─────────┘  └─────────┘  └─────────┘                │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        │   Conclusion:                                                  │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │  2PC is better for SMALL, tightly controlled systems,  │  │
        │   │  not internet-scale microservice workflows.             │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        └─────────────────────────────────────────────────────────────────┘
9 2PC vs Saga = prevent vs recover
        ┌─────────────────────────────────────────────────────────────────┐
        │                    2PC vs SAGA                                 │
        ├─────────────────────────────────────────────────────────────────┤
        │                                                                 │
        │   2PC:                                                         │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │  "Don't let partial commit happen."                    │  │
        │   │                                                         │  │
        │   │  ┌──────┐   ┌──────┐   ┌──────┐                       │  │
        │   │  │Step 1│──►│Step 2│──►│Step 3│   ALL or NOTHING      │  │
        │   │  └──────┘   └──────┘   └──────┘                       │  │
        │   │                                                         │  │
        │   │  Favors: Stronger immediate atomicity                  │  │
        │   │  Cost:   Availability, scalability                      │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        │   SAGA:                                                         │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │  "Partial progress may happen; recover and converge."  │  │
        │   │                                                         │  │
        │   │  ┌──────┐   ┌──────┐   ┌──────┐   ┌──────┐           │  │
        │   │  │Step 1│──►│Step 2│──►│Step 3│──►│Step 4│           │  │
        │   │  └──────┘   └──┬───┘   └──────┘   └──────┘           │  │
        │   │                │                                        │  │
        │   │                ▼                                        │  │
        │   │            ┌──────────┐                                │  │
        │   │            │ FAIL!    │   Compensate earlier steps     │  │
        │   │            └──────────┘                                │  │
        │   │                                                         │  │
        │   │  Favors: Availability, scalability, independence       │  │
        │   │  Cost:   Complexity, temporary inconsistency           │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        │   Choose based on:                                              │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │  Business tolerance for temporary inconsistency         │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        └─────────────────────────────────────────────────────────────────┘
10 TCC is a middle-ground pattern
        ┌─────────────────────────────────────────────────────────────────┐
        │               TCC — Try, Confirm, Cancel                      │
        ├─────────────────────────────────────────────────────────────────┤
        │                                                                 │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │                                                         │  │
        │   │  Phase 1: TRY                                          │  │
        │   │  ┌──────────────────────────────────────────────────┐  │  │
        │   │  │  Reserve resources without committing            │  │  │
        │   │  │  • Lock inventory quantity                       │  │  │
        │   │  │  • Reserve payment amount                        │  │  │
        │   │  │  • Hold the order slot                           │  │  │
        │   │  └──────────────────────────────────────────────────┘  │  │
        │   │                                                         │  │
        │   │  Phase 2: CONFIRM                                       │  │
        │   │  ┌──────────────────────────────────────────────────┐  │  │
        │   │  │  Convert reservations to actual operations      │  │  │
        │   │  │  • Deduct inventory                              │  │  │
        │   │  │  • Capture payment                               │  │  │
        │   │  │  • Confirm order                                 │  │  │
        │   │  └──────────────────────────────────────────────────┘  │  │
        │   │                                                         │  │
        │   │  Phase 3: CANCEL                                        │  │
        │   │  ┌──────────────────────────────────────────────────┐  │  │
        │   │  │  Release reservations without side effects      │  │  │
        │   │  │  • Release inventory                             │  │  │
        │   │  │  • Release payment hold                          │  │  │
        │   │  │  • Cancel order                                  │  │  │
        │   │  └──────────────────────────────────────────────────┘  │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        │   Advantages:                                                   │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │  ✓ Uses application-level actions (not DB locks)       │  │
        │   │  ✓ Stronger coordination than plain Saga               │  │
        │   │  ✓ Avoids some 2PC limitations                         │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        └─────────────────────────────────────────────────────────────────┘

Useful when: Resources can explicitly support reservation/confirmation/cancellation.

11 Saga = sequence of local transactions
        ┌─────────────────────────────────────────────────────────────────┐
        │                   SAGA WORKFLOW                                │
        ├─────────────────────────────────────────────────────────────────┤
        │                                                                 │
        │   Forward Path:                                                 │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │                                                         │  │
        │   │  ┌─────────────┐                                        │  │
        │   │  │ 1. Create   │──► Order created (local commit)        │  │
        │   │  │    Order    │                                        │  │
        │   │  └─────────────┘                                        │  │
        │   │        │                                                │  │
        │   │        ▼                                                │  │
        │   │  ┌─────────────┐                                        │  │
        │   │  │ 2. Reserve  │──► Inventory reserved (local commit)   │  │
        │   │  │  Inventory  │                                        │  │
        │   │  └─────────────┘                                        │  │
        │   │        │                                                │  │
        │   │        ▼                                                │  │
        │   │  ┌─────────────┐                                        │  │
        │   │  │ 3. Charge   │──► Payment charged (local commit)      │  │
        │   │  │  Payment    │                                        │  │
        │   │  └─────────────┘                                        │  │
        │   │        │                                                │  │
        │   │        ▼                                                │  │
        │   │  ┌─────────────┐                                        │  │
        │   │  │ 4. Create   │──► Shipment created (local commit)     │  │
        │   │  │  Shipment   │                                        │  │
        │   │  └─────────────┘                                        │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        │   Failure Path (Compensation):                                 │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │                                                         │  │
        │   │  Step 3 fails!                                          │  │
        │   │        │                                                │  │
        │   │        ▼                                                │  │
        │   │  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐   │  │
        │   │  │    Saga     │  │ Compensate  │  │ Compensate  │   │  │
        │   │  │  orchestrator│◄─│   Step 2    │◄─│   Step 1    │   │  │
        │   │  └─────────────┘  └─────────────┘  └─────────────┘   │  │
        │   │                                                         │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        │   Each step commits locally.                                   │
        │   Saga is only as good as the recovery logic behind each step.│
        │                                                                 │
        └─────────────────────────────────────────────────────────────────┘
12 Compensation ≠ rollback
        ┌─────────────────────────────────────────────────────────────────┐
        │              COMPENSATION vs ROLLBACK                          │
        ├─────────────────────────────────────────────────────────────────┤
        │                                                                 │
        │   ROLLBACK:                                                     │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │                                                         │  │
        │   │  Charge: $100.00                                        │  │
        │   │     │                                                   │  │
        │   │     ▼                                                   │  │
        │   │  Rollback: Erase the change as if it never happened    │  │
        │   │     │                                                   │  │
        │   │     ▼                                                   │  │
        │   │  Result: Transaction is undone                         │  │
        │   │                                                         │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        │   COMPENSATION:                                                 │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │                                                         │  │
        │   │  Charge: $100.00                                        │  │
        │   │     │                                                   │  │
        │   │     ▼                                                   │  │
        │   │  Compensation: Perform a NEW action to reverse         │  │
        │   │     │                                                   │  │
        │   │     ▼                                                   │  │
        │   │  Refund: $100.00                                        │  │
        │   │     │                                                   │  │
        │   │     ▼                                                   │  │
        │   │  Result: Two separate transactions:                    │  │
        │   │  Charge (+$100) + Refund (-$100) = Net zero            │  │
        │   │                                                         │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        │   EXAMPLES:                                                    │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │  Charge     → Compensation is Refund                  │  │
        │   │  Reserve    → Compensation is Release                 │  │
        │   │  Create     → Compensation is Cancel                  │  │
        │   │                                                         │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        │   Key difference: A refund is NOT literally the same as      │
        │   the original charge never occurring.                        │
        │                                                                 │
        └─────────────────────────────────────────────────────────────────┘

Remember: Compensation has its own side effects and failure modes.

13 Design around reversibility
        ┌─────────────────────────────────────────────────────────────────┐
        │               REVERSIBILITY ORDERING                           │
        ├─────────────────────────────────────────────────────────────────┤
        │                                                                 │
        │   Reversible Steps (Early):                                   │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │  1. Validate      → Can be reversed (just check)       │  │
        │   │  2. KYC           → Can be reversed (status change)    │  │
        │   │  3. Eligibility   → Can be reversed (recalculate)      │  │
        │   │  4. Reserve       → Can be reversed (release)          │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                         │                                      │
        │                         ▼                                      │
        │   Less Reversible Steps (Middle):                             │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │  5. Payment Hold   → Can be released (but timing matters)│  │
        │   │  6. Internal State  → Harder to reverse cleanly        │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                         │                                      │
        │                         ▼                                      │
        │   Irreversible Step (Last):                                   │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │  7. EXTERNAL ACTION → SMS sent, regulator notified,    │  │
        │   │                         external account created        │  │
        │   │                         CANNOT BE UNDONE!              │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │  Identify the "Point of No Return" before designing    │  │
        │   │  your saga.                                            │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        └─────────────────────────────────────────────────────────────────┘
14 Not everything is compensatable
        ┌─────────────────────────────────────────────────────────────────┐
        │           DIFFICULT OR IMPOSSIBLE TO COMPENSATE                 │
        ├─────────────────────────────────────────────────────────────────┤
        │                                                                 │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │  ✗ SMS already delivered to customer                   │  │
        │   │                                                         │  │
        │   │  ✗ Regulator already notified                          │  │
        │   │                                                         │  │
        │   │  ✗ External account created at partner bank            │  │
        │   │                                                         │  │
        │   │  ✗ Irreversible financial action (settlement)          │  │
        │   │                                                         │  │
        │   │  ✗ Email already sent (can't unsend)                  │  │
        │   │                                                         │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │  Strategy: Push irreversible steps LATER in the        │  │
        │   │  workflow, after all reversible steps have succeeded.  │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        │   Example:                                                     │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │                                                         │  │
        │   │  BAD: Send SMS → Reserve Inventory → Charge Payment    │  │
        │   │       (SMS sent even if payment fails)                 │  │
        │   │                                                         │  │
        │   │  GOOD: Reserve Inventory → Charge Payment → Send SMS  │  │
        │   │       (SMS only sent after success)                    │  │
        │   │                                                         │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        └─────────────────────────────────────────────────────────────────┘

Rule: Don't assume every step has a perfect "undo."

15 Compensation itself must be idempotent
        ┌─────────────────────────────────────────────────────────────────┐
        │           COMPENSATION MUST BE IDEMPOTENT                      │
        ├─────────────────────────────────────────────────────────────────┤
        │                                                                 │
        │   WITHOUT IDEMPOTENCY:                                         │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │                                                         │  │
        │   │  Step 3 fails!                                          │  │
        │   │       │                                                 │  │
        │   │       ▼                                                 │  │
        │   │  Compensate: Release Inventory                         │  │
        │   │       │                                                 │  │
        │   │       ▼                                                 │  │
        │   │  Compensate times out!                                  │  │
        │   │       │                                                 │  │
        │   │       ▼                                                 │  │
        │   │  Retry: Release Inventory AGAIN                        │  │
        │   │       │                                                 │  │
        │   │       ▼                                                 │  │
        │   │  ❌ Double-release! Inventory count is now WRONG!      │  │
        │   │                                                         │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        │   WITH IDEMPOTENCY:                                            │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │                                                         │  │
        │   │  Compensate: Release Inventory with idempotency_key    │  │
        │   │       │                                                 │  │
        │   │       ▼                                                 │  │
        │   │  Compensate times out!                                  │  │
        │   │       │                                                 │  │
        │   │       ▼                                                 │  │
        │   │  Retry: Release Inventory AGAIN                        │  │
        │   │       │                                                 │  │
        │   │       ▼                                                 │  │
        │   │  ✓ Idempotency check: Already released                 │  │
        │   │  ✓ Inventory count remains CORRECT!                   │  │
        │   │                                                         │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        └─────────────────────────────────────────────────────────────────┘

Key point: Every compensation needs its own idempotency protection. "It's only rollback" is not a valid reason to ignore idempotency.

16 Orchestration vs choreography
        ┌─────────────────────────────────────────────────────────────────┐
        │           ORCHESTRATION vs CHOREOGRAPHY                       │
        ├─────────────────────────────────────────────────────────────────┤
        │                                                                 │
        │   ORCHESTRATION (Centralized):                                 │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │                                                         │  │
        │   │              ┌─────────────┐                           │  │
        │   │              │ Orchestrator│                           │  │
        │   │              └──────┬──────┘                           │  │
        │   │                     │                                   │  │
        │   │          ┌──────────┼──────────┐                      │  │
        │   │          ▼          ▼          ▼                      │  │
        │   │    ┌──────────┐┌──────────┐┌──────────┐             │  │
        │   │    │  Order   ││Inventory ││ Payment  │             │  │
        │   │    │ Service  ││ Service  ││ Service  │             │  │
        │   │    └──────────┘└──────────┘└──────────┘             │  │
        │   │                                                         │  │
        │   │  Central coordinator tells each service what to do     │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        │   CHOREOGRAPHY (Event-Driven):                                 │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │                                                         │  │
        │   │  ┌──────────┐   OrderCreated    ┌──────────┐         │  │
        │   │  │  Order   │──────────────────►│Inventory │         │  │
        │   │  │ Service  │                   │ Service  │         │  │
        │   │  └──────────┘                   └────┬─────┘         │  │
        │   │                                      │                │  │
        │   │                        InventoryReserved               │  │
        │   │                                      │                │  │
        │   │                                      ▼                │  │
        │   │  ┌──────────┐   PaymentSucceeded   ┌──────────┐      │  │
        │   │  │ Payment  │◄─────────────────────│Inventory │      │  │
        │   │  │ Service  │                      │ Service  │      │  │
        │   │  └──────────┘                      └──────────┘      │  │
        │   │                                                         │  │
        │   │  Each service reacts to events independently           │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        └─────────────────────────────────────────────────────────────────┘
17 When orchestration is easier
        ┌─────────────────────────────────────────────────────────────────┐
        │              WHEN ORCHESTRATION IS EASIER                     │
        ├─────────────────────────────────────────────────────────────────┤
        │                                                                 │
        │   Clear answers to:                                            │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │                                                         │  │
        │   │  ✓ What step are we on?                                │  │
        │   │  ✓ What happens next?                                  │  │
        │   │  ✓ Why is this order stuck?                            │  │
        │   │                                                         │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        │   Advantages:                                                  │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │  ✓ Single place to see workflow progress               │  │
        │   │  ✓ Easier debugging and operational visibility         │  │
        │   │  ✓ Clear state machine                                  │  │
        │   │  ✓ Simpler to implement retry logic                    │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        │   ⚠️ But orchestrator becomes an important component:          │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │  • Requires high availability                           │  │
        │   │  • Must handle its own failures                         │  │
        │   │  • Needs explicit ownership                             │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        └─────────────────────────────────────────────────────────────────┘
18 Choreography can become invisible spaghetti
        ┌─────────────────────────────────────────────────────────────────┐
        │          CHOREOGRAPHY RISK — INVISIBLE SPAGHETTI              │
        ├─────────────────────────────────────────────────────────────────┤
        │                                                                 │
        │   Event Chain (Hard to Debug):                                 │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │                                                         │  │
        │   │  OrderCreated                                            │  │
        │   │       │                                                  │  │
        │   │       ▼                                                  │  │
        │   │  InventoryReserved                                       │  │
        │   │       │                                                  │  │
        │   │       ▼                                                  │  │
        │   │  PaymentCharged                                          │  │
        │   │       │                                                  │  │
        │   │       ▼                                                  │  │
        │   │  ShipmentCreated                                         │  │
        │   │       │                                                  │  │
        │   │       ▼                                                  │  │
        │   │  NotificationSent                                       │  │
        │   │                                                         │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        │   The Problem:                                                 │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │  • Each service knows only its own event reactions    │  │
        │   │  • End-to-end flow is spread across many codebases    │  │
        │   │  • Debugging requires reconstructing an event chain   │  │
        │   │  • No single place to see "what's happening"          │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        │   When to Worry:                                               │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │  • Events are chained more than 3-4 steps              │  │
        │   │  • Teams are confused about end-to-end flow            │  │
        │   │  • Hard to answer "why did this order fail?"          │  │
        │   │  • Growing choreography deserves review/migration      │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        └─────────────────────────────────────────────────────────────────┘
19 Transactional Outbox solves the DB → event gap
        ┌─────────────────────────────────────────────────────────────────┐
        │              TRANSACTIONAL OUTBOX PATTERN                     │
        ├─────────────────────────────────────────────────────────────────┤
        │                                                                 │
        │   BAD (Without Outbox):                                        │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │                                                         │  │
        │   │  1. UPDATE business table                               │  │
        │   │  2. publish event to broker                            │  │
        │   │  3. CRASH ❌                                            │  │
        │   │                                                         │  │
        │   │  Result: DB updated, but event was NEVER published!    │  │
        │   │                                                         │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        │   GOOD (With Outbox):                                           │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │                                                         │  │
        │   │  1. BEGIN TRANSACTION                                   │  │
        │   │  2. UPDATE business table                               │  │
        │   │  3. INSERT outbox table (event)                         │  │
        │   │  4. COMMIT                                              │  │
        │   │  5. (Separate Relay) Read outbox → publish to broker   │  │
        │   │                                                         │  │
        │   │  Result: Local state change + event creation is        │  │
        │   │  ATOMIC!                                                │  │
        │   │                                                         │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │                                                         │  │
        │   │  ┌─────────────┐     ┌─────────────┐                   │  │
        │   │  │  Business  │     │   Outbox    │   Both in one     │  │
        │   │  │    Table   │     │    Table    │   local commit!   │  │
        │   │  └─────────────┘     └─────────────┘                   │  │
        │   │         │                   │                           │  │
        │   │         └───────┬───────────┘                           │  │
        │   │                 │                                        │  │
        │   │                 ▼                                        │  │
        │   │         ┌─────────────┐                                 │  │
        │   │         │   Outbox    │                                 │  │
        │   │         │   Relay     │  ← Separate process/thread      │  │
        │   │         └──────┬──────┘                                 │  │
        │   │                │                                         │  │
        │   │                ▼                                         │  │
        │   │         ┌─────────────┐                                 │  │
        │   │         │   Broker   │                                 │  │
        │   │         └─────────────┘                                 │  │
        │   │                                                         │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        └─────────────────────────────────────────────────────────────────┘
20 Outbox ≠ exactly-once delivery
        ┌─────────────────────────────────────────────────────────────────┐
        │           OUTBOX = AT-LEAST-ONCE (Not Exactly-Once)           │
        ├─────────────────────────────────────────────────────────────────┤
        │                                                                 │
        │   WHY AT-LEAST-ONCE:                                           │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │                                                         │  │
        │   │  1. Relay reads outbox record                           │  │
        │   │  2. Relay publishes event to broker                    │  │
        │   │  3. Relay crashes before marking record as published   │  │
        │   │                                                         │  │
        │   │  On restart, relay reads the record AGAIN              │  │
        │   │  and publishes the event AGAIN                         │  │
        │   │                                                         │  │
        │   │  Result: Event delivered TWICE                         │  │
        │   │                                                         │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        │   SOLUTION:                                                    │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │  Event may be delivered multiple times:                │  │
        │   │  Consumers must be IDEMPOTENT                         │  │
        │   │                                                         │  │
        │   │  ┌──────────────┐                                     │  │
        │   │  │   Consumer   │                                      │  │
        │   │  │  (Idempotent)│←── Event 1 (processed once)        │  │
        │   │  └──────────────┘                                      │  │
        │   │        ▲                                               │  │
        │   │        │                                               │  │
        │   │  ┌─────┴──────┐                                       │  │
        │   │  │   Event 2  │ (duplicate, ignored)                  │  │
        │   │  └────────────┘                                       │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        └─────────────────────────────────────────────────────────────────┘
21 Idempotency = repeated request, one business effect
        ┌─────────────────────────────────────────────────────────────────┐
        │                   IDEMPOTENCY PRINCIPLE                        │
        ├─────────────────────────────────────────────────────────────────┤
        │                                                                 │
        │   Same Intent → Multiple Retries → ONE Business Effect        │
        │                                                                 │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │                                                         │  │
        │   │  Request 1: ReserveInventory(order_id, 10, idempotency)│  │
        │   │       │                                                 │  │
        │   │       ▼                                                 │  │
        │   │  ❌ Timeout! (Request may have succeeded)              │  │
        │   │       │                                                 │  │
        │   │       ▼                                                 │  │
        │   │  Request 2: ReserveInventory(order_id, 10, idempotency)│  │
        │   │       │                                                 │  │
        │   │       ▼                                                 │  │
        │   │  ✅ Already reserved, return success                   │  │
        │   │                                                         │  │
        │   │  Result: Inventory reserved ONCE                       │  │
        │   │                                                         │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        │   IDEMPOTENCY KEYS:                                            │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │                                                         │  │
        │   │  ✓ order_id + saga_step                                │  │
        │   │  ✓ payment_intent_id                                   │  │
        │   │  ✓ external_event_id                                   │  │
        │   │  ✓ request_id from client                              │  │
        │   │                                                         │  │
        │   │  Each important saga step needs its own                │  │
        │   │  idempotency model.                                    │  │
        │   │                                                         │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        └─────────────────────────────────────────────────────────────────┘
22 The deepest rule: timeout = UNKNOWN
        ┌─────────────────────────────────────────────────────────────────┐
        │                 TIMEOUT = UNKNOWN                              │
        ├─────────────────────────────────────────────────────────────────┤
        │                                                                 │
        │   Payment Request:                                             │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │                                                         │  │
        │   │  ┌────────────────────────────────────────────────┐     │  │
        │   │  │ Charge $100.00                                   │     │  │
        │   │  └────────────────────────────────────────────────┘     │  │
        │   │                    │                                    │  │
        │   │                    ▼                                    │  │
        │   │              ┌──────────┐                              │  │
        │   │              │ TIMEOUT! │                              │  │
        │   │              └──────────┘                              │  │
        │   │                    │                                    │  │
        │   │                    ▼                                    │  │
        │   │         ┌─────────────────────┐                       │  │
        │   │         │ Did payment happen? │                       │  │
        │   │         └─────────────────────┘                       │  │
        │   │                    │                                    │  │
        │   │         ┌──────────┼──────────┐                       │  │
        │   │         ▼          ▼          ▼                       │  │
        │   │    SUCCESS    PENDING     FAILED                      │  │
        │   │                                                         │  │
        │   │   Timeout only means:                                  │  │
        │   │   "Response wasn't received in time"                  │  │
        │   │   The remote operation may already have succeeded!    │  │
        │   │                                                         │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        │   RULES FOR TIMEOUT:                                          │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │  ✗ Never blindly retry          (could duplicate)      │  │
        │   │  ✗ Never blindly compensate     (could double-refund)  │  │
        │   │  ✗ Never blindly cancel         (could double-cancel)  │  │
        │   │  ✓ First determine/reconcile the actual outcome        │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        └─────────────────────────────────────────────────────────────────┘
23 Use explicit workflow states
        ┌─────────────────────────────────────────────────────────────────┐
        │               EXPLICIT WORKFLOW STATES                         │
        ├─────────────────────────────────────────────────────────────────┤
        │                                                                 │
        │   State Machine:                                               │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │                                                         │  │
        │   │  ┌─────────────┐                                       │  │
        │   │  │   PENDING   │                                       │  │
        │   │  └──────┬──────┘                                       │  │
        │   │         │                                               │  │
        │   │         ▼                                               │  │
        │   │  ┌─────────────┐                                       │  │
        │   │  │ ORDER_     │                                        │  │
        │   │  │ CREATED    │                                        │  │
        │   │  └──────┬──────┘                                       │  │
        │   │         │                                               │  │
        │   │         ▼                                               │  │
        │   │  ┌─────────────┐                                       │  │
        │   │  │ INVENTORY_  │                                        │  │
        │   │  │ RESERVED    │                                        │  │
        │   │  └──────┬──────┘                                       │  │
        │   │         │                                               │  │
        │   │         ▼                                               │  │
        │   │  ┌─────────────┐     ┌─────────────────┐              │  │
        │   │  │  PAYMENT_   │────►│  PAYMENT_       │              │  │
        │   │  │  CHARGED    │     │  RECONCILIATION │              │  │
        │   │  └──────┬──────┘     └─────────────────┘              │  │
        │   │         │                                               │  │
        │   │         ▼                                               │  │
        │   │  ┌─────────────┐                                       │  │
        │   │  │  COMPLETED  │                                       │  │
        │   │  └─────────────┘                                       │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        │   Why This Matters:                                            │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │  • FAILED and UNKNOWN are fundamentally different      │  │
        │   │  • State machine makes ambiguity explicit              │  │
        │   │  • Each service owns its local state                   │  │
        │   │  • Orchestrator tracks workflow progress               │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        └─────────────────────────────────────────────────────────────────┘
24 Reconciliation is a feature, not an admission of failure
        ┌─────────────────────────────────────────────────────────────────┐
        │              RECONCILIATION AS A FEATURE                      │
        ├─────────────────────────────────────────────────────────────────┤
        │                                                                 │
        │   Why Reconciliation is Needed:                               │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │                                                         │  │
        │   │  • External systems are black boxes                    │  │
        │   │  • Some outcomes cannot be known immediately           │  │
        │   │  • Network timeouts obscure the truth                  │  │
        │   │  • Third-party APIs may not return status              │  │
        │   │                                                         │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        │   Reconciliation Flow:                                         │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │                                                         │  │
        │   │  ┌─────────────┐                                       │  │
        │   │  │   UNKNOWN   │                                       │  │
        │   │  └──────┬──────┘                                       │  │
        │   │         │                                               │  │
        │   │         ▼                                               │  │
        │   │  ┌─────────────┐                                       │  │
        │   │  │ PENDING_    │  ← Explicit state for uncertainty    │  │
        │   │  │ RECONCILIATION│                                      │  │
        │   │  └──────┬──────┘                                       │  │
        │   │         │                                               │  │
        │   │         ▼                                               │  │
        │   │  ┌─────────────┐     ┌─────────────┐                  │  │
        │   │  │   Query     │────►│   External  │                  │  │
        │   │  │   External  │     │    Truth    │                  │  │
        │   │  └─────────────┘     └─────────────┘                  │  │
        │   │         │                                               │  │
        │   │         ▼                                               │  │
        │   │  ┌─────────────┐     ┌─────────────┐                  │  │
        │   │  │  CONFIRMED  │     │  CONFIRMED  │                  │  │
        │   │  │  SUCCESS   │     │  FAILURE    │                  │  │
        │   │  └─────────────┘     └─────────────┘                  │  │
        │   │                                                         │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │  Particularly important for money movement and         │  │
        │   │  regulated external integrations.                      │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        └─────────────────────────────────────────────────────────────────┘
25 Exactly-once is usually the wrong promise
        ┌─────────────────────────────────────────────────────────────────┐
        │          EXACTLY-ONCE is Usually the Wrong Promise            │
        ├─────────────────────────────────────────────────────────────────┤
        │                                                                 │
        │   Why Exactly-Once is Hard:                                   │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │                                                         │  │
        │   │  ┌──────────┐  ┌──────────┐  ┌──────────┐             │  │
        │   │  │  Network │  │  Broker  │  │  Database│   Any of    │  │
        │   │  │  can     │  │  can     │  │  can     │   these can │  │
        │   │  │  retransmit│  │  replay  │  │  fail    │   cause    │  │
        │   │  │  packets │  │  messages│  │  mid-   │   duplicate │  │
        │   │  └──────────┘  └──────────┘  │  commit  │             │  │
        │   │                               └──────────┘             │  │
        │   │                                                         │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        │   More Realistic Target:                                       │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │                                                         │  │
        │   │     At-least-once delivery                              │  │
        │   │          +                                              │  │
        │   │     Idempotent consumers                                │  │
        │   │          +                                              │  │
        │   │     Reconciliation                                      │  │
        │   │          =                                              │  │
        │   │     Effectively-once business outcome                  │  │
        │   │                                                         │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        │   Rules:                                                       │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │  ✓ Automatic compensation for CONFIRMED failures       │  │
        │   │  ✓ Reconciliation for UNKNOWN outcomes                 │  │
        │   │  ✓ Idempotency for all operations                      │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        └─────────────────────────────────────────────────────────────────┘
26 Production + leadership mindset
        ┌─────────────────────────────────────────────────────────────────┐
        │          PRODUCTION PATTERNS + LEADERSHIP MINDSET             │
        ├─────────────────────────────────────────────────────────────────┤
        │                                                                 │
        │   Platform Patterns:                                           │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │  ✓ Saga orchestration                                   │  │
        │   │  ✓ Outbox for reliable event publishing                 │  │
        │   │  ✓ order_id + step idempotency                          │  │
        │   │  ✓ Reconciliation for unknown outcomes                  │  │
        │   │  ✓ External-system safeguards                           │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        │   Domain-Specific Considerations:                              │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │                                                         │  │
        │   │  MF Redemption    → Payment timeout                    │  │
        │   │                    → RECONCILIATION, not compensation  │  │
        │   │                                                         │  │
        │   │  IPO Allotment    → Duplicate webhook                  │  │
        │   │                    → Dedupe using provider event ID    │  │
        │   │                                                         │  │
        │   │  NSDL Demat       → Irreversible account creation      │  │
        │   │                    → Make it the FINAL step            │  │
        │   │                                                         │  │
        │   │  Outbox Lag       → Monitor oldest unpublished event   │  │
        │   │                                                         │  │
        │   │  Compensation     → Compensation itself must be        │  │
        │   │    Retry           idempotent                          │  │
        │   │                                                         │  │
        │   │  LAMF             → Reversible lien marking            │  │
        │   │                    → Before irreversible disbursal     │  │
        │   │                                                         │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        │   Leadership Question:                                         │
        │   ┌─────────────────────────────────────────────────────────┐  │
        │   │                                                         │  │
        │   │  "Why does this workflow/configuration exist,           │  │
        │   │   what assumption does it depend on,                   │  │
        │   │   and does that assumption still hold?"                │  │
        │   │                                                         │  │
        │   └─────────────────────────────────────────────────────────┘  │
        │                                                                 │
        └─────────────────────────────────────────────────────────────────┘
One-Line Mental Model
BUSINESS INVARIANT
       ↓
TRANSACTION BOUNDARY
       ↓
LOCAL COMMITS
       ↓
2PC or SAGA
       ↓
COMPENSATION
       ↓
OUTBOX
       ↓
IDEMPOTENCY
       ↓
STATE MACHINE
       ↓
TIMEOUT = UNKNOWN
       ↓
RECONCILIATION
       ↓
OBSERVABILITY + AUDIT
       ↓
BUSINESS CORRECTNESS
The 5 Rules Worth Memorizing
┌─────────────────────────────────────────────────────────────────────────────┐
│                                                                           │
│   1. Distributed transaction != distributed DB transaction               │
│                                                                           │
│   2. Compensation != rollback                                             │
│                                                                           │
│   3. Timeout != failure                                                   │
│                                                                           │
│   4. Outbox != exactly-once                                               │
│                                                                           │
│   5. Idempotency + reconciliation = practical correctness                │
│                                                                           │
└─────────────────────────────────────────────────────────────────────────────┘
Best Interview Sentence

"I don't start by choosing 2PC or Saga. I first identify the business invariant, classify each step by reversibility and failure semantics, then choose the coordination pattern, idempotency, and reconciliation strategy that keeps the business outcome correct even when the network doesn't tell me the truth."

Quick Reference: The 26 Points in One View
# Topic Key Takeaway
1 Mental model Distributed transaction = workflow + failure + retry + compensation + reconciliation
2 No global commit No single DB controls all services; must design coordination or recovery
3 Business correctness Start with business invariants, not DB atomicity
4 Data vs business atomicity Not every step needs all-or-nothing
5 Transaction boundary Strong for money, eventual for notifications
6 Building blocks 2PC, Saga, Outbox, Idempotency + Reconciliation
7 2PC PREPARE → COMMIT/ROLLBACK, all-or-nothing
8 2PC availability cost Locks, slowest participant, coordinator failure
9 2PC vs Saga Prevent vs recover
10 TCC Try → Confirm → Cancel, middle-ground
11 Saga Sequence of local transactions with compensation
12 Compensation ≠ rollback Refund is a new action, not an undo
13 Reversibility Reversible → less reversible → irreversible (LAST)
14 Not everything is compensatable Push irreversible steps later
15 Compensation idempotency Compensation itself must be idempotent
16 Orchestration vs choreography Centralized coordinator vs event-driven
17 When orchestration is easier Clear visibility, easier debugging
18 Choreography risk Invisible spaghetti, hard to debug
19 Outbox DB update + event insert in one transaction
20 Outbox ≠ exactly-once At-least-once + idempotent consumers
21 Idempotency Repeated request, one business effect
22 Timeout = UNKNOWN Never blindly retry/compensate/cancel
23 Explicit workflow states FAILED and UNKNOWN are different
24 Reconciliation Query external truth for UNKNOWN
25 Effectively-once At-least-once + idempotency + reconciliation
26 Production + leadership Business correctness, assumptions, ownership