← Back to System Design, page 2

system-design-0023 · Hard senior

How do you keep data consistent across microservices? Compare two-phase commit, sagas and the transactional outbox pattern.

Two-phase commit uses a coordinator that asks every participant to prepare and then tells all to commit, giving atomicity but blocking if the coordinator fails. A saga splits the work into local transactions, each with a compensating action to undo it if a later step fails, giving eventual consistency. The transactional outbox writes the business change and the event to publish in one local transaction, so events are never lost or sent for rolled-back changes.

Full answer

The problem

Enrolling a student in a paid course touches three services: Payments charges the card, Enrolment creates the enrolment, Notifications sends a welcome email. Each owns its own database. There is no single database transaction that covers all three.

Two-phase commit (2PC)

  1. Prepare: the coordinator asks each participant "can you commit?". Each writes the change durably, holds locks, and votes yes or no.
  2. Commit or abort: if all voted yes, the coordinator tells everyone to commit; otherwise abort.

Strength: atomic outcome. Weaknesses: participants hold locks while waiting; if the coordinator crashes after prepare, participants are blocked until it recovers; every participant must support the protocol (XA), and latency is that of the slowest participant. 2PC is used inside some distributed databases, but rarely across independent microservices.

Sagas

A saga is a sequence of local transactions. If step N fails, run compensating transactions for steps N-1 back to 1.

StepActionCompensation
1Reserve seat in courseRelease seat
2Charge paymentRefund payment
3Activate enrolmentDeactivate enrolment
  • Orchestration: a saga orchestrator tells each service what to do next and tracks state. Easier to follow and debug.
  • Choreography: each service reacts to events from the previous one. Less central logic, harder to see the whole flow.

Sagas give eventual consistency: between steps, other users can see a seat reserved but not yet paid. Design for that with states such as PENDING_PAYMENT, and make every step and compensation idempotent because messages are retried. Some actions cannot be undone (an email sent), so put them last.

Transactional outbox

A service must update its database and publish an event. Doing them separately fails in two ways: commit then crash before publishing (event lost), or publish then rollback (event for a change that never happened).

The outbox fixes this:

  1. In one local transaction, update the business table and insert a row into an outbox table.
  2. A relay process reads the outbox (polling, or change data capture from the database log) and publishes to the broker, then marks rows sent.
  3. Delivery is at least once, so consumers deduplicate by event ID.

Sagas typically use the outbox for every step.

Choosing

Prefer keeping data that must change atomically inside one service and database. Use sagas with an outbox across services. Use 2PC only where all participants support it and blocking is acceptable.

By level

Fresher: Explain why a single transaction cannot span separate services and describe undoing earlier steps when a later step fails. A fresher can describe the compensation idea without naming every pattern.

Mid: Describe 2PC phases and blocking, sagas with compensations and orchestration versus choreography, and the outbox pattern. Expect the dual-write problem to be explained before the outbox.

Senior: Handle compensation failures, saga state persistence, timeouts, semantic locks, CDC-based relays, ordering guarantees, and decide when service boundaries are wrong instead of adding coordination. It should consider merging services as a real option.

Key points

  • 2PC gives atomicity but blocks on coordinator failure and holds locks
  • Sagas chain local transactions with compensations; consistency is eventual
  • Orchestration is easier to trace than choreography
  • Outbox writes the change and the event in one local transaction
  • All steps and compensations must be idempotent

Common mistakes

  • Proposing 2PC across independent microservices without discussing blocking
  • Forgetting that compensations can also fail and must be retried
  • Publishing an event outside the database transaction
  • Ignoring intermediate states that users can see during a saga

Follow-ups

  • What happens if a compensation fails?
  • How would the outbox relay avoid publishing an event twice?
  • When would you merge two services to avoid a saga?

Tips

  • Use one concrete business flow with three services
  • Draw the saga as a table of actions and compensations
  • Explain the dual-write problem before the outbox

Evaluation rubric

  • Correct 2PC phases and failure mode
  • Correct saga mechanics including compensations and idempotency
  • Explains dual-write problem and outbox solution
  • Gives a sensible recommendation