InterviewKata
Software architecture · Level 5: Data and transactions · Module 24 of 46

Distributed transactions and sagas

Inside one database, a transaction makes several changes happen all together or not at all. Spread those changes across services, each with its own database, and that guarantee is gone. Booking a trip (flight, hotel, card payment) now means three separate commits, any of which can fail after the others succeeded. Sagas are how distributed systems cope: a sequence of local transactions, each with a compensating step to undo it. This module covers what you give up, how sagas work, and how they fail.

1. The idea

Booking a holiday yourself. You book the flight, then the hotel, then try the car rental, which is sold out. There's no "undo all" button across three companies: you cancel the hotel and the flight yourself, one by one, and maybe pay a cancellation fee, and the hotel's confirmation email already landed in your inbox. That's a saga: separate commitments, undone by separate compensating actions, with the intermediate states visible to the world.
ACID (one database)BASE (distributed)
Atomic / Basically availableAll changes or noneThe system keeps answering, even with parts failing
Consistent / Soft stateRules hold after every transactionState may be in flux while updates propagate
Isolated / Eventually consistentOthers never see half-finished workCopies converge once updates stop
DurableCommitted means savedEach local step is durable on its own

CAP explains why: when the network splits replicas apart (a partition), a system must either refuse some requests to stay consistent (CP) or keep answering and reconcile later (AP). Distributed architectures usually choose availability and eventual consistency for most data, and keep strict, atomic consistency inside one service's database for the data that truly needs it.

How a saga works

2. How it works

A trip-booking saga, including a compensation that fails

CAP, illustrated

Two replicas of a seat inventory, one in each data centre. The link between them fails. Watch the two possible behaviours.

3. Try it yourself

Eight kinds of saga

Sagas vary along three dimensions: how services communicate, how consistent the result must be, and how they're coordinated. Each combination behaves differently. Choose one and see its profile. The names are the memorable ones given to them in Software Architecture: The Hard Parts.

Ratings are our qualitative summary, on a five-point scale, following the book's scheme. "Atomic" here means the saga tries to behave like one transaction (nothing visible until all steps finish, coordinated undo); "eventual" lets each step commit and become visible on its own.

4. Trade-offs

Atomic vs eventual

Trying to make a distributed transaction look atomic adds locking, coordination and fragility. Eventual consistency is simpler and more scalable, if the business can accept visible intermediate states.

Compensation vs prevention

Compensating is complex and sometimes impossible (an email can't be unsent). Sometimes it's better to reorder steps so the risky one goes first, or hold resources with a reservation that expires.

Two-phase commit vs sagas

Two-phase commit gives real atomicity across databases but blocks while waiting and needs every participant to support it. Most services and cloud databases don't, so sagas are the norm.

Keep it in one service

The cheapest distributed transaction is the one you don't need. If data must change atomically, consider keeping it in one service (the integrator from Module 16).

5. In real projects

Compensations fail too. A refund API can be down, a hotel can refuse a late cancellation. Every compensation needs retries, idempotency and, in the end, a queue of cases for a person to resolve. Assuming compensation always works is one of the classic fallacies.

Design the visible states. Between steps, other users and systems can see half-finished work: a seat shown as held, a "booking confirmed" email for a trip that's about to be cancelled. Use pending states, delay irreversible side effects (emails, shipping) to the end, and word messages carefully.

Use durable workflow tooling. Orchestrated sagas run well on workflow engines (Temporal, Step Functions, Camunda) that persist the saga's state, retry steps and survive crashes.

Say it in the interview

Two-sentence answers
  • "How do you do a transaction across services?" A saga: local transactions in each service, each with a compensating step, run in reverse if a later step fails. Coordinated by an orchestrator for complex flows, or by events for simple ones.
  • "What if a compensation fails?" Retry it idempotently with backoff; if it still fails, park it for manual resolution and alert. And avoid irreversible steps until the saga is sure to complete.
  • "Explain CAP." During a network partition, a distributed store must choose between consistency (refuse some requests) and availability (answer from possibly stale data). Without a partition, you can have both.
Red flags interviewers notice
  • Assuming you can roll back across services like in one database.
  • Compensations with no failure handling.
  • Sending the confirmation email in the first step of the saga.

6. Remember it

Memory hook: "Book, book, fail, cancel, cancel"A saga moves forward one commit at a time, and on failure walks back, undoing each in reverse.
How this hook works: what "book, book, fail, cancel, cancel" means

What the slogan means, phrase by phrase

"Book, book"
Each step commits locally and is visible straight away.
"fail"
A later step fails; there's no global rollback.
"cancel, cancel"
Compensating steps undo the completed ones, in reverse order, and may themselves fail.

Why this picture fits: everyone who has untangled a holiday booking knows that cancelling is a separate action, sometimes with fees, and that emails already sent stay sent.

The story → the concept

Booking a holidayA saga
Flight, hotel, car booked separatelyLocal transactions in separate services
Cancelling the hotelA compensating update
The non-refundable feeA compensation that can't fully undo
The confirmation emailA visible intermediate state

The trap it saves you from

Designing a distributed workflow as if one database transaction wrapped it.

7. Common mistakes

Irreversible steps firstEmailing, shipping or charging before the risky steps are done. Put irreversible actions last.
Non-idempotent compensationsA refund retried after a timeout refunds twice. Every step and compensation needs an idempotency key.
No home for stuck sagasSagas that can neither finish nor compensate need a dashboard and an owner, not a log line.
Atomic where eventual would doForcing atomic behaviour across services costs scalability and adds failure modes; ask whether the business needs it.

8. Design exercise (30 minutes)

  1. Write the saga (10 min). For an order (reserve stock, charge card, book courier, email), list each step and its compensation.
  2. Reorder it (10 min). Which step is most likely to fail? Which is irreversible? Reorder to fail early and commit irreversibly late.
  3. Break a compensation (10 min). Pick one compensation and design what happens when it fails three times in a row.

9. Self-check quiz

The third of four saga steps fails. How are the first two undone?

Each completed step's compensation runs, last first.

How does a saga differ from an ACID transaction regarding isolation?

Each local commit is visible before the saga finishes.

During a network partition, CAP says a system must:

Refuse some requests (CP) or answer with possibly stale data (AP).

A refund compensation keeps failing. What's the right design?

Compensations can fail; stuck sagas need an owner.

When should the confirmation email be sent?

Irreversible side effects go last.

10. Go deeper

ResourceTimeWhy
Software Architecture: The Hard PartsbookThe eight saga types, transactional sagas and their trade-offs in depth.
Saga pattern (Chris Richardson)15 minOrchestrated and choreographed sagas with examples.
Sagas (Garcia-Molina and Salem, 1987)40 minThe original paper that named the idea.