Designing an Order Management System

A complete system design for an e-commerce order management system using the 18-step framework: requirements, labelled assumptions and capacity calculations, API, data model, architecture, saga with transactional outbox, idempotent payments, failure handling, scaling and trade-offs — including when a modular monolith is the better choice.

On this page

Overview#

This case study designs the system that takes a customer’s order in an online shop, reserves stock, takes payment and hands the order to fulfilment. It follows the 18-step framework and reuses the retail domain from the DDD articles.

The ordering core is implemented as a runnable Spring Boot service in Hexagonal Architecture with Spring Boot. This page designs the system around it.

Functional requirements#

In scope:

  1. A customer places an order containing one or more products from their cart.
  2. Stock is reserved so that the same item is not sold twice.
  3. The customer is charged once, through an external payment provider.
  4. The customer can view the order’s status and history.
  5. The customer can cancel an order before it is shipped; the payment is refunded.
  6. Paid orders are passed to the shipping system.
  7. Customers are notified of status changes (e-mail).

Out of scope: catalogue management, pricing and promotions engine, search, returns and exchanges, warehouse operations.

Nonfunctional requirements#

AttributeRequirement
CorrectnessA customer is never charged twice for one order. A confirmed order is never lost.
AvailabilityOrder placement: 99.95% monthly. Order history may degrade (stale) during incidents.
Latencyp99 under 500 ms for placing an order, excluding the payment provider’s time; order status reads p99 under 200 ms.
ConsistencyStock overselling is avoided through reservations; brief inconsistencies between services are acceptable if they are reconciled within minutes.
DurabilityOrders retained for 7 years for legal and accounting reasons.
SecurityCard data never touches our systems (the provider’s hosted fields or tokens handle it), keeping our PCI DSS scope small. Customers see only their own orders.

Traffic and capacity estimates#

Assumptions — no figures were given, so these are stated explicitly and should be confirmed:

#AssumptionValue
A1Daily active users10 million
A2Orders per DAU per day0.05 (one order every 20 days)
A3Order-status and history reads per order10 over the order’s life
A4Peak-to-average ratio5× normally; 20× during a flash sale
A5Average order size3 lines; about 2 KB stored per order including lines
A6Order events (status changes) per orderAbout 6
A7Retention7 years

Calculations:

Orders/day          = 10,000,000 × 0.05                  = 500,000
Average orders/s    = 500,000 / 86,400                   ≈ 6
Peak orders/s       = 6 × 5                              ≈ 30
Flash-sale orders/s = 6 × 20                             ≈ 120

Status reads/day    = 500,000 × 10                       = 5,000,000
Average reads/s     = 5,000,000 / 86,400                 ≈ 58
Peak reads/s        = 58 × 5                             ≈ 290

Events/day          = 500,000 × 6                        = 3,000,000
Peak events/s       = 3,000,000 / 86,400 × 20            ≈ 700  (flash sale)

Order storage/year  = 500,000 × 365 × 2 KB               ≈ 365 GB
Order storage/7 y   ≈ 2.6 TB  (before replicas and indexes)

Conclusions:

  • Write rates — even 120 orders per second during a flash sale — are well within a single relational primary’s capacity. Orders do not need sharding.
  • The hot spot during flash sales is stock reservation for a few popular products, not order volume. Many concurrent writes to the same stock row cause lock contention.
  • Event volume (hundreds per second at peak) is comfortable for any mainstream message broker.
  • Storage grows steadily; plan to archive orders older than about 2 years from the transactional database to cheaper storage while keeping them queryable for the 7-year retention.

Architecture choice: services or modular monolith?#

Before drawing services, apply the decision criteria.

FactorSituation (assumed)
TeamsFour teams: Ordering, Inventory, Payments integration, Fulfilment & notifications
Load profileInventory has flash-sale spikes; ordering and notifications have very different scaling and release needs
CompliancePayment integration benefits from isolation for audit
PlatformContainer platform, CI/CD and observability already exist

With four teams, differing scaling needs and an existing platform, separate services per bounded context are justified.

API design#

Public API (through the API gateway):

POST /orders
Idempotency-Key: 8f14e45f-ceea-467f-a8c5-4b1d6d5e3c2a
Content-Type: application/json

