BBackend CourseLeroy Labs
0/0
Search the course

Distributed transactions, from 2PC to sagas (orchestration vs choreography)

Why ACID stops at the service boundary, two-phase commit and its problems, the saga pattern with compensating actions, orchestration vs choreography, and workflow engines like Temporal.

● expertSpring-Boot-Store

The problem

“Place order” in a microservice world touches the Order, Payment, Inventory and Shipping services, each with its own database. No single @Transactional spans them.

Two-phase commit (2PC / XA)

Coordinator: "PREPARE?"  ->  all participants lock resources and vote YES/NO
Coordinator: "COMMIT"    ->  all commit (or ROLLBACK if any voted NO)

The problems:

  • It blocks: if the coordinator dies after PREPARE, participants hold locks indefinitely.
  • It’s slow because of many round trips.
  • Most modern brokers and HTTP APIs don’t support it.

It’s still used inside some databases, and Postgres has PREPARE TRANSACTION, but avoid it across services.

Sagas

A saga is a sequence of local transactions. Each step has a compensating action that semantically undoes it if a later step fails.

1. Order:     create order (PENDING)       compensate: cancel order
2. Inventory: reserve stock                compensate: release stock
3. Payment:   charge card                  compensate: refund
4. Shipping:  create shipment
   -> step 3 fails?  run compensations for 2 and 1 in reverse order
Spring-Boot-StoreCheckoutService.checkout

You already wrote a tiny saga: save the order as PENDING, then create the Stripe session. If that fails, compensate with orderRepository.delete(saved).

Orchestration vs choreography

Orchestration Choreography
How A central orchestrator tells each service what to do Services react to each other’s events
Pros The flow is visible in one place; easy to add timeouts Loose coupling, no central component
Cons The orchestrator can become a “god service” The flow is implicit; hard to trace (“event spaghetti”)
Tools Temporal, Camunda, AWS Step Functions Kafka/RabbitMQ events

SperoTek’s modules use choreography in-process: approvals emits ApprovalDecided, and the ledger posts in response.

Saga properties

  • Not isolated. Other transactions can see intermediate states (an order is PENDING while payment runs). Design the UI and logic for that, for example with reservations or semantic locks.
  • Compensations must be idempotent and retryable.
  • Some steps can’t be undone (an email was sent), so order steps with irreversible ones last, or compensate with another action (“sorry, cancelled” email).

Workflow engines

Temporal (and Cadence) let you write the saga as ordinary code that is durable. If the worker crashes mid-saga, it resumes exactly where it stopped, with retries, timers (“escalate after 48 h”) and compensation built in. They are increasingly standard for payments and onboarding flows.

Check yourself

1. Main drawback of 2PC across services?
2. In a saga, how is a completed step undone?
3. Services reacting to each other’s events without a coordinator is…