KatInterview

Build a Consistent Online Store with Cart and Payment Handling

System DesignModel answer
HardModel answerSystem Designe-commercedistributed-systemstransactions

Last updated

Design an online shopping system handling product discovery, shopping carts, order placement, inventory management, and payment processing with consistency guarantees.

You need to design an online shopping system. Explain your approach to letting customers browse products, manage their shopping cart, submit orders, and track the results—while keeping product stock, payment transactions, and order records consistent even under concurrent access and failures.

Scope and Boundaries

Design for physical goods sold through distinct product codes (SKUs). Your system should include:

  • A browsable product catalog with search
  • Shopping cart functionality
  • Order submission and checkout
  • Stock tracking and allocation
  • Integration with a third-party payment processor
  • Order status visibility

You may omit personalized recommendations, multi-seller marketplaces, and warehouse-specific fulfillment logic.

Technical Considerations

  • No specific traffic numbers, uptime targets, or geographic requirements are given. Discuss how these would influence your choices rather than inventing constraints.
  • The system must handle multiple customers trying to buy the same limited-stock item simultaneously.
  • Customers may retry a checkout action, and payment webhooks may arrive more than once.
  • Stock may run out between when a customer adds an item to their cart and when they complete checkout.

Questions You Should Ask

Before diving into design, clarify:

  • Should adding items to a cart reserve inventory, or does reservation happen only during checkout?
  • Can product prices or stock status change after a customer has added items to their cart?
  • What guarantees does the payment provider offer? Can we make idempotent payment requests? Does it support a two-phase authorize-then-capture flow? Can we query definitive payment status?
  • If an order contains multiple items, must we allocate stock for all of them or none?
  • Which parts of product browsing can tolerate stale data, and where do we need real-time accuracy?

Part 1: Core Architecture and Data Flow

Describe the major components and data structures needed for browsing, cart management, checkout, and order tracking. Explain where authoritative data lives for pricing, inventory, and order state.

Expected coverage:

  • How read-optimized catalog views differ from the authoritative data used during purchase decisions
  • Data models for products with variants, cart line items, finalized order records, and order identity
  • How you partition data and requests by customer to maintain isolation

Part 2: Ensuring Checkout Correctness

Walk through the checkout flow for a multi-item order. Cover inventory allocation, price confirmation, payment initiation, handling success and failure, and dealing with duplicate requests.

Expected coverage:

  • How you make an all-or-nothing decision about stock availability and track order progress through explicit states
  • Techniques to make customer checkout requests and payment provider callbacks idempotent
  • Recovery strategy when payment completes successfully but the acknowledgment is lost, avoiding double charges or double-selling inventory

Part 3: Scaling and Reliability

Discuss how you would scale product browsing independently from checkout transactions, handle a high-demand item with limited stock, and identify orders that get stuck between payment and fulfillment.

Expected coverage:

  • Caching and search indexing strategies with clear boundaries around where stale data is acceptable
  • Managing contention on scarce inventory, using durable background jobs, and applying backpressure when needed
  • Reconciliation procedures between inventory records, order state, and the payment provider to detect and resolve inconsistencies

Hints

Hint 1

Distinguish between optimistic views for browsing and authoritative checks at checkout—catalog search can be eventually consistent while inventory decisions must be transactional.

Hint 2

Use explicit order state machines (pending, authorized, confirmed, failed) and make every state transition idempotent by keying on a client-provided request ID.

Hint 3

When payment succeeds but confirmation is lost, re-querying the payment provider with the same idempotency key reveals the true outcome without creating a duplicate charge.