Microservices can't do 2PC across services. Saga pattern: series of local transactions with compensations. Complex but works at scale.

Reserve inventoryCharge paymentShip orderNotify customerRelease inventorycompensationRefund paymentCancel shipmentOrchestratorcentral coordinatorChoreographyevents + reactionsBoth viable
Saga: forward steps with compensating rollback at each stage
Advertisement

Local transactions

Each service commits locally. No distributed lock. Success = next step. Failure = trigger compensation.

Local transactions

Each service commits locally. No distributed lock. Success = next step. Failure = trigger compensation.

Advertisement

Compensating actions

For each forward step, define undo. Refund charged payment. Release reserved inventory. Not always semantically identical to rollback.

Orchestrated saga

Central orchestrator commands each step. Explicit. Easier to reason about + monitor. Downside: orchestrator is a service.

Choreographed saga

Services publish events. Others react. No central controller. Decentralized. Harder to trace.

Idempotent compensations

Compensations may retry. Must be safe to run twice. Design in from day one.

Local txns + compensations. Orchestrated for clarity, choreographed for coupling. Idempotent throughout.