Skip to content
Prompt Words
History Member

Transactional outbox

Also called: outbox pattern.

Save the event you want to send as a row in an outbox table, in the same database transaction as the change itself. A separate relay reads the unsent rows, publishes them to the message broker and marks them sent. The change and its event are saved together or not at all, so a crash can delay an event but never lose it, though it may send it twice.

Place an order: one transaction saves it and its event. Crash before publish stops the relay (press it again to restart it); Try a dual write shows the old way, with no outbox.

Orders saved: 0 · Events in Kafka: 0 · Waiting in outbox: 0 · Lost: 0

No orders yet. The relay reads the outbox and publishes each event to Kafka.

    No orders yet

    Say it in a prompt

    When the Orders API creates an order, insert the order row and an OrderPlaced row into an outbox table (id, order_id, type, payload, created_at, sent_at) in the same Postgres transaction. A relay polls every 500 ms for rows where sent_at is null, oldest first, publishes each to the Kafka topic orders keyed by order id, and sets sent_at only after Kafka acknowledges. After a crash the relay can publish a row twice, so consumers skip event ids they have already handled. A cleanup job deletes rows sent more than 7 days ago.

    Vague vs precise prompt

    Vague prompt

    when an order is placed, send an OrderPlaced event to Kafka

    Typical resultSaves the order, then calls the Kafka producer on the next line. If the app crashes or Kafka is down between the two, the order exists but no event is ever sent, so the warehouse never ships it. If the publish works but the save rolls back, an event goes out for an order that doesn't exist.

    Precise prompt

    Use a transactional outbox: insert the order and an OrderPlaced row into the outbox table in one transaction. A relay publishes unsent rows to the Kafka topic orders, oldest first, and marks each sent after Kafka acknowledges. Consumers skip event ids they've already handled.

    Typical resultEvery saved order gets its event, even when the relay or Kafka is down for a while: the row waits in the outbox and goes out when the relay can publish. A rolled-back order sends no event, and an event sent twice is handled once.

    Seen on

    • microservices.io: Describes the pattern: the service stores its messages in a database table in the same transaction as its business data, and a separate message relay sends them to the broker. The relay may publish a message more than once, so consumers should be idempotent.
    • AWS Prescriptive Guidance: Explains the dual write problem (a database update and an event that can fail apart) and the fix: write the outbox row in the same transaction, and let another process send it to the queue.
    • Azure Architecture Center: Shows an ordering service whose event is lost when an error comes between saving the order and publishing it, and how saving the event in the same transaction, then relaying it and marking it processed, prevents that.

    You might describe it as

    • save the order and its message in one go
    • never lose the event if the app crashes after saving
    • a table of messages still waiting to be sent

    Not to be confused with

    • Change data capture (CDC)

      An outbox is the app writing an event row on purpose, in the same transaction as the change; change data capture reads every committed change from the database's own log. They often work together, with a CDC connector as the relay that reads the outbox table.

    • Queue

      A queue holds work until a consumer takes it; an outbox is a table in the sender's own database that makes sure the message gets to the queue at all.