{ "cartId": "c-7781", "shippingAddressId": "a-12", "paymentToken": "tok_…" }

→ 202 Accepted
Location: /orders/3f2b…
{ "orderId": "3f2b…", "status": "PENDING" }
GET  /orders/{orderId}                       → 200 { status, lines, total, history[] }
GET  /customers/me/orders?cursor=…&limit=20  → 200 { items[], nextCursor }
POST /orders/{orderId}/cancel                → 202 Accepted | 409 Conflict (already shipped)

Design decisions:

  • 202 Accepted, not 201 Created with a final status, because reserving stock and charging happen asynchronously. The client polls GET /orders/{id}, or receives a push notification.
  • Idempotency-Key header: if the client retries after a network failure, the server returns the original result instead of creating a second order.
  • Cursor-based pagination for order history: it remains stable while new orders are being added, unlike offset pagination.
  • Prices are not accepted from the client; the server reads them from the cart and pricing services.

Data model#

Each service owns its data. No service reads another’s tables.

Data owned by each service
erDiagram
    ORDERS ||--|{ ORDER_LINES : contains
    ORDERS ||--o{ ORDER_STATUS_HISTORY : records
    ORDERS {
        uuid order_id PK
        string customer_id
        string status
        decimal total_amount
        string currency
        string idempotency_key UK
        bigint version
        timestamp placed_at
    }
    ORDER_LINES {
        uuid order_id FK
        int line_no
        string product_id
        int quantity
        decimal unit_price
    }
    ORDER_STATUS_HISTORY {
        uuid order_id FK
        string status
        timestamp at
    }
    OUTBOX {
        uuid event_id PK
        string aggregate_id
        string event_type
        json payload
        timestamp created_at
        timestamp published_at
    }
    STOCK {
        string product_id PK
        string warehouse_id PK
        int on_hand
        int reserved
        bigint version
    }
    RESERVATIONS {
        uuid reservation_id PK
        uuid order_id UK
        string status
        timestamp expires_at
    }
    PAYMENTS {
        uuid payment_id PK
        uuid order_id UK
        string provider_reference
        string status
        decimal amount
    }

Ordering owns ORDERS, ORDER_LINES, ORDER_STATUS_HISTORY and its own OUTBOX. Inventory owns STOCK and RESERVATIONS. Payments owns PAYMENTS. Each service has its own outbox table.

Key access patterns and the indexes they need:

Access patternIndex
Get order by IDPrimary key
A customer’s orders, newest first(customer_id, placed_at DESC)
Detect a duplicate submissionUnique idempotency_key
Find stuck orders (pending longer than 15 minutes)(status, placed_at)
Unpublished outbox events(published_at) where null, or a partial index

High-level architecture#

Order management — high-level architecture
flowchart LR
    C["Web / mobile clients"] --> GW["API gateway<br/>authN, rate limiting"]
    GW --> ORD["Ordering service"]
    ORD --- ODB[("Orders DB<br/>+ outbox")]
    ORD -- "price & cart lookup" --> CART["Cart / Pricing<br/>(existing)"]
    ORD -- "events via outbox" --> BUS{{"Message broker"}}
    BUS --> INV["Inventory service"]
    INV --- IDB[("Inventory DB<br/>+ outbox")]
    BUS --> PAY["Payment service"]
    PAY --- PDB[("Payments DB<br/>+ outbox")]
    PAY -- "HTTPS, idempotency key" --> PSP["Payment provider<br/>(external)"]
    BUS --> FUL["Fulfilment adapter"]
    FUL --> SHIP["Shipping system<br/>(existing)"]
    BUS --> NOTI["Notification service"]
    NOTI --> MAIL["E-mail provider<br/>(external)"]
    INV -- "events" --> BUS
    PAY -- "events" --> BUS

Open the interactive diagram: Order management architecture Standalone page with zoom, light/dark themes and SVG/PNG export, generated with Archify. Diagram source.

Component responsibilities#

ComponentResponsible forNot responsible for
API gatewayTLS termination, authentication (token validation), rate limiting, routingBusiness rules
Ordering serviceOrder aggregate and lifecycle; idempotent order creation; orchestrating the order saga; order queriesStock levels, payment details
Inventory serviceStock levels; reservations with expiry; confirming or releasing reservationsPrices, orders
Payment serviceCharging and refunding through the provider; idempotency towards the provider; reconciliationDeciding whether an order should be charged
Message brokerDurable, at-least-once delivery of events and commandsBusiness logic, exactly-once guarantees
Fulfilment adapterTranslating paid orders into the shipping system’s format (an anti-corruption layer)Warehouse operations
Notification serviceSending e-mails from events; deduplication; templatesDeciding when an order changes state

Data flow#

The core workflow spans three services, so it cannot be one ACID transaction. It is a saga: a sequence of local transactions, each publishing an event that triggers the next step, with compensating actions if a later step fails.

This design uses an orchestrated saga: the Ordering service holds the saga state (inside the Order aggregate) and sends commands. Orchestration keeps the workflow visible in one place, which matters for a core business process. Choreography — each service reacting to the others’ events with no central coordinator — is a valid alternative for simpler flows.

Place order saga — happy path
sequenceDiagram
    autonumber
    participant Client
    participant ORD as Ordering
    participant BUS as Broker
    participant INV as Inventory
    participant PAY as Payment
    participant PSP as Provider
    Client->>ORD: POST /orders (Idempotency-Key)
    ORD->>ORD: TX: insert order PENDING + outbox ReserveStock
    ORD-->>Client: 202 Accepted {orderId}
    ORD-)BUS: ReserveStock (outbox relay)
    BUS-)INV: ReserveStock
    INV->>INV: TX: reserve items + outbox StockReserved
    INV-)BUS: StockReserved
    BUS-)ORD: StockReserved
    ORD->>ORD: TX: status STOCK_RESERVED + outbox ChargePayment
    ORD-)BUS: ChargePayment
    BUS-)PAY: ChargePayment
    PAY->>PSP: charge(amount, idempotencyKey = orderId)
    PSP-->>PAY: succeeded (ref)
    PAY->>PAY: TX: record payment + outbox PaymentCaptured
    PAY-)BUS: PaymentCaptured
    BUS-)ORD: PaymentCaptured
    ORD->>ORD: TX: status PAID + outbox OrderPaid, ConfirmReservation
    ORD-)BUS: OrderPaid → Fulfilment, Notifications
    ORD-)BUS: ConfirmReservation → Inventory

