StoicBuilder

HandbookXV the PantheonSagas and distributed transactions

XVthe Pantheon, Design the system

Sagas and distributed transactions

1 min readUpdated 28 November

In plain words

A saga runs a business process across several services as a series of local steps, each with a compensating action that undoes it, because one database transaction can't span them all.

Where you see it

Upgrading to Harbor Team touches three services: billing authorises the card, deploys raises build limits, identity adds seats, and billing captures the payment last. If adding seats fails, nothing rolls back by itself; the saga restores the limits, releases the authorisation and tells the customer.

Team upgrade sagaagent
Step Undo (compensation)1. billing: authorise card release the authorisation2. deploys: raise limits restore the old limits3. identity: add seats remove the seats4. billing: capture payment (last, because it's the hardest to reverse) Every step and undo is idempotent and retried; progress is saved in the saga's row.
If step 3 fails, run the undos for 2 and 1 in reverse order. The customer gets a clear message, not half an upgrade.

What goes wrong

  1. Assuming a transaction covers several services

    A database transaction stops at the edge of its database. A call to Stripe or another service inside it isn't undone when it rolls back, so money can move while your data says it didn't.

  2. Undos that can't really undo

    A sent email can't be unsent, and a refund isn't the same as never charging. Put the hardest-to-reverse steps last, and write each step's compensation before building the step.

  3. Losing track mid-saga

    If the process crashes between steps, something has to know where it got to. Store the saga's state in the database, or use an orchestrator such as Temporal, so it resumes or compensates after a restart.

What to tell your agent

Copy this into Lovable, Bolt, Cursor or any agent
Model the Team upgrade as a saga: list each step, the service that owns it, its compensating action, and any step that can't be reversed. Store progress in a table so a crash resumes from the last completed step, make every step and compensation idempotent, and put capturing the payment as late as possible.

Quick check

Sign in to earn +10 XP for it

Harbor's upgrade code charges the card through Stripe inside a database transaction, and then the transaction rolls back. What state is the customer in?

Got a prompt that touches sagas and distributed transactions?

Paste it. You get a better prompt first, then the cards that explain it.

Check your own prompt

Free, no sign-up.