System Design
Shopping Cart
Design the shopping cart service for a retailer like Amazon or Walmart.
Functional requirements
- Shoppers can add a product variant with a quantity, change the quantity, or remove the line; the cart shows a subtotal and every page header shows the item count.
- Guests can build a cart before signing in; on sign-in or account creation the guest cart is merged into the account cart (e.g., the same variant in both becomes one line).
- A signed-in shopper sees the same cart on every device and after closing the browser or app.
- Viewing the cart shows each line's current price and whether it is in stock; lines whose price changed or that went out of stock since being added are flagged, not silently dropped.
- The checkout service reads the cart to start an order, and the ordered lines are removed from the cart once the order is placed.
Non-functional requirements
- Cart reads and writes are on the critical shopping path: p99 under 200ms to view the cart with current prices and stock, and under 300ms to add or change a line.
- Availability over strict consistency for edits: a shopper can always add an item and a second device may see it a moment late, but a login merge must never lose a line or double a quantity.
- A cart is durable for its whole life: a line added on the phone survives app-server restarts, deploys, and a switch to the laptop; sticky sessions and in-memory carts are not acceptable.
- Price and stock shown in the cart may be up to 60 seconds stale (checkout re-verifies both), but the cart must never show a price the catalog never published.
- A cart is readable and writable only by its owner: guest cart identifiers are unguessable, and a merge must never expose one shopper's cart to another.
Scaling & constraints
- 50M live carts at any time (about 60% guest, 40% signed in); 3M new carts per day; 20M cart edits per day on average, peaking at 2,000 edits per second during sales.
- Read:write ratio is about 10:1 (cart views, header item counts, and checkout reads vs. add, update, and remove).
- A cart holds 4 lines on average and up to 200; a line is about 150 bytes, so a typical cart is under 1KB and the largest about 30KB.
- Guest carts expire after 30 days of inactivity and member carts after 90; about 40% of guest carts are abandoned within a day and never touched again.
- The catalog's batch price and stock lookup (up to 200 variants per call) answers at p50 20ms and p99 150ms and is unavailable about 0.1% of the time.
- About 5% of logins arrive with a non-empty guest cart to merge; during a flash sale the same 100 variants appear in 30% of all carts viewed.
Out of scope
- Checkout, payment, and order placement.
- Inventory reservation (adding to cart never holds stock).
- Promotions, coupons, tax, and shipping calculation.
- User accounts and authentication (assume a user id or guest session token is available).
Sign in to save your progress
AI design evaluation
Get a score, what your board does well, what to fix first, and what an interviewer would ask next.
Sign in to get AI feedback on your design.