Video in progress

Strong Middle 🏗️ System design

Orders, payments and inventory are separate services. How do you build the process across them with a saga, and what is the difference between orchestration and choreography?

A saga is a chain of local transactions in each service; if a step fails, the earlier ones are undone with compensating actions. In choreography, services react to each other's events on their own; in orchestration, a central coordinator commands every step.

📖 Answer

Across several services, 2PC (a distributed transaction) is rarely used in practice — it's slow, if the coordinator dies everyone stays stuck in locks, and Kafka and many NoSQL stores don't support it. A saga instead gives up atomicity and compensates.

Steps and compensations:

Step Compensation
Create the order (PENDING) Cancel the order
Reserve stock in inventory Release the reservation
Charge the payment Refund
Mark the order CONFIRMED —

Choreography: order-service emits OrderCreated → inventory reserves and emits StockReserved → payment sees it and charges → if there's a PaymentFailed, inventory releases the reservation and order cancels. There's no center, the services are independent. But after 5–6 steps nobody sees the whole process, it gets hard to answer "where did this order get stuck?", and cyclic dependencies appear.

Orchestration: an OrderSaga (a separate component or inside order-service) runs a state machine: "inventory, reserve" → waits for the reply → "payment, charge" → on an error, "inventory, release". The saga state is stored in the DB. The process is visible in one place and easier to test. The risk is that the orchestrator gathers all the business logic into itself.

In both approaches:

  • every step and compensation must be idempotent — messages get repeated;
  • a compensation isn't a "delete", it's a new action: the customer sees that the money was refunded;
  • there's no isolation: intermediate states are visible to others (an item that's reserved but not paid for). You manage that with states (PENDING) and semantic locks;
  • events should be published through an outbox.

I usually pick choreography for a simple 2–3 step flow, and orchestration for a multi-step flow that touches money (a workflow engine like Temporal is also an option).