Transactional Outbox for Reliable Event Publishing

2801
0

I used a transactional outbox when I needed my database write and my event publish to succeed or fail together. In OutboxEvent model, I treated the outbox like a queue: a durable row per event, a dedupe_key for idempotency, and a ready scope that pulls pending rows in a stable order. Next, in Write + outbox in one transaction, I wrapped Order.create! and OutboxEvent.create! inside a single ApplicationRecord.transaction, and I made the payload explicit so replays and backfills are predictable. Finally, in Publisher worker, I process small batches, transition to publishing under with_lock, publish under ActiveSupport::Notifications, and only then mark published; failures get recorded as failed for safe retries.