Transactional Outbox Pattern
What you'll learn: How to atomically persist notifications alongside business data to guarantee at-least-once delivery without message loss.
The Challenge
When your application creates an order, updates a profile, or triggers any business event that also needs to send a notification, you face a coordination problem. If you write to the database and then publish to a message queue, what happens if the queue publish fails? You've lost the notification. If you publish first and then the database write fails? You've sent a spurious notification.
The Core Idea
The Transactional Outbox Pattern solves this by treating notification events as data. Instead of directly publishing to a queue:
- Write both the business data AND the notification event to the database in a single transaction
- Store notification events in an "outbox" table within the same database
- A separate background worker polls the outbox table and publishes events to the queue
- Mark events as processed after successful publication
Why It Works
Database transactions are atomic—either both writes succeed or both fail. You never end up with inconsistent state. The outbox table acts as a durable log of "notifications we need to send."
Simple Flow
[Business Logic] ---> [DB Transaction: order + outbox_event]
↓
[Outbox Worker polls]
↓
[Publish to Queue] ---> [Notification System]
The worker can retry failed publishes indefinitely because the outbox persists events. Combined with idempotency keys (from lesson 1711), you achieve at-least-once delivery with duplicate protection.
Key Takeaway: The Transactional Outbox Pattern ensures notification events are never lost by storing them atomically with business data in the same database transaction, then asynchronously publishing them via a reliable worker process.