The transactional outbox#

Every step must change its own database and publish a message atomically. Writing to the database and then publishing to the broker is unsafe: if the process crashes between the two, the state changes but no message is ever sent (or the reverse).

The transactional outbox solves this. The service writes the event to an OUTBOX table in the same local transaction as the state change. A separate relay — a polling publisher, or change data capture such as Debezium reading the database log — publishes outbox rows to the broker and marks them as published.

Transactional outbox
flowchart LR
    subgraph Service["Ordering service"]
        H["Command handler"] -- "one local transaction" --> T[("orders + outbox")]
    end
    T -- "poll or CDC" --> R["Outbox relay"]
    R -- "publish, then mark sent" --> B{{"Broker"}}
    B --> Consumers["Consumers<br/>(must be idempotent)"]

The relay can publish a message and then crash before marking it as sent, so the message will be published again. The outbox therefore gives at-least-once delivery, and every consumer must be idempotent. Consumers record processed event IDs in an inbox table, or make their updates naturally idempotent — for example, “set status to PAID if it is STOCK_RESERVED”.

Order state machine#

Order states, including compensation
stateDiagram-v2
    [*] --> PENDING
    PENDING --> STOCK_RESERVED : StockReserved
    PENDING --> REJECTED : StockUnavailable
    STOCK_RESERVED --> PAID : PaymentCaptured
    STOCK_RESERVED --> PAYMENT_FAILED : PaymentDeclined
    PAYMENT_FAILED --> [*] : ReleaseReservation sent
    PAID --> SHIPPED : Shipped
    PAID --> CANCELLED : customer cancels → RefundPayment + ReleaseReservation
    REJECTED --> [*]
    SHIPPED --> [*]
    CANCELLED --> [*]

