System Design Interview: Designing a Shopping Cart Service
3 views
"Design the shopping cart for a large e-commerce site" is a common system design interview question. It looks simple, a list of items per user, but good answers cover guest carts, merging carts after login, price and stock changes, and keeping the cart fast and available during peak shopping days.
1. Requirements
Functional: add, update and remove items; view the cart with current prices, discounts and estimated totals; carts persist across sessions and devices; guest users can shop before logging in, and their cart merges when they sign in; move items to "save for later"; start checkout. Non-functional: very low latency for add-to-cart, high availability (a failed add-to-cart is lost revenue), and correct prices at checkout.
2. Data model
Cart: cart_id, owner (user_id or guest token), status, updated_at.
CartItem: cart_id, product_id, variant_id (size, color), quantity, price snapshot at add time, added_at.
Keep the cart as a single document per user in a key-value or document store, such as DynamoDB or Cassandra, keyed by cart owner. Reads and writes always target one cart, so this partitions naturally.
3. Guest carts and merging
Guests get a cart keyed by a random token stored in a cookie. When they log in, merge the guest cart into the user's cart: combine quantities for identical items (capped by stock or per-order limits), keep the most recent changes, and delete the guest cart. Make the merge idempotent so a retry does not double quantities.
4. Prices and promotions
Never trust the price stored in the cart. Store a snapshot for display ("price dropped since you added this"), but recompute prices, discounts and taxes from the pricing and promotion services when the cart is viewed and again at checkout. Cache product prices aggressively, with short TTLs.
5. Inventory
Adding to the cart usually does not reserve stock; it only checks availability. Reservation happens at checkout, often with a time-limited hold. Show warnings like "only 2 left" or "out of stock" when the cart is loaded.
6. Availability and consistency
Shopping carts are the classic example of favoring availability: the original Dynamo paper used carts to explain why it is better to accept a write and reconcile conflicts later than reject an add-to-cart. Mention conflict resolution: with concurrent updates from two devices, merge items rather than keeping only one version, and let the user fix quantities.
7. Scale and performance
Add a cache in front of the cart store for hot carts, use asynchronous events (cart_updated) for analytics, recommendations and abandoned-cart emails, and expire inactive guest carts after a set period.
8. Checkout handoff
At checkout, freeze a copy of the cart into an order draft, validate prices and stock, reserve inventory and hand off to payment. If anything changed, show the user before charging.
9. Trade-offs to discuss
Document per cart versus row per item, reserving stock early versus at checkout, and how long to keep carts.
Cover requirements, data model, merging, pricing, inventory and availability. If you want help while the interviewer drills in, Linkjob AI (https://linkjob.ai) offers real-time help during job interviews and technical interviews, including system design rounds.