How would you design a visual search system that finds products looking like an uploaded photo? Assuming 500 images/s per GPU (graphics processing unit) and USD 2 per GPU-hour, how much GPU time and cost does it take to re-embed a 500-million-image catalogue when upgrading the embedding model?

ML system design, Case studies
Level L4 Advanced. Length Long. Nature Th+Mat Both.

What's being asked

Whether you can design a low-latency multimodal retrieval pipeline from messy user photos to clean catalogue items, including object cropping, metric-learning embeddings, ANN (approximate nearest neighbour) indexing, deduplication, and full-catalogue re-embedding economics.

Key idea

Real user photos contain clutter and multiple items, so an object detector must first localise and crop the target product before a metric-learned encoder maps the crop into a unit-norm (ℓ2-normalised) embedding space for ANN search, deduplication and business re-ranking. Re-embedding 500 million images at 500 images/s per GPU takes 500×106/500=1,000,000 GPU-seconds (≈277.8 GPU-hours, or ≈8.7 hours on 32 GPUs), costing roughly USD 556.

Steps

  1. Clarify requirements and scale: define the catalogue size (500 million product images across apparel, home goods and accessories), query traffic in QPS (queries per second), interactive latency budget (such as p95, or 95th-percentile, end-to-end latency under 200 ms), support for user-adjustable bounding boxes when multiple items appear in a scene, and freshness for newly listed or out-of-stock products.
  2. Define ML objective and metrics: offline, evaluate retrieval with recall@k (fraction of test queries whose ground-truth matching product appears in the top k), MRR (mean reciprocal rank) and NDCG@k (normalised discounted cumulative gain at k) on curated exact-match and visually-similar pairs. Online, track CTR (click-through rate), save or add-to-cart rate, zero-result rate, and user crop adjustments.
  3. Build data, labels and deduplication signals: assemble positive pairs from clean studio photos paired with in-the-wild user photos, multiple angles of the same SKU (stock-keeping unit), and high-confidence co-engagement logs (with position-bias filtering). Sample in-batch and semi-hard negatives from the same fine-grained category (for example, different black sneakers). Flag near-duplicate product photos offline using pHash (perceptual hashing) combined with high cosine similarity and merchant IDs so identical listings do not crowd the top results.
  4. Design feature extraction (detection and embeddings): run a fast single-stage object detector to detect candidate products and crop the primary region of interest, stripping distracting backgrounds; optionally extract predicted category and colour tags for filtering. Feed the crop into a CNN (convolutional neural network) or ViT (Vision Transformer) backbone producing a compact unit-norm embedding (for instance, 256-D or 512-D).
  5. Iterate on models from simple to strong: start with a frozen pretrained ViT or contrastive image–text encoder as a zero-shot baseline to validate the pipeline. Improve by fine-tuning the backbone with contrastive or multi-similarity loss (such as InfoNCE, noise-contrastive estimation, with hard negatives) on domain pairs so embeddings ignore lighting, viewpoint and background differences while preserving product style. Add a lightweight second-stage re-ranker over the top 100 ANN candidates combining visual cosine similarity, category match, price/popularity priors, and diversity constraints.
  6. Design serving architecture, latency budget and re-embedding cost: online, keep total p95 latency within 200 ms: image decode and resize (~10 ms), object detection (~25 ms), GPU embedding inference (~20 ms), distributed ANN search with HNSW (hierarchical navigable small world) or IVF-PQ (inverted file with product quantisation) (~30 ms; index memory sizing is covered in a separate bank question), and deduplication, stock-status filtering and re-ranking (~20 ms), leaving headroom for network hops. For incremental catalogue updates, a streaming worker embeds new images within minutes. When upgrading the embedding model, old and new vectors are incompatible, so run a blue-green full backfill: re-embedding 500 million images at 500 images/s per GPU requires 500×106/500=1,000,000 GPU-seconds≈277.8 GPU-hours (≈8.7 hours on 32 GPUs), costing 277.8×USD 2≈USD 556 before building the new ANN index and swapping traffic atomically.
  7. Evaluate and launch: test offline recall@k segmented by category, lighting and occlusion, and run human side-by-side relevance ratings to catch visual mismatches that click logs miss. Shadow-test the new pipeline and index on live image traffic to verify latency and throughput, then run a controlled A/B test on CTR and conversion.
  8. Monitor, iterate and address interviewer trade-offs: monitor index freshness lag, out-of-stock result rate, detector miss rate, p95 latency per stage, and embedding drift. Interviewers will probe: why an object crop is critical for cluttered user photos; embedding dimensionality and product quantisation compression vs recall@k; single dominant crop vs multi-box selection user experience; and how often to absorb a full catalogue re-embedding backfill.

Common mistakes

How long your answer should be

About 6–8 minutes: walk through detection/cropping, metric-learning embeddings, ANN retrieval and deduplication, a 200 ms latency breakdown, and the 277.8 GPU-hour / USD 556 backfill calculation.

Read this

Previous in ML system design: How would you design a retrieval-augmented question answering system over a company's internal documents? As part of the prompt budget, if the model has an 8,000-token context window, 1,000 tokens are reserved for system instructions and the user question, and 500 tokens are reserved for the answer, how many 400-token chunks fit?
Next in ML system design: How would you design a system to serve a 7B-parameter large language model (LLM) chat service at scale? For a 32-layer model with hidden size 4,096 in 16-bit floating point (fp16, 14 GB of weights), what are the key–value (KV) cache memory per token, the memory for a 4,096-token sequence, roughly how many full-length sequences fit on an 80 GB GPU (graphics processing unit), and what is the serving cost per million output tokens if a GPU costs USD 2 per hour and sustains 2,500 output tokens/s?