Medium system-design fundamentals

What are two-phase commit, sagas and the outbox pattern, and when do you use each?

The 30 second answer

Two-phase commit makes several databases commit or abort together: a coordinator asks everyone to prepare, then tells them all to commit. It's atomic, but if the coordinator dies after prepare, participants are stuck holding locks, so it's rarely used across microservices. A saga is a chain of local transactions with compensating actions to undo earlier steps, and the outbox pattern makes "update my database and publish an event" reliable by writing the event in the same local transaction.

Could you say this in an interview?

Two-phase commit (2PC).

  • Phase 1, prepare: the coordinator asks each participant to get ready; each writes everything needed to commit to disk and votes yes or no. In PostgreSQL this is PREPARE TRANSACTION, after which the transaction's state is fully stored on disk and can later be finished from any session with COMMIT PREPARED or ROLLBACK PREPARED.
  • Phase 2, commit: if everyone voted yes, the coordinator tells all of them to commit; otherwise to roll back.
  • The blocking problem: a participant that has voted yes cannot decide alone. If the coordinator crashes, prepared transactions stay open, holding locks, until it recovers or someone resolves them by hand. PostgreSQL's docs warn against leaving prepared transactions open. microservices.io adds that many modern databases and message brokers don't support 2PC at all.

Saga. Each step is a local transaction in one service; if a later step fails, compensating transactions undo the earlier ones (cancel the order, refund the payment). Two styles:

  • Choreography: each service publishes events that trigger the next step. No central component, but the flow is hard to see.
  • Orchestration: one orchestrator tells each service what to do next. Easier to follow and test.

Both microservices.io and Microsoft's guide point out the cost: a saga has no isolation like an ACID transaction, so concurrent requests can see intermediate states or lose updates. That is why the order starts in a PENDING state in the microservices.io example, and why Microsoft separates compensable steps from the pivot step, the point of no return after which the remaining steps must be retryable until they succeed.

Transactional outbox. The problem is the dual write: updating the database and publishing to a broker cannot be atomic without 2PC. With an outbox, the service inserts the event into an outbox table in the same local transaction as the business change, and a separate relay, polling or reading the database log, publishes it to the broker. Delivery is at least once, so consumers must deduplicate.

How I choose. Inside one database: a normal local transaction. Across services: a saga, with each step publishing through an outbox. 2PC only where all participants support it, the scope is small, and the blocking risk is acceptable.

A worked microservices answer for the same problem is on the senior Java and Spring Boot page.

Sources (4)
  1. PostgreSQL: PREPARE TRANSACTIONpostgresql.org
  2. microservices.io: Saga patternmicroservices.io
  3. microservices.io: Transactional outboxmicroservices.io
  4. Microsoft Azure Architecture Center: Saga design patternlearn.microsoft.com