← Lessons

quiz vs the machine

Silver1110

System Design

Shopping Cart Design

Storing a shopper's intended purchases reliably across devices and sessions.

4 min read · intro · beat Silver to climb

What the cart must do

A shopping cart holds the items a shopper intends to buy before checkout. It must survive page reloads, work for both guests and signed in users, and ideally follow the user across devices.

Where to store it

  • Client side only: keep the cart in browser storage. Simple, but it is lost on a new device and cannot be analyzed.
  • Server side: store the cart in a fast key value store keyed by user or session id. This enables cross device carts and recovery emails.
  • Hybrid: a guest cart lives client side, then merges into the server cart when the user signs in.

Important details

  • Carts are mutable and frequent, so a low latency store such as Redis fits well, often with a time to live.
  • The cart should store product ids and quantities, not frozen prices. Prices are re fetched at checkout so the shopper pays the current price.

Key idea

Store carts server side in a fast key value store, merge guest and user carts on sign in, and recompute prices at checkout.

Check yourself

Answer to earn rating on the learn ladder.

1. Why store product ids and quantities rather than frozen prices in the cart?

2. What happens to a guest cart when the user signs in?

3. Why is a store like Redis a good fit for carts?