KatInterview

Cart Hold System for High-Traffic E-Commerce

System DesignModel answer
MediumModel answerSystem Designconcurrencydistributed-systemscaching

Last updated

Build a scalable service that temporarily locks inventory when users add items to their cart, prevents duplicate allocation, and releases stock automatically.

Context

You're architecting the backend for an online grocery marketplace where physical warehouses stock thousands of products with finite quantities. When a shopper adds a limited-availability item (think flash-sale electronics or holiday gift baskets) to their cart, your platform needs to guarantee that those units remain theirs until they either complete payment or their session times out. The challenge: if fifty people simultaneously try to claim the last three units of a product, your system must allocate correctly without allowing more reservations than stock available, all while maintaining fast response times and handling sudden traffic bursts.

What You Need to Design

Core Capabilities

  • Temporary Allocation: Lock a requested quantity of a product for a specific user session with a time limit (assume 15 minutes default).
  • Stock Return: Automatically free locked inventory when time expires or when a user manually removes items before checkout.
  • Finalization: Permanently deduct inventory once payment processing confirms success.
  • Real-Time Visibility: Show shoppers accurate or near-accurate stock levels while browsing.

Performance & Reliability Targets

  • Accuracy: Zero cases where total allocations exceed physical stock, regardless of concurrent access patterns.
  • Speed: Reserve and query operations should complete in under 50ms at the 99th percentile.
  • Elasticity: Handle normal load and accommodate a 10x spike during promotional events without degradation.
  • Recovery: System failures (crashed workers, network partitions) must not permanently trap inventory in a locked state.

Operating Environment

  • Product Catalog: 50 million SKUs distributed across 20,000 warehouse locations.
  • User Base: 10 million daily active shoppers.
  • Baseline Traffic:
    • Allocation requests: 20,000 per second
    • Stock queries: 100,000 per second
  • Peak Traffic (during sales events):
    • Allocation requests: 200,000 per second
    • Stock queries: 1,000,000 per second

Areas to Address

  • Storage Architecture: Evaluate transactional databases versus in-memory data stores for managing the allocation ledger.
  • Race Condition Prevention: Explain how you'll eliminate conflicts when many sessions compete for the same scarce product.
  • Timeout Management: Design a mechanism to reclaim expired locks at scale without overwhelming your primary data layer.
  • Error Handling: Describe recovery strategies for payment gateway delays, service crashes, and inconsistent state scenarios.

Hints

Hint 1

Consider separating the fast decision path from the durable record-keeping path

Hint 2

Look for storage technologies that can perform atomic read-modify-write operations in microseconds

Hint 3

Think about how to clean up expired locks without scanning every record in your database