ProgramGuru

Distributed Transactions and Sagas

Complete a business action across separate owners

Follow the responsibility

OrderRecords the workflow identity
InventoryReserves or releases stock
PaymentCharges or refunds under a contract
RecoveryRetries, compensates or seeks review
Order: Records the workflow identity → Inventory: Reserves or releases stock → Payment: Charges or refunds under a contract → Recovery: Retries, compensates or seeks review

A checkout can reserve stock and charge payment in separate services. A local database transaction cannot automatically make both owners commit together. If one step succeeds and the next fails, the product needs a truthful state and a recovery path.

First ask whether the operation can stay inside one transactional boundary. When separate ownership is justified, compare coordination and compensating workflows explicitly.

What you’ll learn

  • Separate a local transaction from a cross-service promise.
  • Explain compensation without calling it automatic rollback.
  • Handle uncertain effects and repeated workflow steps.

Atomic commit and a workflow make different promises

Two-phase commit coordinates participating resource managers through prepare and commit decisions. It requires compatible participants and recovery of the coordinator’s decision; failures can leave participants waiting and holding resources. It is not available for every external API.

A saga is a sequence of local transactions with defined actions to compensate earlier work when required. Other observers can see intermediate states unless the application designs around them. Prefer an explicit pending order to claiming an unfinished checkout already succeeded.

A refund is a new action, not erasing history

If stock was reserved but payment was rejected, release the reservation under its identity. If payment succeeded and a later step failed, the refund contract determines what can be reversed and when. Fees, external notifications and shipped goods may not be perfectly reversible.

Compensation can fail and need retry or human review. Persist each step’s state, use stable operation keys and make repeated commands safe. Keep a reconciliation process for uncertain payment results; a timeout alone is not evidence of rejection.

Choose how the incomplete order recovers

Try the design choices

Inventory is reserved, but the payment service rejects the charge.
The payment response is lost after submission.

Compare the visible consequences. With JavaScript enabled, choose an option in each scenario to assemble your decision record.

This is an exploratory design exercise. It does not run infrastructure, measure capacity or replace the lesson’s quiz.

Select one option per situation. Every consequence remains visible; the record can be changed as you learn.

Evaluate consequences across the owners rather than hiding partial success.

Choose who records and drives progress

With orchestration, a coordinator records workflow progress and issues commands. With choreography, services react to events under a shared contract. Orchestration centralizes visibility; choreography can decouple participants but make global progress harder to inspect. Neither makes duplicates or lost communication disappear.

AWS Step Functions and Temporal are workflow options with different hosting and execution semantics. Durable outboxes can connect local commits to message publication. Read AWS’s saga orchestration guidance and your workflow engine’s actual recovery contract.

Monitor pending-age, compensations and unresolved outcomes. Set deadlines for reservations and an explicit human-review path. See event-driven architecture for publication safety.

Check your reasoning

Why is saga compensation different from rolling back a single database transaction?
Choose one answer. You can try again.

Answer and explanation

It performs new actions and may not perfectly reverse external effects.

Earlier local commits can already be visible.

Check your reasoning

Two services each commit a local transaction. Does that alone make checkout atomic across them?
Choose one answer. You can try again.

Answer and explanation

No; the cross-service promise needs a coordination or recovery design.

Separate local commits do not supply a shared atomic boundary.

Practice the individual recovery patterns

Use the Saga pattern exercise to inspect compensation and uncertainty. Outbox can protect each participant’s publication intent; Retry still needs a stable operation identity.

Bring the decisions together

Keep a simple transaction boundary when it fits. When work spans owners, record progress, define compensation and resolve uncertainty before repeating effects.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Microservices vs Monolith – A Beginner's Guide to Architecture Styles →