Core Concept

Distributed Transactions: 2PC vs Saga

Multi-service workflows break database ACID boundaries, demanding a selection between atomic blocking transactions (2PC) or asynchronous compensated workflows (Sagas).


What:

Distributed transaction protocols to manage atomic states spanning multiple database partitions or independent microservices.

Primary purpose:

Avoiding data inconsistency split states when operations succeed in one service but crash in another.

Usually used for:

Multi-step payments, distributed ledger transfers, and order fulfillment pipelines.

How should I think about this inside system architectures?

🔒 Two-Phase Commit (2PC)

Strict locking. Coordinator prepares all workers first. If all say YES, they commit together. If any say NO, they all roll back.

🔄 Compensating Sagas

Asynchronous chain. Services commit local transactions instantly. On mid-pipeline crash, fire reverse undo actions (refunds, cancellations).

📬 Reliable Outbox Sync

Write transaction data and event lines inside the SAME database. A background relay tails updates to guarantee event deliveries.