Caching#

  • Order status reads: about 290 per second at peak — modest. Serve from the Ordering database’s read replica first; add a cache only if measurements require it. If a cache is added, invalidate on the Order* events and use a short TTL (for example, 30 seconds) as a safety net.
  • Prices and product details at order time: always read from the authoritative pricing service, never from a cache, so that the customer is charged the confirmed price. Cache only for display.
  • Idempotency results: store the response for each Idempotency-Key for 24 hours (in the orders database, or in a cache with persistence) so that retries get the same answer.

Database selection#

ServiceChoiceReason
OrderingRelational (for example, PostgreSQL)Transactions for order + outbox; relational queries for history; modest write rate
InventoryRelationalAtomic conditional updates (UPDATE … SET reserved = reserved + :q WHERE on_hand - reserved >= :q); strong consistency for stock
PaymentsRelationalAuditability, unique constraints for idempotency
Order history archiveObject storage in a columnar format, queryable by a SQL engineCheap long-term retention

A document or wide-column store would also work for orders, but nothing in the access patterns requires one, and relational transactions make the outbox simple.

Scaling#

  • Stateless services scale horizontally behind the gateway. Ordering needs few instances; notifications scale with queue depth.
  • Read replicas handle order-history reads.
  • Flash-sale stock contention is the real bottleneck. Options, from simplest:
    1. Atomic conditional UPDATE statements (no read-then-write), so each reservation is a single short row lock.
    2. Split hot stock into several buckets (for example, 10 rows of 100 units each) and reserve from a random bucket, which spreads lock contention.
    3. Put a queue in front of reservations for the hot product and process them sequentially, giving customers a “you’re in the queue” experience.
    4. Pre-allocate a fixed quantity of stock to the sale in an in-memory store with atomic decrements, reconciling with the database afterwards.
  • Broker partitions keyed by orderId preserve per-order event ordering while allowing parallel consumers.

Failure handling#

FailureEffectHandling
Client retries POST /orders after a timeoutPossible duplicate orderIdempotency-Key with a unique constraint; the retry returns the original order
Ordering crashes after its DB commit, before publishingEvent never sentTransactional outbox: the relay publishes it on recovery
Duplicate event deliveryDouble processingIdempotent consumers (inbox table or conditional updates)
Stock unavailableOrder cannot proceedStockUnavailable → order REJECTED; customer notified
Payment declinedNo paymentPAYMENT_FAILED → compensating ReleaseReservation
Payment provider times outUnknown outcome — the charge may or may not have happenedNever blindly re-charge with a new key. Retry with the same idempotency key (the order ID), or query the provider by that key. If still unknown, mark the payment UNKNOWN and let a reconciliation job resolve it.
Payment succeeded but PaymentCaptured was not processedCustomer charged, order stuck in STOCK_RESERVEDOutbox guarantees eventual publication; a stuck-order monitor alerts on orders in a non-final state for more than 15 minutes
Reservation never confirmed (saga stalled)Stock locked indefinitelyReservations have an expiry (for example, 15 minutes); a sweeper releases expired ones
Inventory service downOrders stay PENDINGCommands wait durably in the broker; order placement still returns 202; the backlog drains on recovery. Alert on queue age.
Broker downNo events flowOutbox rows accumulate safely in each database; the relay resumes later
E-mail provider downNotifications delayedRetry with exponential backoff and jitter; dead-letter queue after N attempts; notifications never block the order flow

Synchronous calls — from the gateway to Ordering, and from Ordering to Cart/Pricing — use timeouts, limited retries with backoff for idempotent reads, and a circuit breaker, so that a slow pricing service fails fast instead of exhausting Ordering’s threads.

Security#

  • Authentication at the gateway (OAuth 2.0 / OpenID Connect tokens); services validate the token’s signature and claims again (“zero trust” inside the network) or rely on mutual TLS between services.
  • Authorisation: a customer can read and cancel only their own orders. Check order.customerId == token.subject in the Ordering service, not only at the gateway.
  • Payment data: card details go directly from the browser to the provider (hosted fields or a token); our systems store only tokens and references.
  • Idempotency keys are scoped per customer, so one customer cannot replay another’s key.
  • Rate limiting per customer and per IP address on POST /orders protects against abuse and bots during flash sales.
  • Encryption in transit (TLS everywhere) and at rest; personal data such as addresses is minimised and access-controlled for GDPR-style obligations.
  • Secrets (provider API keys) are kept in a secrets manager, never in configuration files.

