Multi-Step Processes

How to run a business process that spans several services, such as placing an order, so that it either finishes or is cleanly undone: sagas, compensations, orchestration and choreography, and durable workflow engines.

Overview

Many business actions touch several services. Placing an order reserves stock in the inventory service, charges a card through the payment service, creates a shipment and sends an email. Inside one database, a transaction would make all of that happen or none of it. Across services, each with its own database, there is no such transaction: a step can succeed and the next one fail, leaving stock reserved for an order that was never paid.

A multi-step process is a design for exactly this. It keeps track of which steps are done, retries the ones that fail for a moment, and, when the process cannot finish, undoes the finished steps with compensating actions. The standard name for this is a saga, from a 1987 paper by Hector Garcia-Molina and Kenneth Salem about long-lived transactions.

The design questions are always the same. Who coordinates the steps: a central orchestrator, or services reacting to each other's events? Where is the process state kept, so a crash does not lose it? How is each step made safe to repeat? What happens while the process is only half done? Interviewers listen for compensations, idempotency and a clear owner of the process state.

What you will be able to do

  • Explain why two-phase commit is rarely used across services, and what a saga gives up instead.
  • Design a saga with compensations, and order its steps around a pivot.
  • Choose between orchestration and choreography, and say what each costs.
  • Make every step idempotent, and publish events without dual writes.
  • Describe how a durable workflow engine keeps a long process running through crashes, timers and human approvals.

Key concepts

  • Local transactions
  • Two-phase commit
  • Sagas
  • Compensating transactions
  • Pivot and retriable steps
  • Orchestration vs choreography
  • Idempotent steps
  • Durable execution
  • Timers and callbacks
  • Semantic locks

Interview questions

  • Design the order placement flow across inventory, payment and shipping services.
  • Why not use two-phase commit across microservices?
  • Compare choreography and orchestration. Which would you choose?
  • What is a compensating transaction, and what makes one hard to write?
  • How do you make sure a saga step is not executed twice?
  • How does a durable workflow engine such as Temporal or Step Functions help?
  • A customer sees their order as confirmed, and then it is canceled. How do you prevent that?
  • How would you model a process that needs a manager's approval within 48 hours?

Sign up free to read the full topic

More System Design topics

Start free on Cariara · Coding problems · System design studio