wtf( )unctionsystem design, drawn
← all problemsDistributed SystemsHard

Two writes, one of which will fail

Placing an order writes a row to the database and publishes an OrderPlaced event. Two separate systems, no transaction spanning them.

If the publish fails after the commit, the order exists and nothing downstream knows — no email, no fulfilment. If you publish first and the commit fails, you've announced an order that doesn't exist. There is no ordering of these two writes that is safe.

Make the event part of the same transaction as the order.
Components — tap one, then tap a slot on the diagram
!An order is in the database and nothing downstream ever heard about it. No email, no fulfilment.

Boundaries, outermost first: One transaction: orders table, an empty slot for the same database Outside every boundary: Event bus (downstream; FAILED: never told), Order service, an empty slot for the reads the outbox Connections: Order service calls orders table — insert order (step 1) Order service calls same database — insert event · same commit (step 2) same database publishes to reads the outbox (step 3) reads the outbox publishes to Event bus — at least once (step 4)

orders table
Event busdownstreamnever told
Order service