Step 1 · The pattern
Save it, then tell everyone
An order comes in. The service saves it to its database, then publishes an event so shipping, email and analytics can react. Two writes, to two systems.
- order request
- done
In short
Outbox pattern: how it works and how it breaks
A service often has to do two things for one request: save a row in its database and publish an event so other services can react. Those are two writes to two systems, and no ordinary transaction covers both. If the process crashes after the commit but before the publish, the order exists and nobody downstream hears about it. Publishing first just flips the problem. The outbox pattern removes the gap. In the same transaction that saves the order, the service inserts the event into an outbox table. A separate relay reads that table, or the database's change log, publishes each row to the broker and marks it sent. If the relay crashes it publishes again, so consumers must tolerate duplicates. You trade a lost event for a possible repeat, which is the easier problem.
In Spring Boot
An outbox row in the same transaction, and a relay
@Transactional
public Order place(OrderRequest req) {
Order order = orders.save(Order.from(req));
outbox.save(new OutboxEvent("order.created", order.getId(), toJson(order)));
return order; // both rows commit, or neither
}
@Scheduled(fixedDelay = 500) // milliseconds; needs @EnableScheduling
public void relay() throws Exception {
for (OutboxEvent e : outbox.findTop100BySentAtIsNullOrderByIdAsc()) {
// wait for the broker's acknowledgement before marking it sent
kafka.send("orders", e.getAggregateId(), e.getPayload()).get();
e.markSent();
outbox.save(e);
}
}The order and its event are saved by one database transaction, so there is never an order without an event. A scheduled relay reads unsent rows in order, publishes each with KafkaTemplate and marks it sent only after the broker has acknowledged it.
Gotcha: This is at-least-once. If the process stops after the send and before the row is marked, the event goes out again, so consumers must ignore an event id they have seen. Two instances of the relay will also pick up the same rows unless only one runs or the query locks them.
Names checked against the official docs on 15 Nov 2026:
How this comes up in interviews
"An order service must save the order and publish an event for shipping. What happens if it crashes between the two, and how do you fix it?"
Then they ask
- Why not publish the event first and then commit the order?
- The relay publishes a row and crashes before marking it sent. What do consumers see?
- How would you read the outbox: polling the table or reading the database's change log? What are the trade-offs?
A strong answer covers
- A database commit and a broker publish are two writes to two systems, and no ordinary transaction covers both.
- A crash after the commit and before the publish leaves an order nobody downstream hears about.
- Insert the event into an outbox table in the same transaction as the order.
- A separate relay reads the outbox, publishes each row and marks it sent.
- Delivery is at-least-once, so consumers must tolerate duplicates.
Points to cover, not a script. Say them in your own words and tie them to a system you have worked on.
On one page: the track cheat sheet.
Sources: Airbnb: Avoiding double payments in a distributed payments system ↗ · Uber: Reliable reprocessing and dead letter queues ↗ · Jay Kreps: The Log ↗ · Report a problem
Diagrams are our own simulations: the timings, rates and counts in them are illustrative, not measurements. Captions are original wording.