Last Minute Notes
  • Github
  • Docs
  • Learn
    @sayanstwt
Lessons
0/37 done
  • CAP & PACELC
  • Consistency Models
  • Time & Ordering
  • Consensus with Raft
  • Locks, Leases & Fencing
  • Isolation Levels & Anomalies
  • 2PC & Sagas
  • Idempotency & Exactly-Once
Learn/System Design/Distributed Systems

2PC & Sagas

8 min Advanced 6 steps

Commit across services, or compensate when you cannot.

Databases
3
Local txn
cannot span
Options
2PC / saga
OrchestratorOrderPaymentStockcreate ordercharge cardreserve stockwhat if step 3 fails?
request reply failure
In this lessonStep 1 of 6
  1. The problem
  2. 2PC
  3. Blocking
  4. Saga
  5. Compensate
  6. Recap
Step 1 / 6problem

One action, several databases

Placing an order touches orders, payments and stock, owned by different services and databases.

A normal local transaction cannot span them, so a failure halfway leaves the data inconsistent.

Key idea

You need either one atomic commit across systems (2PC) or a way to undo (saga).

←→ step·R replay·Space pause

You'll learn

  • How two-phase commit works
  • Why 2PC blocks when the coordinator dies
  • Run a saga and compensate on failure
  • Orchestration vs choreography
Read the full notesDistributed Systems, 7