System design walkthrough · 11 min
An accepted order is never lost
The restaurant taps Accept; the kitchen, the rider and the customer must all hear about it, exactly once, even when services restart and networks drop. From two writes to an outbox.
Seen in food delivery, e-commerce, payments, bookings: anything where one action must reach several systems.
The ask
What we are building
When a restaurant accepts an order, four things must follow: the kitchen display shows it, dispatch finds a rider, the customer is told, and the payment is captured. Each lives in its own service. Design acceptance so that none of them ever misses an order, and none acts on it twice.
- An accepted order always reaches every service that needs it.
- No service acts on it twice: one meal cooked, one rider, one charge.
- A restart, deploy or network timeout at any moment loses nothing.
- A slow broker delays orders but never drops them.
- Each order's events arrive in order (accepted before cancelled).
- Orders a day
- 5 million
- Services per acceptance
- 4
- Deploys a day
- dozens
- Acceptable losses
- zero
Back of the envelope
How big is it?
Illustrative numbers. The point is that rare failures stop being rare at this volume.
| What | Working | Result |
|---|---|---|
| Acceptances at peak | 5M × 60% ÷ 3 h | ~280 a second |
| Events to deliver | 280 × 4 services | ~1,100 a second |
| If 1 in 10,000 publishes is lost | 5M orders ÷ 10,000 | ~500 lost orders a day |
| Each lost order costs | a refund, a voucher and a customer | far more than the meal |
| Restarts while busy | dozens of deploys + crashes, each mid-request | every day |
Build it, then break it
Your turn: design it first
Make the two calls yourself before reading on. The simulator runs your design quietly, at dinner and at peak, with a deploy restarting the services every few seconds, as dozens of deploys a day do.
Answer every question to run it.
Version 1
Save the order, then publish the event
Breaks · loses orders silently, and many more under load
The obvious code: UPDATE orders SET status = 'accepted', commit, then publish(OrderAccepted) to the message broker, and let the kitchen, dispatch and notifications subscribe.
Between those two lines is a gap. If the publish times out because the broker is busy, or the service is restarted by a deploy right after the commit, the order is accepted in the database and nobody else hears about it. The customer waits for a meal nobody is cooking, and no alert fires. Swapping the order (publish first, then save) gives the opposite bug: an event about an order that was never saved.
- Writes
- 2, not atomic
- Failure
- silent
Try each load. Numbers are illustrative: a small simulation, not a measurement.
Why not…
One distributed transaction across the database and the broker?
Common brokers, Kafka among them, cannot join a distributed (XA) transaction with your database, and where two-phase commit is possible it is slow and stalls when either side is down.
Publish first, then save?
Moves the gap instead of closing it: now a crash leaves an event about an order the database never accepted.
Go deeper:Outbox pattern
Version 2
Retry the publish until the broker says yes
Breaks · duplicates under load, and still loses orders on restart
Wrap the publish in a retry loop with backoff, so a timeout no longer loses the event.
But a timeout means you do not know whether it landed. Often the broker stored it and only the reply was lost, so the retry delivers a second copy: the kitchen cooks twice, two riders are sent, the card is captured twice. And the retry loop lives in memory: if the service restarts after the commit and before a successful publish, the event is still lost.
- Lost on timeout
- no
- Lost on restart
- still yes
- Duplicates
- yes
Try each load. Numbers are illustrative: a small simulation, not a measurement.
Why not…
Never retry, to avoid duplicates?
Then every timeout is a lost order. You need retries; you also need the receiver to recognise a copy.
Ask the broker whether the first attempt landed?
During a timeout the broker is exactly what you cannot reach. And the question races with the original publish, which may still land a moment later.
Go deeper:Idempotency keys
Version 3
Write an outbox row in the same transaction
Holds · at 6× load: slower when the broker is busy, never lost, never twice
Put the event in the database too. In one transaction, update the order and insert a row into an outbox table: both commit or neither does. There is no longer a moment where the order exists and its event does not.
A separate relay publishes outbox rows: it reads them from the database's change log (change data capture) or polls the table, and keeps retrying until the broker accepts, so a slow broker only delays orders. Delivery becomes at least once: after a relay restart some events are sent twice.
So every event carries a unique id, and each consumer records the ids it has handled, in the same transaction as its own work, skipping any it has seen. Events are keyed by order id, so one order's events stay in order. The tablet's Accept call carries an idempotency key too, so a double tap is one acceptance.
- Order and event
- 1 transaction
- Delivery
- at least once
- Effect
- exactly once
Try each load. Numbers are illustrative: a small simulation, not a measurement.
Why not…
Poll the outbox table instead of reading the log?
Simpler to start with and fine at modest volume; it adds load and a delay of up to one polling interval. Reading the log (for example with Debezium) is near real time with low overhead.
Skip the outbox and make everything synchronous calls?
The order service would wait on four services at once: if any is slow or down, accepting an order fails or hangs. The outbox decouples them; the broker absorbs the bursts.
Harden it
Poison messages, cleanup and a safety net
Holds · with a dead-letter queue and a sweeper behind it
Poison messages: an event a consumer can never process must not block the ones behind it. Retry a few times, then move it to a dead-letter queue and alert a person.
Cleanup: an outbox table grows forever unless rows are removed. With change data capture the row can be deleted right after insert (the log keeps the event); with polling, delete published rows in small batches.
A safety net: a sweeper looks for orders accepted more than two minutes ago with no rider assigned and raises them (or re-emits the event). It catches the bug nobody predicted. Watch the relay's lag: a growing lag is the earliest sign that orders are being delayed.
Go deeper:Retry topics and dead-letter queuesPayment retry double charge (use case)
Putting it together
The final design
The order and its event are written in the same transaction, so neither can exist without the other. The relay and the broker may deliver an event more than once, so every consumer skips event ids it has already handled.
Tablet → order service
Accept with an idempotency key, so a double tap or a retried request is one acceptance.
Watch out: Keys must be stored with the result, or a retry after a timeout runs the work again.
Order + outbox, one transaction
The order's new state and its event commit together.
Watch out: An outbox write outside the transaction brings the original bug back.
Relay (change data capture)
Reads new outbox rows from the database log and publishes them, retrying until accepted.
Watch out: If it stops, nothing is lost but everything waits: alert on its lag.
Broker, keyed by order id
Carries events to every consumer, in order for each order.
Watch out: Keying by restaurant or city creates hot partitions at dinner.
Idempotent consumers
Record each event id with their own work, in one transaction; skip ids already seen.
Watch out: Recording the id separately from the work reopens the gap on their side.
Dead-letter queue
Holds events that keep failing, so they do not block the rest.
Watch out: A dead-letter queue nobody watches is just a slower way to lose orders.
Sweeper
Finds accepted orders that never progressed and raises them.
Watch out: Too eager and it duplicates work: it should alert or re-emit, relying on idempotency.
From boxes to an API
API and data model
Acceptance is one request that must happen exactly once, and several consumers that must each act on it exactly once. The API carries idempotency keys in; the tables carry event ids out.
Endpoints
POST/v1/orders/{id}/acceptCalled by Restaurant tabletRequires an Idempotency-Key; a double tap or a retried request returns the first result instead of accepting twice.POST/v1/orders/{id}/rejectCalled by Restaurant tabletSame rules as accept; the order's state machine refuses a reject after an accept, and the reverse.GET/v1/orders/{id}Called by Tablet, customer appCurrent state, read from the order database, so a tablet that lost its connection can see what really happened.GET/v1/orders/{id}/eventsCalled by Support toolsThe order's events in sequence, to see which consumers have handled each one.
Data
orders
Key:
order_idorder_id, restaurant_id, state, version, accepted_at
The state machine. The version column turns a stale double accept into a no-op.
outbox
Key:
event_idevent_id, order_id, type, payload, created_at
Written in the same transaction as the order's new state; the relay reads it from the database log and publishes it.
processed_events
Key:
(consumer, event_id)consumer, event_id, processed_at
Each consumer records the event id with its own work, in one transaction, and skips ids it has seen.
idempotency_keys
Key:
(restaurant_id, key)restaurant_id, key, order_id, response, expires_at
Keeps the first response to an accept, so retries from the tablet get the same answer.
From their own write-ups
How real companies did it
Debezium's write-up explains why a service cannot simply write to its database and Kafka: Kafka cannot join a distributed transaction, so one write can succeed without the other. Its outbox pattern inserts the event in the same transaction and streams it from the database log; delivery is at least once, so each event carries a unique id that consumers check before processing.
Debezium: Reliable Microservices Data Exchange With the Outbox Pattern (2019) ↗SeatGeek adopted the transactional outbox to solve the dual-write problem when distributing data in real time. Instead of an outbox table, which needs a separate cleanup process, it writes events straight into Postgres's write-ahead log with pg_logical_emit_message and streams them to Kafka with Debezium.
SeatGeek: The Transactional Outbox Pattern (2025) ↗Stripe's post on idempotency starts from the fact that a client cannot tell whether a failed call took effect. Clients send a unique idempotency key with each operation and retry with exponential backoff and jitter; the server recognises a repeated key and returns the original result, so a payment is made only once.
Stripe: Designing robust and predictable APIs with idempotency (2017) ↗
In the interview
Follow-up questions
- The relay is down for ten minutes during dinner. What happens to orders?
- They keep being accepted: each acceptance commits its outbox row as usual. Nothing reaches the kitchen or dispatch until the relay is back, then the backlog drains in order. That is why relay lag needs an alert measured in seconds, and why the relay should run with a standby.
- How exactly does a consumer avoid acting on an event twice?
- In the same database transaction as its own work (say, creating the kitchen ticket), it inserts the event id into a processed-events table with a unique constraint. A repeat fails that insert, so the transaction rolls back and the duplicate is ignored. If the work and the record were separate, a crash between them would reopen the gap.
- The kitchen receives 'cancelled' before 'accepted' for the same order. How do you prevent it?
- Key every event by order id, so all of one order's events go to the same partition and are read in the order they were written. Consumers can also check a version number and ignore an event older than the state they already hold.
Your turn
Check yourself
Why does 'save the order, then publish the event' lose orders?
A publish times out and the service retries. Why can the kitchen end up with two copies?
With an outbox, delivery is at least once. What makes the effect happen exactly once?
Related
Sources: Debezium: Reliable Microservices Data Exchange With the Outbox Pattern (2019) ↗ · SeatGeek: The Transactional Outbox Pattern (2025) ↗ · Stripe: Designing robust and predictable APIs with idempotency (2017) ↗ · Report a problem
A typical design, informed by public write-ups, not any one company's exact system. Diagrams are our own. Numbers are illustrative. All wording is original and paraphrases the linked sources.