Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 36 ยท Partitioning and Storage

Distributed Transactions: 2PC and Sagas

Learn atomic commit across services: how two-phase commit works and why it blocks, how sagas use compensations, and the outbox pattern, with simulators.

On one database, a transaction is easy to reason about: transfer 50 from account A to account B, and either both updates happen or neither does. Once the data is sharded across nodes, or one business action spans several services, "both or neither" becomes a hard problem. One node may crash after the other has already committed. Messages may be lost. Nobody can look at the whole system at once.

The goal is atomic commit: all participants reach the same decision, commit or abort, even when some fail. This lesson covers the classic protocol, two-phase commit (2PC), shows why it can block, and then introduces the pattern most microservice systems use instead: the saga.

Two-phase commit

2PC uses one coordinator and several participants (each holds part of the data). As the name says, there are two phases.

sequenceDiagram
    participant C as Coordinator
    participant P1 as Participant 1
    participant P2 as Participant 2
    C->>P1: prepare
    C->>P2: prepare
    P1-->>C: yes (promise to commit)
    P2-->>C: yes (promise to commit)
    Note over C: Decision logged durably: COMMIT
    C->>P1: commit
    C->>P2: commit

Phase 1 collects votes; phase 2 tells everyone the decision.

  1. Prepare (voting). The coordinator asks every participant "can you commit?". A participant that says yes writes everything needed to its log, keeps its locks, and from then on is not allowed to change its mind.
  2. Commit or abort (decision). If all voted yes, the coordinator durably logs "commit" and tells everyone to commit. If anyone voted no (or timed out), it logs "abort" and tells everyone to roll back.

The point of no return is the moment the coordinator writes its decision to disk. Before that, an abort is always possible; after it, the decision is final and must be delivered even if machines restart.

Why 2PC blocks

Suppose a participant has voted yes and then the coordinator crashes before sending its decision. The participant is in doubt: it cannot commit (maybe the others voted no) and it cannot abort (maybe the others already committed). It must wait for the coordinator to return, holding its locks the whole time, so other transactions touching those rows are stuck. This is the blocking problem.

What 2PC gives and costs

Atomicity All commit or all abort.
Held locks Prepared participants wait with locks held.
Coordinator is critical If it dies in the window, participants are stuck.
Latency Two round trips plus disk syncs.

Consensus-based commit protocols avoid the single point of failure.

1 / 2

Let us simulate it. Each participant may vote no, and we can crash the coordinator at a chosen moment.

Python Two-phase commit simulator

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

In case 3 nothing is wrong yet from a data point of view, but the system is stuck until the coordinator recovers. Practical systems mitigate this by replicating the coordinator, for example by running the commit decision through a consensus group. Even then, 2PC across heterogeneous systems and long-running business steps remains heavy, which is why microservice architectures avoid it.

Sagas: a sequence of local transactions

A saga breaks the big transaction into a sequence of local transactions, one per service. Each step commits immediately and releases its locks. If a later step fails, the saga runs compensating actions for the steps that already succeeded, in reverse order, to undo their business effect: "cancel the reservation", "refund the payment".

Example: placing an order. Steps: reserve stock, charge the card, create the shipment. If the shipment fails, refund the card and release the stock.

Key properties:

  • No isolation. Other users can see intermediate states (stock reserved but not yet paid). Design for this, for example with a "pending" status.
  • Compensations are business actions, not rollbacks. You cannot un-send an email; you send a correction. A compensation must be idempotent and must be able to run even if the original step is only partly done (see idempotency and retries).
  • Orchestration vs choreography. In orchestration, a central orchestrator tells each service what to do next. In choreography, services react to each other's events. Orchestration is easier to follow; choreography has less central coupling but its flow is hard to see.
Python A saga with compensations

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that after a failure the state is back to the start, yet in between other readers could have seen the reduced stock. That is the price of dropping isolation.

The outbox pattern

A saga step usually does two things: update its own database and send a message to trigger the next step. These are two systems, so a crash between them either loses the message (the saga stalls) or sends it without the update (the saga moves on wrongly).

The transactional outbox fixes this with a local transaction: write the business change and the outgoing message into an outbox table in the same database transaction. A separate relay process reads unsent rows, publishes them to the broker, and marks them sent. A crash can cause the relay to publish a row twice, so the consumer must deduplicate: the combination gives effectively-once processing on top of at-least-once delivery (see messaging and RPC).

flowchart LR
    S["Service"] -->|"one local transaction"| DB[("Orders table<br/>+ outbox table")]
    DB --> R["Relay"]
    R -->|"publish (maybe twice)"| Q["Message broker"]
    Q --> N["Next service<br/>(deduplicates)"]

State change and message are committed together, then delivered at least once.

Choosing

Use 2PC (or a database's built-in distributed transaction) when the participants are few, fast, trusted, and strong isolation matters, for example shards of one database. Use sagas when steps cross service boundaries, take a long time, or involve external systems. Better yet, redraw the boundaries so that data that must change together lives in one shard or service.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In 2PC, what does a participant promise when it votes yes in the prepare phase?

    A yes vote is binding. The participant has logged enough to commit and must wait for the coordinator's decision.
  2. 2. What is the blocking problem of 2PC?

    A participant that voted yes cannot decide alone, so it is in doubt until it learns the outcome.
  3. 3. What is a compensating action in a saga?

    Earlier steps are already committed, so they cannot be rolled back. Compensations such as refunds or cancellations undo their effect.
  4. 4. What problem does the transactional outbox solve?

    Both the change and the message are written in one local transaction; a relay publishes the message afterwards.

Summary

  • Atomic commit means all participants commit or all abort.
  • 2PC: prepare (votes), then commit/abort. It is atomic but blocking if the coordinator fails after votes, and it holds locks.
  • Sagas replace one big transaction by local transactions plus compensations; they give up isolation and need idempotent steps.
  • The outbox pattern makes "update the database and send a message" reliable.
  • Prefer designs where data that changes together lives together.

Next: Distributed File Systems.