Design the checkout flow for an online store

    An e-commerce checkout turns a cart into a paid order while keeping inventory, payment, and order creation consistent across services. The interview centers on avoiding overselling and coordinating a multi-step transaction that can fail anywhere.

    Web Apps, 12 components, about 40 min

    1. Scope

    In scope

    • • Cart
    • • Inventory reservation
    • • Payment
    • • Order creation
    • • Failure rollback

    Out of scope

    • • Catalog search ranking
    • • Recommendations
    • • Shipping/logistics internals

    2. Functional requirements

    Must have

    • • Add to cart and check out
    • • Reserve inventory (no oversell)
    • • Charge payment
    • • Create order atomically across services

    Nice to have

    • • Promotions/coupons
    • • Guest checkout
    • • Saved payment methods

    3. Non-functional requirements

    ConsistencyStrong on inventory + payment
    Availability99.99% (revenue path)
    LatencyCheckout < 2 s
    Spike toleranceFlash sales / Black Friday

    4. Back-of-envelope estimation

    Assumptions

    • • 10K checkouts/sec peak
    • • Catalog read-heavy
    • • Inventory contended on hot SKUs
    • • Order ~2 KB

    Derived numbers

    Checkout QPS~10K/s peak
    Catalog reads~100K/s— Browse ≫ buy
    Order storageTBs/year

    5. API design

    POST/api/cart

    Add/update cart items

    POST/api/checkout

    Reserve → authorize → create order → capture (saga)

    GET/api/catalog/search

    Browse/search products (read model)

    6. High-level design

    What each box in the reference diagram is responsible for:

    Cart Service — Redis (24h TTL) + DB for logged-in carts.
    Catalog (read) — Denormalized Elasticsearch read model.
    Order Service — Owns checkout; persists orders; emits events.
    Inventory — Optimistic-locked stock counts.
    Payment — Charges via PSP.
    Saga Orchestrator — Coordinates reserve → authorize → create order → capture, with a compensating action for each step.
    Kafka Outbox — Fans order events to warehouse, email, analytics.

    7. Data model

    Inventory DB: stock(sku, qty)— Atomic conditional decrement
    Order DB: orders(id, user, items, total, status)

    8. Deep dives

    Avoiding overselling

    Hot SKUs in a flash sale get hammered. Reserve stock with one atomic conditional update: UPDATE stock SET qty = qty - :n WHERE sku = :sku AND qty >= :n. Zero rows updated means there is not enough stock, so fail fast. No read-then-write, and it scales far better than pessimistic row locks under contention.

    Checkout as a saga

    Checkout spans inventory, payment, and order services — no single ACID transaction. Use a saga: reserve inventory → authorize payment → create order → capture payment. If a step fails, run the compensating actions in reverse (void the authorization, release the reservation). The saga orchestrator tracks state and drives compensation, giving eventual consistency with correctness.

    Read/write separation (CQRS)

    Browsing is read-heavy and tolerant of slight staleness; checkout writes need consistency. Serve catalog/search from a denormalized read model (Elasticsearch) updated asynchronously, while the write side keeps authoritative inventory/orders. This is CQRS in practice.

    Reliable downstream via outbox

    After an order is created, warehouse, email, and analytics must hear about it. Write events to a Kafka outbox in the same transaction as the order, then relay — avoiding the dual-write problem and guaranteeing at-least-once delivery.

    9. Bottlenecks

    • Hot-SKU contention → optimistic locking, queue per SKU
    • Distributed checkout consistency → saga + compensation
    • Flash-sale spikes → autoscale, cache catalog, queue

    10. Trade-offs

    When to reserve inventory

    Choose: Reserve at checkout start with a TTL (for example 10 minutes), commit on payment success, release on expiry.

    Instead of: Decrement only on payment: no stale holds, but two buyers can pay for the last unit and one must be refunded.

    Refunding after payment is worse for customers than briefly holding stock. The TTL bounds the cost of abandoned carts.

    Strong or eventual consistency

    Choose: Strong for inventory, orders and payments (single-row conditional updates); eventual for catalog, search and recommendations.

    Instead of: Eventual everywhere: higher availability, but overselling.

    Only a few tables carry money or stock; keeping strong consistency there keeps the expensive guarantees small.

    Checkout as one transaction or a saga

    Choose: Saga: reserve stock → authorize payment → create order → capture, each step with a compensating action.

    Instead of: Distributed transaction across services (2PC): simpler reasoning, but blocks on coordinator failure and couples availability.

    External payment providers cannot join a database transaction anyway; sagas with idempotent steps are the practical answer.

    11. Failure modes

    Flash sale on one SKU

    One inventory row receives thousands of updates per second; lock contention times out checkouts.

    Mitigation: Split stock into N sub-counters (buckets) or pre-issue purchase tokens through a queue; admit buyers at the rate the row can sustain.

    Payment provider times out after charging

    Customer charged, no order.

    Mitigation: Idempotency key per checkout, then reconcile by querying the provider before retrying; a reconciliation job closes gaps from webhooks.

    Cart service down

    Users cannot buy.

    Mitigation: Keep the cart client-side as a fallback and rebuild server-side state at checkout; carts are not money.

    12. What interviewers probe

    Answer each one yourself before opening it.

    Write the SQL that prevents overselling.Show a strong answer

    UPDATE inventory SET available = available - :qty WHERE sku = :sku AND available >= :qty; check rows affected = 1. No read-then-write.

    How does search stay in sync with the catalog?Show a strong answer

    Change data capture from the catalog DB into Kafka, consumed by an indexer; accept seconds of lag and re-index from a snapshot when mappings change.

    How do you price-check at checkout if prices changed?Show a strong answer

    Order lines snapshot the price and currency at reservation; checkout validates against current price and asks the user to confirm if it changed.

    What are your SLOs on Black Friday?Show a strong answer

    Checkout success rate above 99.9% and p99 under 2 s, measured per step; load-test to 3× forecast and pre-scale, with a queue page as a controlled overflow.

    Sign in to track this guide in your curriculum progress.