Design Visual Search
Understanding the Problem
🔍 What is visual search? Visual search lets you search with an image instead of words. Point your camera at a lamp, or tap a lamp in a photo, and get visually similar lamps, ideas for rooms that use it, and the exact product with a link to buy it. Pinterest Lens, Google Lens, Amazon's camera search and the "shop similar" buttons in fashion apps are examples. Underneath, each image (or each object in it) becomes an embedding vector, and search is a nearest-neighbor lookup among billions of vectors.
You'll see this as "design Pinterest Lens," "design Google Lens," "design visual product search," or "find similar images at scale." The parts every answer needs are the same: find the object the user cares about (crop it), turn it into an embedding that captures what matters visually, retrieve candidates from a huge index in milliseconds with approximate nearest neighbor (ANN) search, and re-rank. The parts that separate strong answers are how the embedding is trained (what "similar" means is a product decision), and the difference between similar items (other mid-century floor lamps) and the same item (this exact lamp, model number and store), which needs a second, stricter stage.
Functional Requirements
We're building camera and image search for a large visual discovery and shopping platform.
Core Requirements
- Users should be able to search with a camera photo or an image already on the platform, and get visually similar results.
- The system should detect the objects in the image and let users pick one (or adjust a crop) to search for just that object.
- For shoppable objects, the system should return matching products: the exact item when it's in the catalog, and close alternatives otherwise, with price and availability.
- Users should be able to refine results with text ("in blue," "under $100").
Below the line (out of scope)
- Text-only search (though the same index serves "more like this").
- Landmark, plant and text recognition modes of general-purpose lenses (they reuse the design with different indexes).
- Ads in visual search results.
Non-Functional Requirements
Ask about index size, query volume and latency. Our numbers: an index of 5 billion images (user-saved images) plus 500 million product images from merchant catalogs. 50 million visual queries a day, about 580 per second on average and 2,000 per second at peak.
Core Requirements
- Latency: results within 500 ms at p95 after the image reaches the server (upload time is separate and depends on the network).
- Relevance: the top results should look like the selected object, by style, shape, color and category, and the exact product should rank first when it exists.
- Freshness: new products and images should be searchable within minutes to an hour; out-of-stock products should drop out quickly.
- Scale: support index growth of hundreds of millions of images a month without rebuilding everything.
- Safety: results must not include unsafe content just because it looks similar.
Below the line (out of scope)
- Exactly identifying people. Visual search doesn't do face recognition.
- On-device search over a user's own camera roll.
Now the arithmetic that shapes the index. Take 256-dimensional embeddings for 5.5 billion images:
- In float32, 5.5B × 256 × 4 bytes ≈ 5.6 TB. In int8, 1.4 TB. With product quantization to 64 bytes per vector, about 350 GB.
- An HNSW graph with ~32 neighbors per node adds roughly 128-256 bytes per vector, another 0.7-1.4 TB.
- So the index doesn't fit on one machine in any form that keeps HNSW speed. We'll shard it across machines, keep compressed vectors and the graph in RAM, and re-rank a short list with full-precision vectors.
- At 2,000 QPS, if each query fans out to all 32 shards, the shards see 64,000 shard-queries per second, about 2,000 each, which is comfortable for an in-memory ANN index with a few replicas per shard.
State early that there are two problems: "similar" (style and category, served by embedding retrieval) and "same product" (instance matching, served by a stricter verification stage). Mixing them up is the most common gap in answers.