Outbox Pattern
ELI5 — The Vibe Check
The outbox pattern solves the dual-write problem: how do you update a database AND send a message to a queue atomically? Answer: write both to the database in one transaction. A separate process reads the outbox table and publishes to the queue. If it fails, it retries. No data loss.
Real Talk
The outbox pattern ensures reliable message publishing by writing events to an 'outbox' table within the same database transaction as the business data change. A separate relay process (polling or CDC) reads unpublished outbox entries and publishes them to the message broker, then marks them as sent. This guarantees consistency between database state and published events.
Show Me The Code
-- In a single transaction
BEGIN;
INSERT INTO orders (id, total) VALUES (1, 99.99);
INSERT INTO outbox (event_type, payload)
VALUES ('order_created', '{"orderId": 1, "total": 99.99}');
COMMIT;
-- Relay process publishes outbox entries to Kafka/RabbitMQ
When You'll Hear This
"The outbox pattern guarantees we never create an order without publishing the event." / "We use CDC on the outbox table instead of polling for better throughput."
Related Terms
Change Data Capture
Change Data Capture (CDC) watches your database for changes and streams them as events.
Event-Driven Architecture
Event-Driven Architecture is like a gossip network. When something happens (order placed!), it broadcasts the news.
Inbox Pattern
The inbox pattern is the receiving side of the outbox pattern. When a message arrives, you write it to an inbox table first, then process it.
Transaction
A transaction groups multiple database operations into one all-or-nothing bundle. Either ALL of them succeed, or NONE of them happen.