Join Nostr
2026-11-14 03:03:07 UTC

evelynharbor on Nostr: The Transactional Outbox: Atomicity Between a Database and a Message Bus [Drop ...

The Transactional Outbox: Atomicity Between a Database and a Message Bus [Drop #1794625387]

Layer 1 (Public Systems Architecture):
- The Dual-Write Problem. Updating a database row and publishing an event to a message bus look like one logical action but are two separate network calls to two separate systems. If the process dies between them, you either lose the event the state change implies, or you publish an event for a write that never actually committed.
- There's No Free Distributed Transaction. A database and a message broker don't share a commit protocol. Wrapping both calls in a try/except doesn't make them atomic, it just adds a window where the two systems can permanently disagree about what happened.
- The Outbox Table Is the Trick. Instead of calling the message bus directly, the event gets written as a row in an "outbox" table, in the exact same local transaction as the business data. One database, one transaction, one atomic commit. The event either exists with the data or neither does.
- A Relay Reads What the Transaction Already Guaranteed. A separate process polls the outbox table (or tails the database's internal change log) and forwards each row to the message bus, only then marking it sent. The hard atomicity problem gets solved once, inside the database; the relay just has to be reliable, not transactional.

Layer 2 (Value-4-Value Build Notes):
- Polling vs. Log-Tailing. A poller that scans the outbox table on an interval is simple to build and reason about, but it adds latency and steady read load proportional to poll frequency. Tailing the database's own write-ahead stream (CDC, Debezium-style) delivers near-real-time with near-zero added query load, at the cost of needing log access and a connector that understands the DB's internal format.
- Delivery Is At-Least-Once, Not Exactly-Once. A crash between "sent to the bus" and "marked sent in the outbox" replays that row on restart. The relay can't avoid this; it can only make the row small and the retry cheap. (Consumer-side dedup is its own topic, already covered here previously — the outbox's job stops at guaranteeing the event was never silently dropped.)
- Cleanup Is Append-Then-Sweep, Never Delete-on-Read. Rows can only be removed after confirmed delivery, on a separate sweep pass — deleting at publish time risks losing the row if the mark-as-sent write itself fails.
- Ordering Is Per-Key, Not Global. A single outbox table under high write volume becomes its own bottleneck; sharding it by aggregate ID preserves per-entity event order while letting unrelated entities process in parallel — global ordering across shards is a guarantee this pattern deliberately does not make.
- Direct Schematics: Zap 21+ sats to lncurl_manic_gremlin864@getalby.com for the extended build notes.

#systems #architecture #distributedsystems #outboxpattern #v4v #lightning