Designing a Cart Management System (Amazon / Flipkart / JioMart)

Difficulty: Intermediate · Prerequisites: Caching and Message Queues


TL;DR

A cart management system lets users add, remove, and update items before checkout. The hard parts: persisting carts across devices, handling flash sales (1M users adding the same item), merging guest carts on login, and keeping prices/stock accurate without slowing down the add-to-cart path.

flowchart LR
    USER["User Browser"]
    GW["API Gateway"]
    CART["Cart Service"]
    REDIS[("Redis<br/>hot carts")]
    DDB[("DynamoDB<br/>durable carts")]
    INV["Inventory Service"]

    USER -->|"1. Add item to cart"| GW
    GW -->|"2. Forward request"| CART
    CART -->|"3. Write cart fast"| REDIS
    CART -.->|"4. Async persist"| DDB
    CART -->|"5. Check stock on read"| INV

In 3 sentences: User adds items to cart → stored in Redis for sub-ms reads and async-backed to DynamoDB for durability. On cart view, the service enriches items with live price and stock status from downstream services. At checkout, inventory is reserved with a distributed lock to prevent overselling.
💡 A distributed lock is a claim recorded in one shared store so that, across dozens of application servers, only one checkout at a time may act on a given item. Without it “is it in stock” and “take it” are two steps that other buyers can slip between.


1. Understanding the Problem

A shopping cart is the bridge between browsing and buying. Every e-commerce platform needs one, and despite looking simple (just a list of items), it has to handle: millions of concurrent carts during sales, real-time stock awareness, device switching (add on phone, checkout on laptop), guest-to-login transitions, flash sales where 100K users add the same item simultaneously, and graceful degradation when downstream services (inventory, pricing) are slow or down.

Think Amazon Cart, Flipkart Cart, Myntra Bag, or Zepto Cart.


2. Naive First Cut

flowchart LR
    USER["User"]
    API["Cart API"]
    DB[("One Postgres DB<br/>carts table")]

    USER --> API
    API --> DB

User adds item → write a row to Postgres → read it back on cart page. Works for 100 users.

How it breaks at scale:

  • Flash sale: 500K add-to-cart/sec → Postgres row-level locks choke at ~50K writes/sec
  • Cart page load queries Postgres every time → 10M concurrent users = 10M reads/sec on one DB
  • Guest carts with no user_id are awkward to model in a relational schema
  • No separation between hot (active session) and cold (abandoned 3 weeks ago) carts
  • Price and stock checks embedded in the write path → add-to-cart latency spikes when inventory service is slow
  • Single region DB → users across India see 50-200ms latency depending on geography

3. Prior Art We’re Drawing From

  • Amazon Cart Service — Uses a session-based in-memory store backed by a durable persistence layer. Carts are eventually consistent; inventory is checked at checkout, not at add-time.
    💡 Eventually consistent means a change may take a moment to be visible everywhere, and that is accepted deliberately. A cart that shows a five-second-old price is fine; a stock count that lies at the moment of payment is not, which is why the strictness is concentrated at checkout. Learn more → Guest carts merge on login using a deterministic merge strategy. (Amazon Builder’s Library patterns)
  • Flipkart Cart Architecture — Redis-first with MySQL backup. Cart operations are sub-10ms. During Big Billion Days, the cart service handles 1M+ ops/sec by sharding Redis across 100+ nodes. (Flipkart Engineering)
  • Shopify Cart — Stores cart as a JSON document in a KV store. Cart is schema-flexible (different stores have different item attributes). Uses optimistic concurrency with version numbers to handle race conditions.
  • Walmart Labs — Uses a write-behind cache pattern: write to cache immediately, async-persist to DB. If cache fails, fallback to DB reads. Cart merge uses “last-write-wins” per item with timestamps.

4. Functional Requirements

Core (Top 3)

  1. Add, remove, update items — user adds an item to cart, changes quantity, or removes it. Response in <100ms.
  2. Persistent cross-device cart — logged-in user’s cart survives app close, device switch, and browser restart. Guest carts persist via session cookie (7-day TTL).
    💡 TTL = time to live, the age at which the store deletes the record by itself. Guest carts getting a 7-day TTL is how abandoned anonymous carts clean themselves up without a scheduled job.
  3. Real-time cart enrichment — when user views cart, each item shows current price, stock status (in stock / only 2 left / out of stock), and delivery estimate.

Below the Line

  • Cart merge (guest → logged-in) on login
  • Apply coupon / promo codes
  • Save for later (wishlist-like behavior)
  • Cart sharing (share a link with pre-filled cart)
  • Cart-level validations (max items, max quantity per item, seller restrictions)
  • Cart expiry policies (remove items after 30 days)
  • Flash sale quantity limits (“max 1 per customer”)

5. Non-Functional Requirements

Core

NFR Target
Latency Add-to-cart: P99 < 100ms. Cart page load: P99 < 200ms
Availability 99.99% — cart must never be “down” (it’s pre-checkout, losing it = lost revenue)
Scale 50M DAU, 10M concurrent active carts, 500K add-to-cart ops/sec at peak (flash sale)
Durability Logged-in user’s cart must never be lost. Acceptable: guest carts can be lost on infra failure (they’re session-tied).
Consistency Eventually consistent for cart reads (5s stale is fine). Strongly consistent for inventory reservation at checkout.