Observability#

  • Metrics (RED per service: rate, errors, duration), plus business metrics: orders placed per minute, payment success rate, saga completion time, and orders stuck per state.
  • Distributed tracing with OpenTelemetry. Trace context is propagated through HTTP headers and message headers, so one trace follows an order across all services.
  • Structured logs carrying orderId and traceId.
  • Alerts on symptoms customers feel: payment success rate dropping, saga completion p99 rising, outbox backlog age, consumer lag, and orders stuck in a non-final state.

Bottlenecks#

  1. Hot stock rows during flash sales — addressed by the strategies under Scaling.
  2. Payment provider latency and rate limits — outside our control. Bound concurrency towards the provider (a bulkhead) and queue the excess.
  3. Outbox relay throughput — polling at hundreds of events per second is fine; switch to change data capture if volume grows by an order of magnitude.
  4. Order-history queries for heavy users — served from replicas, with archiving to keep tables lean.

Design alternatives#

AlternativeWhy not chosen (for these requirements)
Modular monolithPreferable for a single team; rejected here only because of the four-team structure and different scaling profiles. See the trade-off above.
Synchronous orchestration over HTTP (Ordering calls Inventory, then Payment, in one request)Simpler to read, but availability multiplies across services, and a crash mid-request leaves inconsistent state without an outbox.
Choreographed sagaNo central coordinator; fine for short flows, but the order workflow is core business logic that benefits from one visible owner.
Distributed transaction (two-phase commit)Not supported by brokers or most payment providers; it couples availability and holds locks across services.
Event sourcing for ordersProvides a full history and temporal queries, but adds event-schema evolution and projection complexity that the requirements do not justify; a status-history table covers the audit need.

Trade-offs#

  • Eventual consistency for a responsive API. The customer sees PENDING for a moment instead of a final answer. In exchange, order placement stays available when Inventory or Payments are degraded.
  • Operational complexity for team autonomy. Four services, a broker and outbox relays cost more to run than one application. This is justified by four independent teams, not by load.
  • At-least-once delivery plus idempotency instead of exactly-once delivery. This is simpler and more robust, but every consumer must be written carefully.
  • Reservations with expiry prevent overselling, at the cost of temporarily locking stock that may never be bought.

Interview follow-up questions#

How do you guarantee a customer is never charged twice? Use an idempotency key end to end: the client’s key for order creation, and the order ID as the idempotency key towards the provider. Add a unique constraint on order_id in PAYMENTS, make the ChargePayment handler idempotent, and treat timeouts as unknown, resolving them by querying the provider rather than re-charging.

What if the payment succeeds but the order service never hears about it? The payment service records the payment and its outbox event in one transaction, so the event is eventually published. A monitor alerts on orders stuck in STOCK_RESERVED, and nightly reconciliation compares provider settlements with order states.

How would you handle 100× traffic during a flash sale? Shed or queue load at the edge with rate limiting and a waiting room; split hot stock rows or pre-allocate sale stock; scale stateless services; and protect the payment provider with a bulkhead and queue.

Why not use two-phase commit? The broker and the payment provider do not participate in XA transactions. 2PC also blocks resources while waiting for the coordinator and reduces availability to that of the least available participant.

How would this change for a single team? Keep the same bounded contexts as modules in one Spring Boot application, with in-process events and an outbox (for example, Spring Modulith’s event publication registry). Saga steps that touch only local modules could even use a single database transaction.

Summary#

  • Requirements and labelled assumptions show that order volume is modest; the hard problems are correctness (double charges, lost events) and contention (hot stock).
  • A saga with a transactional outbox and idempotent consumers keeps services consistent without distributed transactions.
  • Payment timeouts are treated as unknown and resolved with idempotency keys and reconciliation.
  • Services are justified by team structure; a modular monolith with the same boundaries is the right choice for a single team.

References#