SDE & MLE Prep

Distributed Transactions

Changing data owned by different databases/services atomically is hard — first try to avoid it (keep data that must change together in one database).

  • Two-phase commit (2PC): the coordinator asks every participant to prepare (lock + vote), then tells all to commit or abort. Atomic but blocking: if the coordinator dies after prepare, participants hold locks until it recovers; as slow as the slowest participant. Used inside databases (Spanner, XA), rarely across microservices.
  • Saga: a chain of local transactions with compensating actions for rollback (eventually consistent; intermediate states are visible). Orchestrated (central coordinator) or choreographed (events).
  • TCC (Try–Confirm–Cancel): each service reserves in try (e.g. freeze $50), then confirm or cancel — explicit holds instead of locks; common in payments and wallets.
  • Transactional outbox: write the business row and an event row in one local transaction; a relay publishes the event (at least once) → never "DB committed but message lost".
  • Make every step idempotent and record processed message ids — retries and duplicate deliveries will happen.