Skip to content

Outbox Pattern

Spicy — senior dev territoryBackend

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."

Made with passive-aggressive love by manoga.digital. Powered by Claude.