Design an e-commerce site: product page, cart and checkout
Design the frontend of an e-commerce site: product listing and search, the product page, the cart, and checkout.
A typical framing: "Design the web storefront of an online retailer with 200,000 products. Most traffic comes from Google and from mobile. The business cares about conversion, so pages must load fast and checkout must never lose an order."
TL;DR
Different pages have different needs, so pick a rendering strategy per page. Product and category pages are public, SEO-critical and change a few times a day: render them on the server and cache them (SSG/ISR or SSR behind a CDN), with structured data for rich Google results. Price and stock are fresher data that can be fetched or streamed separately. The cart is per-user state that must survive refreshes and devices: keep it on the server, keyed by a cart id in a cookie, with optimistic UI on the client. Checkout is a multi-step form where correctness beats speed: validate on both sides, use idempotency keys so a double click never charges twice, and use the payment provider's hosted fields for PCI compliance. Performance (Core Web Vitals) directly affects revenue, so budget JS and optimize images aggressively.
1. Requirements
Questions worth asking:
- How many products, and how often do prices and stock change?
- Where does traffic come from? (SEO and mobile shape almost everything.)
- Guest checkout, or accounts only? Which payment methods?
- Multiple currencies or languages?
Functional
- Browse categories, search and filter, view a product page with images, variants (size, color), price and stock.
- Add to cart, edit quantities, see totals.
- Checkout: address, shipping, payment, review, confirmation.
Non-functional
- SEO: product and category pages indexable, with good titles and structured data.
- Performance: fast LCP and low INP on mid-range phones. Every 100 ms matters for conversion.
- Correctness: no lost carts, no double charges, prices shown are prices charged.
- Security: payment data never touches our servers. Protect against XSS and CSRF.
- Accessibility: an inaccessible checkout loses customers and can be a legal risk.
2. Architecture
Rendering per page
| Page | Strategy | Why |
|---|---|---|
| Home, category | SSG/ISR or SSR + CDN cache | Public, SEO, the same for everyone |
| Product page | ISR/cached SSR, price and stock fetched or streamed | SEO, but stock changes often |
| Search results | SSR (for SEO-worthy queries) + client-side filtering | Many combinations, URL is the state |
| Cart, checkout | Client rendered or SSR without caching | Personal, never cached at the CDN |
The trick on product pages: cache the stable parts (title, description, images) for a long time, and load the volatile parts (price, stock, delivery estimate) separately, so a stock change doesn't require rebuilding 200,000 pages.
3. Data model
type Product = {
id: string
slug: string
title: string
description: string
images: { url: string; width: number; height: number; alt: string }[]
variants: Variant[]
}
type Variant = { sku: string; options: Record<string, string>; price: Money; inStock: boolean }
/** Money as integer minor units, never floats: 0.1 + 0.2 !== 0.3. */
type Money = { amount: number; currency: 'ILS' | 'USD' }
type Cart = {
id: string
items: { sku: string; quantity: number; unitPrice: Money }[]
subtotal: Money
/** Bumped on every change, used to detect stale updates. */
version: number
}URL as state for listings: /c/shoes?size=42&color=black&sort=price_asc&page=2. Filters in the URL are shareable, work with the back button, and can be crawled.
4. Interface (API)
GET /api/products/:slug
GET /api/products/:id/availability -> price, stock, delivery estimate
GET /api/search?q=&filters=&cursor=
POST /api/cart/items { sku, quantity }
PATCH /api/cart/items/:sku { quantity }
POST /api/checkout Idempotency-Key: 7f3c... { cartId, cartVersion, address, shippingMethod }The server recalculates every price and total on checkout. Never trust prices sent by the client.
5. Optimizations and deep dives
Core Web Vitals and conversion
- LCP is usually the main product image: serve responsive images (
srcset, AVIF/WebP) from an image CDN, preload it withfetchpriority="high", and never lazy-load it. - CLS: reserve space for images, prices and banners. Late-loading price or promo blocks that push the "Add to cart" button down cause misclicks.
- INP: keep third-party scripts (analytics, chat widgets, A/B testing) off the critical path. Load them after interaction or with
async/defer, or in a worker (Partytown). They are often the biggest source of main-thread work. - JS budget: ship only what each page needs (route-based code splitting). Islands or Server Components help keep mostly-static product pages light.
SEO
- Server-rendered HTML with unique titles, descriptions and a canonical URL. Faceted filter URLs can explode into millions of near-duplicates: canonicalize them, or mark low-value combinations
noindex. - Structured data (
schema.org/Productwith price, availability and reviews) for rich results in Google. - A sitemap for all product and category pages, and
hreflangfor multiple languages.
Cart
- Server-side cart keyed by a
cartIdcookie, so it survives refreshes, tabs and devices (merged into the account cart when a guest logs in). - Optimistic updates: the badge count updates instantly. Reconcile with the server's response, which is the truth for prices and stock.
- Multiple tabs: use the
versionfield to detect conflicts, andBroadcastChannelor thestorageevent to sync open tabs.
Checkout
- Multi-step form with a progress indicator, each step validated on the client (fast feedback) and the server (security). Save progress so a refresh doesn't lose it.
- Idempotency: generate an idempotency key when checkout starts. If the user double-clicks or the network retries, the server returns the same order instead of charging twice. Also disable the button while submitting.
- Payments: use the provider's hosted fields or redirect (Stripe Elements, Adyen) so card numbers go straight to the provider. This keeps you out of most PCI DSS scope. Handle 3-D Secure challenges, and confirm the final order from the provider's webhook, not only from the redirect back.
- Price changes during checkout: the server rejects a stale
cartVersion, and the client shows "Prices changed, please review". - Accessibility: proper labels,
autocompleteattributes for addresses and cards, clear inline errors linked witharia-describedby, and focus moved to the first error on submit.
Security
- Escape user content (reviews, Q&A) to prevent XSS. Add a strict Content Security Policy, which is also required to keep a payment page safe from injected scripts (web skimming attacks).
SameSitecookies and CSRF tokens on state-changing requests.
Common follow-ups
- "Black Friday: 20x traffic. What breaks?" Uncached pages and APIs. Serve as much as possible from the CDN, use
stale-while-revalidate, protect checkout with a queue or rate limits, and test with load tests in advance. - "How do you run A/B tests without hurting performance?" Decide the variant at the edge or on the server and render it directly. Client-side A/B scripts that hide the page until they run hurt LCP and cause flicker.
- "How would you add a recommendations carousel?" Load it below the fold, lazily, with a fixed height so it doesn't shift the layout. It must never block the main content.
What interviewers look for
- A rendering strategy per page, and splitting stable from volatile data.
- SEO awareness: server HTML, canonical URLs for filters, structured data.
- Correctness in checkout: server-side prices, idempotency keys, webhook confirmation, stale-cart detection.
- Security thinking about payments (hosted fields, PCI scope, CSP).
- Connecting performance to business metrics (Core Web Vitals and conversion).