Course contentsShow
← Computer Science
Lesson 2721 of 2,87260. Distributed Data and CommunicationFree lesson

Saga Pattern for Long-Running Transactions

Breaking long transactions into local transactions with compensating actions, choreography vs orchestration approaches.

Saga Pattern for Long-Running Transactions

What you'll learn: How to break long transactions into manageable steps with rollback strategies when 2PC/3PC isn't practical.

The Core Idea

In distributed systems, holding locks across multiple services during a long transaction (like booking travel: flight, hotel, car) can cause timeouts, deadlocks, and resource exhaustion. The Saga pattern solves this by splitting one big transaction into a sequence of local transactions, each updating a single service. If any step fails, the system runs compensating actions to undo the work of completed steps.

Think of it like planning a road trip: instead of reserving everything simultaneously (and holding all reservations until the last one confirms), you book each leg separately. If the hotel is full, you cancel your earlier flight and car reservations using cancellation procedures—your compensating actions.

Two Coordination Approaches

Choreography

Each service publishes events and listens for events from others. When Service A completes its local transaction, it emits an event. Service B reacts, performs its work, and emits another event. If something fails, services detect it through events and trigger their own compensating actions.

Pros: Decentralized, no single point of failure
Cons: Harder to track overall progress; implicit dependencies

Orchestration

A central coordinator (orchestrator) explicitly tells each service what to do and when. It tracks the saga's state and triggers compensating actions if needed.

Pros: Centralized logic, easier to monitor
Cons: Orchestrator becomes a critical component

Trade-offs

Unlike 2PC, sagas don't guarantee isolation—intermediate states are visible. You must design compensating actions carefully (some operations, like sending emails, can't be perfectly undone).

Key Takeaway: The Saga pattern achieves eventual consistency for long-running distributed transactions by decomposing them into local transactions with compensating rollback logic, coordinated either through choreography (event-driven) or orchestration (centralized control).