Quick revision
The short answer
The order service owns an Order aggregate with lines, totals and a status state machine, stored in a relational database with @Version. An orchestrated saga reserves inventory, authorises payment, confirms the order and requests shipment, and runs compensations that release stock and void or refund payment when a step fails. Commands and events travel through a transactional outbox to Kafka with idempotent consumers, cancellation is a transition allowed only before SHIPPED, every saga step has a timeout with a job that finds stuck orders, and order history is served from read models built from events.
What to remember
In this answer, we designed an order service around an Order aggregate with a state machine and @Version. An orchestrated saga reserves inventory, authorises payment, confirms and ships, with compensations that release stock and void or refund payment. A transactional outbox and idempotent consumers make messaging reliable, cancellation is a transition allowed only before SHIPPED, step deadlines and a job catch stuck orders, and read models built from events serve order history.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The order service owns an Order aggregate with lines, totals and a status state machine, stored in a relational database with @Version. An orchestrated saga reserves inventory, authorises payment, confirms the order and requests shipment, and runs compensations that release stock and void or refund payment when a step fails. Commands and events travel through a transactional outbox to Kafka with idempotent consumers, cancellation is a transition allowed only before SHIPPED, every saga step has a timeout with a job that finds stuck orders, and order history is served from read models built from events.
Why this question is asked
An order touches several services that each own their own data: inventory, payment and shipping. There is no single database transaction that covers all of them, so the order service must reach a consistent outcome through messages, with failures at any point. Interviewers ask this design question to see whether we can apply the saga pattern properly: who coordinates, how each step is undone, how messages stay reliable and how stuck orders are found. In this answer, we will design the aggregate, walk through the saga and its compensations, make messaging reliable, handle cancellation and timeouts, add read models, and answer the follow-up on price changes.
1The Order Aggregate
An aggregate is a cluster of objects that changes together and protects its own rules. Here it is the Order with its OrderLines, the totals and the status. The order service is the only writer of this data, stored in PostgreSQL or MySQL. Its rules live inside the entity: totals equal the sum of lines, lines cannot change after payment, and status changes only through allowed transitions such as PENDING, INVENTORY_RESERVED, PAYMENT_AUTHORIZED, CONFIRMED, SHIPPED, CANCELLED. A @Version column provides optimistic locking, so a customer's cancel and a saga reply arriving at the same moment cannot both apply.
2The Orchestrated Saga
A saga is a sequence of local transactions, one per service, where each step has a compensating action that undoes it if a later step fails. In an orchestrated saga, one component, here a saga orchestrator inside the order service, decides the next step and sends commands; services reply with events. The alternative, choreography, has each service react to the others' events, which is fine for two steps but hard to follow for four with compensations. With orchestration, the whole flow is visible in one place and in one saga state row per order.
9 messages in order. Play them, or step through them one at a time.
Each arrow is a Kafka message; the orchestrator stores the saga step after each reply.
3Compensations When a Step Fails
Suppose stock is reserved and then payment is declined. The orchestrator moves the order to CANCELLING and sends ReleaseStock; when inventory confirms, the order becomes CANCELLED and the customer is told why. If shipping later rejects the request, for example an address that cannot be delivered, the orchestrator voids the payment authorisation (or refunds, if it was already captured) and releases stock. Compensations are business actions, not rollbacks: they create new facts, such as a void, and they can themselves fail, so they are retried until they succeed and are idempotent.
The rule of thumb is to put steps that are easy to undo first and steps that are hard to undo last. Reserving stock is cheap to release; authorising payment can be voided at no cost; capturing money and shipping goods are hard to undo, so they come last.
| Step | Command | Compensation |
|---|---|---|
| 1 | Reserve inventory | Release the reservation |
| 2 | Authorise payment | Void the authorisation |
| 3 | Confirm order | Mark cancelled, notify customer |
| 4 | Request shipment | Cancel shipment if not yet picked |
| 5 | Capture payment on shipment | Refund (after shipping, a return flow) |
4Reliable Messaging With an Outbox
Each saga step changes the order and sends a command. If we commit the database change and then publish to Kafka, a crash between the two leaves the saga stuck; if we publish first and the commit fails, other services act on a step that never happened. The transactional outbox solves this: the state change and an outbox row with the command are written in the same database transaction, and a relay, a poller or Debezium change data capture, publishes outbox rows to Kafka.
Kafka delivers at least once, so every consumer is idempotent. The processed-messages table, a unique message id written in the same transaction as the business change, makes a second delivery a no-op. Downstream services also use stable keys: the payment authorisation uses the order's payment key, so a repeated command does not authorise twice.
5Cancellation, Timeouts and Stuck Orders
Customer cancellation is just another transition: the Order allows cancel() in any state before SHIPPED and rejects it afterwards, when the customer must use the returns flow instead. When it is allowed, the orchestrator runs the compensations for the steps already done, release stock and void payment, exactly as it would for a failure.
Messages can be lost in effect, for example when a downstream service is down for hours. So each saga step records a deadline, such as 5 minutes for inventory and 2 minutes for payment. A scheduled job queries for orders whose current step has passed its deadline and either resends the command (safe, because handlers are idempotent) or, after several attempts, starts compensation and raises an alert. Without this job, an order can wait in PAYMENT_PENDING forever while the customer wonders what happened.
6Read Models for Order History
The write model is optimised for enforcing rules, not for queries like "show my last 50 orders with tracking status and refund amounts". The order service and others publish events, and a read model, a denormalised table or a search index, is built from them for the order history page and customer support screens. It may lag the write model by a second, which is acceptable for history, while the confirmation page right after checkout reads from the write model.
7Follow-up questions
How would you handle a price change between adding to cart and placing the order?
The cart shows prices with a quote, and when the customer places the order, the order service asks the pricing service for current prices and stores them on the order lines; the order is always created with prices locked at that moment. If they differ from what the cart showed, we either honour the cart price for a short validity window, for example 30 minutes, if the business wants that, or show the customer the new total and require confirmation before creating the order. The stored line prices are then final for that order, and later price changes never affect it.
Why not use a distributed transaction such as two-phase commit?
Two-phase commit needs every participant to support it and holds locks across services until the coordinator decides, so one slow or failed service blocks the others. Kafka and most external services such as payment providers do not take part in XA transactions anyway. Sagas accept that the system is briefly inconsistent, for example stock reserved for an order that will be cancelled, and repair it with compensations, which scales and fails more gracefully.
8Conclusion
In this answer, we designed an order service around an Order aggregate with a state machine and @Version. An orchestrated saga reserves inventory, authorises payment, confirms and ships, with compensations that release stock and void or refund payment. A transactional outbox and idempotent consumers make messaging reliable, cancellation is a transition allowed only before SHIPPED, step deadlines and a job catch stuck orders, and read models built from events serve order history.
Open with
I would make the order service own an Order aggregate, the order, its lines, totals and a status state machine, stored in a relational database with a @Version column so concurrent changes cannot both win.
Then, if they want more
Since payment, inventory and shipping each own their data, there is no single transaction, so I would use an orchestrated saga run by the order service. It sends ReserveStock, then AuthorizePayment, then confirms the order, then RequestShipment, and captures payment when the order ships. Each step has a compensation: release the stock, void or refund the payment, cancel the shipment.
I order the steps so the easy-to-undo ones come first and capture and shipping come last. Messaging must be reliable, so every state change and the next command are written in the same transaction using a transactional outbox, and a relay publishes to Kafka. Consumers are idempotent by message id, because Kafka is at-least-once.
Customer cancellation is a transition allowed only before SHIPPED; when it happens the orchestrator runs the compensations for whatever steps are already done. Each saga step also has a deadline, and a scheduled job finds orders stuck past it, resends the command or starts compensation and alerts.
For the customer's order history and support screens, I would build read models from events, separate from the write model. On price changes, I would lock prices on the order lines when the order is created, and if they differ from the cart, either honour a recent quote or ask the customer to confirm.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Design an order management service for an e-commerce site that takes orders, coordinates...": https://scrivio-interview-library.netlify.app/architecture/backend-system-design/65cf120b61/