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?
Keep reading
Related articles from the SudoSecurity blog.
What is Platform as a Service? The Sweet Spot Between "Too Easy" and "Too Much Work"
Platform as a Service sits between raw servers and fully managed software. You push code, the platform runs it. No Nginx config, no OS patches, no 3 AM server alerts. Here is what PaaS actually gives you, what it costs you in control, and how to decide if it is the right layer for your application.
The World now is Serverless and Container based
A new report from Datadog reveals that serverless computing could be entering the mainstream. This has already been the case in my world for the past few years. Results show that more than half of all organizations are using serverless on one of the three major public cloud providers: Amazon,
AWS Emulator Puts AWS on Your Laptop. Keep Your IAM Tests Off It.
AWS released AWS Emulator on November 11, a free local server that answers AWS API calls on 127.0.0.1 with per-run keys and fresh certificates.
A 257-Byte Password Is the Key: Inside the OpenSSH musl Bypass
CVE-2026-123158 lets an attacker log into a locked account on musl-based systems like Alpine by sending a password nobody set. The trigger is one number, 257, and the reason is a C library returning a single asterisk. I reproduced the exact boundary against musl 1.2.4 and read the fix.