Lesson 7 of 16
42. Design E-commerce Cart and Checkout (Amazon / Flipkart)

42.6 How is the cart stored?

3 min read+10 XP

Step 5: Deep dives (10 minutes)

  • Key-value store keyed by user ID (or a guest ID in a cookie for signed-out shoppers), sharded and replicated. A cart write should never fail: if one replica is down, accept the write elsewhere (Chapter 16's availability-first choice).
  • Merging at login: add the guest cart's items into the user's cart, with sensible caps. In the chapter's code, {dune: 1} plus a guest cart {dune: 2, atomic-habits: 1} becomes {dune: 3, atomic-habits: 1}, and quantities never exceed the per-item limit.
  • Prices in the cart are hints. Store the price when added, but re-price at checkout and show the shopper what changed. In the chapter's code, a book that dropped from ₹499 to ₹449 is flagged.
  • Stock in the cart is a hint too: "only 3 left" is shown, but nothing is reserved until checkout.
  • Abandoned carts feed reminder emails (Chapter 33) and expire after weeks.

Memory code: "Never refuse an item; re-price at the till."