Shopping Cart: Low-Level Design

A shopping cart stores a user’s intended purchases before checkout. The design must handle: anonymous users (no account), authenticated users with persistent carts, cart merging when an anonymous user logs in, and inventory validation at checkout. The cart is a high-read, moderate-write data structure with strict consistency requirements at checkout.

Storage Options

Cookie/Client-side: cart data stored in the browser (localStorage, cookie). No server storage required. Problem: lost when cookies are cleared, not shared across devices, no server-side validation of prices or inventory. Appropriate only for simple stores with infrequent inventory changes. Redis (session-based): cart stored in Redis keyed by session_id or user_id. Fast reads/writes, TTL for automatic expiration of abandoned carts. Problem: lost if Redis fails without persistence configured. Database: cart persisted to PostgreSQL, associated with user_id (authenticated) or device_id/session_id (anonymous). Survives system restarts, supports cross-device sync. Used by Amazon, Shopify. Hybrid: Redis for active carts (fast access), database for persisted carts (durability, anonymous + authenticated).

Data Model

Cart table: (cart_id UUID, user_id nullable, session_id, created_at, updated_at, expires_at). Cart items: (cart_item_id UUID, cart_id, product_id, variant_id, quantity INT, price_snapshot DECIMAL, added_at). Price snapshot: store the price at the time the item was added — this is the price shown to the user. Do not store price in the cart if you want to always reflect current pricing; store it if you want to honor the price seen at add-to-cart time (common for user trust). Validate that the snapshot price matches the current price at checkout — if it changed, notify the user.

Anonymous to Authenticated Cart Merge

When an anonymous user logs in, they have two carts: the anonymous cart (from browsing) and possibly a previously saved authenticated cart. Merge strategy: (1) For items in the anonymous cart not in the authenticated cart: move them to the authenticated cart. (2) For items in both carts with the same product/variant: add the quantities (cap at the inventory limit). (3) Discard the anonymous cart after merging. Present the merged cart to the user — they see all their items regardless of when/where they were added. Edge case: the user added item A from their phone (anonymous) and item A from their laptop (authenticated). Merge: sum the quantities (2 of item A), inform the user if the total exceeds inventory.

Inventory Validation at Checkout

The cart is optimistic — items are added without checking inventory. At checkout, validate: for each cart item, check that quantity_requested <= available_inventory. If any item is out of stock: present the checkout page with unavailable items highlighted, allow the user to remove or adjust quantities before proceeding. Do not process payment until all items are validated. Do not reserve inventory during cart browsing — only reserve at checkout initiation (hold for 15 minutes during payment, as in a ticketing system). Inventory validation must be transactional: read and hold inventory atomically to prevent oversell when multiple users check out simultaneously.

Abandoned Cart Recovery

Users who add items to cart but do not checkout represent lost revenue. Abandoned cart emails (sent after 1-24 hours of cart inactivity) recover 5-15% of abandoned carts. Implementation: when a cart is created or updated, record the timestamp. A background job runs hourly, finds carts with no activity in the last 1 hour and not yet emailed, sends an abandoned cart email with cart contents, and marks the cart as “email_sent”. Rate limit: send at most one abandoned cart email per cart per day. Personalize: include the specific items in the cart and their images. Do not send if the user has since completed a purchase. Privacy: comply with email opt-in requirements — only send if the user consented to marketing emails.

See also: Meta Interview Guide 2026: Facebook, Instagram, WhatsApp Engineering

See also: Uber Interview Guide 2026: Dispatch Systems, Geospatial Algorithms, and Marketplace Engineering

See also: Netflix Interview Guide 2026: Streaming Architecture, Recommendation Systems, and Engineering Excellence

See also: Scale AI Interview Guide 2026: Data Infrastructure, RLHF Pipelines, and ML Engineering

See also: LinkedIn Interview Guide 2026: Social Graph Engineering, Feed Ranking, and Professional Network Scale

See also: Airbnb Interview Guide 2026: Search Systems, Trust and Safety, and Full-Stack Engineering

See also: Databricks Interview Guide 2026: Spark Internals, Delta Lake, and Lakehouse Architecture

See also: Anthropic Interview Guide 2026: Process, Questions, and AI Safety

See also: Atlassian Interview Guide

See also: Coinbase Interview Guide

See also: Shopify Interview Guide

See also: Snap Interview Guide

See also: Lyft Interview Guide 2026: Rideshare Engineering, Real-Time Dispatch, and Safety Systems

See also: Stripe Interview Guide 2026: Process, Bug Bash Round, and Payment Systems

Scroll to Top