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.