The outbox pattern

The outbox pattern An architecture diagram generated by Archify. POST /orders · SO-4417 · Architecture component POST /orders SO-4417 PlaceOrderHandler · one connection, one transaction · Architecture component · 201 PlaceOrderHandler one connection, one transaction 201 orders · the row the customer paid for · One transaction: both rows or neither orders the row the customer paid for outbox · id 8814, OrderPlaced, unsent · One transaction: both rows or neither outbox id 8814, OrderPlaced, unsent Poller · FOR UPDATE SKIP LOCKED · Relay: pick one, not both Poller FOR UPDATE SKIP LOCKED Log tailer · reads the WAL, queries nothing · Relay: pick one, not both Log tailer reads the WAL, queries nothing Broker · message id 8814 · Architecture component Broker message id 8814 EmailReceipt · has it handled 8814 before? · The consumer is where duplicates stop EmailReceipt has it handled 8814 before? processed · 8814, written with the side effect · The consumer is where duplicates stop processed 8814, written with the side effect commit, then publish · two systems, one try block · Dual write: the crash lands between them commit, then publish two systems, one try block Broker · never heard about SO-4417 · Dual write: the crash lands between them Broker never heard about SO-4417 one request insert the order insert the message at least once insert 8814, or stop the commit stands One transaction: both rows or neither Relay: pick one, not both The consumer is where duplicates stop Dual write: the crash lands between them Legend Caller Your code A row in your database Broker

Dual write

  • • Commit the order and then publish, and a crash in the gap leaves an order nobody is ever told about
  • • Publish first and a rolled back transaction leaves a message about an order that does not exist
  • • There is no ordering of the two that is safe, because there is no commit that covers both
  • • XA is the other answer: every participant has to be a transaction manager, it holds locks across a network round trip, and Kafka is not one at all

The outbox row

  • • The order row and the message row go in on the same connection in the same transaction, so there is one commit and no gap to crash in
  • • Written after the commit, on a second connection, or into a second database, it is a dual write again with extra steps
  • • The poll wants an index on the unsent predicate, or it degrades into a growing scan as the table fills
  • • SKIP LOCKED lets several relays claim different rows at once, and gives up ordering to do it

At least once

  • • The relay can publish and die before it marks the row sent, so the outbox trades the message you lost for the one you get twice
  • • The outbox id travels as the message id, which is the thing the consumer has to dedupe on
  • • The consumer writes that id in the same transaction as its side effect, or the crash has only moved
  • • A sequence hands out ids before commit, so 8815 can commit first: a poller ordered by id publishes out of order, and one tracking a high water mark skips 8814 outright