Outbox, inbox and sagas
We solve the problem every event-driven system meets on its first day, changing the database and publishing an event as one reliable step. Then we stretch a business process across several services that cannot share a transaction. We will cover the dual-write problem, the transactional outbox and its relays, and sagas with their compensations.
#Section 26The dual-write problem
Wren’s order service must do two things when an order is placed. It inserts the order into PostgreSQL, and it publishes OrderPlaced to Kafka. The naive code does one after the other.
BEGIN; INSERT INTO orders ...; COMMIT;
kafka.send("order-events", OrderPlaced(124))If the process crashes between the commit and the send, or Kafka is unreachable for a minute, the order exists and the event does not. The kitchen never hears of order 124, and the customer waits for food that nobody is cooking. Reversing the order does not help. Publish first and then commit, and a failed commit leaves the kitchen cooking an order that does not exist. Doing the send inside the transaction, before COMMIT, is no better, since the send succeeds immediately and the commit can still fail.
This is the dual-write problem. Any time code writes to two systems, a database and a broker, a database and a cache, a database and a search index, a failure between the writes leaves them inconsistent, and nothing will ever repair it. Retrying the send in application code narrows the window but cannot close it, because the process can die before it retries.
Distributed transactions would solve it in principle, by making PostgreSQL and Kafka commit together through two-phase commit, but Kafka does not participate in XA transactions, and the coordination costs latency and availability. The practical solutions all have the same shape. Write to one system atomically, and derive the second write from the first, reliably and repeatedly, until it succeeds.
The same problem hides in other places. Unit VII’s cache invalidation after a database write is a dual write, which is why it has a TTL as a safety net and change events as a better option. Sending an email after committing is a dual write, which is why the email is a job. Recognising the shape is half the skill.
#Section 27The transactional outbox
The transactional outbox puts the event in the database, in the same transaction as the state change. Wren’s order service has an outbox table with an id, a topic, a key, a payload, a creation time and a published flag. Placing an order inserts the order rows and an outbox row for OrderPlaced, and commits once. Either both exist or neither does. There is no window.
A separate relay process then moves outbox rows to Kafka. It reads unpublished rows, publishes each with the stored key, waits for Kafka’s acknowledgement, and marks the rows published or deletes them. If Kafka is down, the rows wait. If the relay crashes after publishing and before marking, it publishes those rows again on restart. Delivery to Kafka is therefore at-least-once, which consumers handle with the event id and the inbox from Part III.
There are two ways to build the relay. A polling relay queries the table every 100 ms or so, SELECT ... FROM outbox WHERE published_at IS NULL ORDER BY id LIMIT 500 FOR UPDATE SKIP LOCKED, which lets several relay instances share the work as in Unit VI. It is simple and works on any database. It adds steady query load, its latency is the polling interval, and ordering by id can differ slightly from commit order under concurrency, since a transaction that got a lower id can commit later.
A log-based relay uses change data capture. Debezium, an open-source CDC platform, reads PostgreSQL’s write-ahead log through logical replication, sees each committed outbox insert in commit order, and publishes it, with an outbox event router that turns the row into a proper event on the right topic with the right key. There is no polling, latency is milliseconds, and ordering follows commits exactly. The cost is another system to run and the operational care of replication slots, which hold WAL on the primary until the connector consumes it and can fill the disk if the connector stops.
Outbox rows must be cleaned up, by deleting them once published or by a periodic job. With CDC, the service can even delete the row in the same transaction it inserted it, since the WAL still records the insert for Debezium to read.
The outbox has a mirror image on the consuming side, the inbox from Part III. Together they give a reliable chain. The producer’s state change and event commit together, delivery is at-least-once, and the consumer’s effect and deduplication record commit together.
#Section 28Sagas: choreography and orchestration
Placing an order at Wren is not one transaction. It is a business process across four services. The order service records the order, the payment service captures the payment, the kitchen accepts the ticket, and the courier service assigns a rider. Each has its own database. No single transaction spans them, and holding locks across services for minutes would be absurd anyway. A saga, a term from a 1987 paper by Hector Garcia-Molina and Kenneth Salem, is a sequence of local transactions, one per service, where each step publishes an event or message that triggers the next, and where failures are handled by compensating transactions that semantically undo the steps already completed.
There are two ways to coordinate a saga. In choreography, there is no coordinator. Each service listens for the previous step’s event and publishes its own. The order service publishes OrderPlaced, the payment service reacts and publishes PaymentCaptured, the kitchen reacts and publishes KitchenAccepted, the courier service reacts and publishes CourierAssigned, and the notification service tells the customer at each step.
Choreography is simple to start and keeps services decoupled. As the flow grows, it becomes hard to see. The process exists only as a set of subscriptions spread across repositories, cycles can appear, and answering “where is order 124 stuck?” means reading four services’ logs.
In orchestration, one component, the orchestrator, owns the process. It sends a command to the payment service, waits for the reply, sends the next command to the kitchen, and so on, storing the saga’s state for each order, PAYING, COOKING, DISPATCHING, DONE, in its own database. Workflow engines such as Temporal, Camunda and AWS Step Functions provide orchestrators with durable state, timers and retries built in.
Orchestration makes the process explicit, visible and testable in one place, at the cost of a central component that knows about every participant. Wren uses orchestration for order fulfilment, which has many steps, timeouts and failure paths, and choreography for loosely related reactions such as analytics, loyalty points and search updates, which no process depends on.
#Section 29Compensations and failure paths
Sagas have no rollback. Once the payment service has committed a captured payment, nothing can make that transaction not have happened. What a saga can do is run a new transaction that undoes its business effect, a compensating transaction. Refunding a captured payment compensates for capturing it. Cancelling a kitchen ticket compensates for creating it.
Trace Wren’s failure. The payment is captured, the kitchen accepts the ticket, and the courier service finds no rider within 10 minutes. The orchestrator moves the saga into its compensation path and runs the compensations in reverse order. It tells the kitchen to cancel the ticket and the payment service to refund the payment, then marks the order cancelled and notifies the customer.
Compensations bring their own rules. They are business operations visible to the outside world, a refund appears on the customer’s statement, so they need product design, not only code. They must be idempotent and retryable, because they will be retried after failures like any other step. And some steps cannot be compensated. Once food is cooked, cancelling the ticket does not uncook it, so the restaurant is paid anyway, which is a business decision encoded in the compensation.
Ordering the steps well reduces the need for compensation. Steps that are hard to undo should come as late as possible. The saga literature calls the point after which the saga will run to completion the pivot transaction. Steps before it are compensatable, and steps after it are retried until they succeed. For Wren, assigning a courier before telling the kitchen to start cooking would have avoided cooking food nobody could deliver, a change the team made after this exact incident.
Sagas also lack isolation. While order 124’s saga is half done, other services can see its intermediate states, payment captured and no courier yet. Designs handle this with semantic locks, such as a PENDING status that other processes respect, and by making intermediate states meaningful to users, “confirming your order”.