Skip to content

Lesson 8 of 13 · 16m

There Is No BEGIN Across a Network Boundary

Two-phase commit blocks on a dead coordinator, sagas compensate instead of rolling back, and the outbox pattern exists because a commit and a publish cannot be atomic.

The request arrives, you write to Postgres, you publish to Kafka, you return 200. On the second request, the process crashes between the commit and the produce. The database has the row; the broker never got the event; every downstream consumer now disagrees with the system of record and nobody will ever reconcile it automatically. This is the dual-write problem, and it is the reason "just write to both" is not a transaction.

There is no BEGIN that spans two services, and there are three honest ways to fake one.

Two-phase commit: real atomicity, real hostage-taking

2PC splits commitment into a vote and a decision: the coordinator asks every cohort to prepare (can you commit this?), and if all vote yes, broadcasts commit. It works — MySQL speaks the XA protocol directly (XA START 'xid'), while Postgres implements the same two-phase semantics natively (PREPARE TRANSACTION / COMMIT PREPARED) without speaking XA — and it fails in a specific, instructive way: once a cohort has prepared, it is holding locks and cannot unilaterally resolve the transaction. If the coordinator dies after collecting votes, prepared cohorts sit locked until the coordinator recovers and reads its own log. Atomic, and blocking. Spanner shows what the design costs when you refuse to accept the blocking: it runs 2PC over Paxos-replicated participant groups, so the "cohort" survives leader loss — non-blocking atomic commitment, achievable because each participant is itself a consensus group. That is infrastructure you buy, not a pattern you adopt.

Sagas: forward actions, no isolation

The alternative (Garcia-Molina & Salem, SIGMOD 1987) is a sequence of local transactions — reserve inventory, charge card, create shipment — each with a defined compensating transaction: release reservation, refund, cancel shipment. A saga has no rollback in the ACID sense. Committed steps are undone by new committed steps, which means intermediate states are visible to other transactions: the order exists and says pending_payment while the card call is in flight. Isolation is the property you gave up, and the cost shows up as saga anomalies — two sagas interleaving on the same order — mitigated by versioning rows, semantic locks, or making state machines model the intermediate state explicitly (order.payment_failed, not a boolean flip).

The compensating transaction is itself a distributed operation that can fail. It needs its own retry discipline and an alert when it exhausts, because a saga whose compensation fails is a double-charge with extra steps.

The outbox: atomically commit, then relay

The third pattern accepts that DB-and-broker cannot be atomic, and splits the job: write an outbox row in the same transaction as the state change — the commit and the "intent to publish" are atomic because they are one row in one database. A relay then publishes committed outbox rows to the broker and marks them sent. Debezium does this without touching your app: it reads the Postgres WAL, so the outbox table is just a table and the relay is just a tail of the commit log. What you bought: the event can be published at-least-once (the relay can crash and resend), and the state is never missing its event. What you still owe: an idempotent consumer — the previous module's lesson returns — because at-least-once publish plus dedup on the read side is the full picture.

What to actually do

Default to outbox + saga for cross-service workflows; model the intermediate states as first-class states, not error cases. Reach for 2PC only inside one trust domain with a recoverable coordinator — not across teams, not across networks you do not control. Whatever you pick, draw the crash line: for every pair of writes, name what happens when the process dies between them. If the answer is "nothing bad," say which mechanism makes that true.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.