Designing a Copy-Match Service for a Billion-Video Catalog
System DesignModel answerLast updated
Design a pipeline that flags re-uploaded or lightly altered videos against a billion-item catalog within 500 ms at 10k uploads per second, balancing precision and recall.
Scenario
You run the content-integrity team for a large video-sharing platform. When someone uploads a clip, you must decide whether it is essentially a copy of something already in the catalog. A copy here means a video that was only lightly altered: re-compressed, color-graded, watermarked, cropped, or trimmed from a longer source.
Requirements
Functional
- Check on upload: return a verdict within 500 ms of the upload finishing.
- Reference index: keep a global searchable index covering 1 billion existing videos.
- Freshness: a video judged to be unique must become searchable within 5 minutes.
Non-functional
- Sustain 10,000 uploads per second.
- Keep precision very high, since wrongly flagging an original creator is costly, while keeping recall high enough to meaningfully reduce infringement.
What to cover
- Representation: how do you turn a video into compact vectors that stay stable under minor edits yet separate different videos?
- Search architecture: how do you build approximate nearest-neighbor retrieval that works at this size, including sharding, replication, and freshness?
- Verification: how do you confirm borderline candidates before any automated action is taken?
- Trade-offs: discuss how vector dimensionality, storage cost, and query latency interact.
Include rough capacity numbers, a latency budget, and the failure modes you would watch for.
Hints
Hint 1
Start by clarifying what the 500 ms covers and whether partial copies (a short clip taken from a long video) count. This decides whether you embed whole videos or time segments.
Hint 2
Do the memory math: a billion float vectors at a few hundred dimensions is terabytes. Think about product quantization with a coarse inverted index, then an exact re-rank on a small candidate set stored on SSD.
Hint 3
A single similar frame is weak evidence. Think about checking that matching frames line up consistently in time, down-weighting generic content like black frames and common intros, and routing mid-confidence scores to humans.
Hint 4
For the 5-minute freshness target, consider a small separate index for recent inserts that is queried alongside the large static index, plus handling for two identical uploads arriving seconds apart.
Model answer
Hard Try it yourself first — then compare your approach, complexity and edge cases.
1. Clarify and size the problem
Clarifications to settle first
- The 500 ms clock should start at upload completion. Frame sampling and early embedding can overlap with the upload stream, since fully decoding a long video inside 500 ms is unrealistic.
- Partial copies must count (a 30 s clip from a 2 h film), so I index at segment level, not only whole-video level.
- The action after a match (block, claim revenue, send to review) determines the precision target. For automated action I would aim for roughly 99.5% precision or better.
Back-of-envelope
- Average video about 5 min; one vector per ~10 s segment, capped at about 16 sampled segments per video, gives roughly 10-16B vectors.
- 10k uploads/s with about 60 sampled frames each is about 600k frames/s to embed. At around 8k frames/s per GPU with a small quantized model, that is on the order of 80-100 GPUs plus headroom.
- Each upload issues about 8 query vectors, so about 80k vector queries/s against the index.
2. Architecture
- Upload service streams chunks to a frame sampler (about 1 fps plus scene-change frames, resized to ~224 px).
- Embedding service (batched GPU inference) produces per-frame vectors, pools them into segment vectors and one global vector.
- Retrieval service fans out to sharded main ANN index and a fresh index, then merges top-K.
- Verifier re-ranks, aligns in time, and cross-checks audio.
- Policy engine produces
UNIQUE,DUPLICATE, orREVIEW. - Unique videos are published to a log (e.g. Kafka); an indexer adds them to the fresh index and later to the main index.
Latency budget (500 ms): final decode/sampling ~100 ms (mostly overlapped with upload), embedding ~100 ms, ANN fan-out ~50 ms, re-rank and verification ~150 ms, ~100 ms slack for p99 tail.
Stores: an in-RAM ANN index (PQ codes plus id-to-(video, segment) map); an SSD key-value store with full-precision vectors for re-ranking; a metadata store (owners, rights policies, allowlists); and a separate audio fingerprint index.
3. Feature extraction
- Train a copy-detection embedding with contrastive self-supervision. Positives are heavy augmentations of the same frame: crop/pad, color jitter, re-encoding, overlays and watermarks, flips, picture-in-picture.
- The objective is instance-level similarity, not semantic. Two different cat videos must stay far apart.
- Output 256-d, L2-normalized, optionally PCA-whitened.
- Segment vector: mean-pool frame vectors in a ~10 s window and re-normalize. This supports clipped copies.
- Global vector: pooled over the whole video for cheap whole-copy detection.
- Audio fingerprint as an independent signal; it rescues visually mangled copies and adds precision evidence.
4. Indexing at a billion scale
Memory per billion vectors
| Format | Size |
|---|---|
| float32, 256-d | ~1 TB |
| fp16 | ~512 GB |
| OPQ + PQ (64 bytes) + 8 B id | ~72 GB |
For ~10B segment vectors the PQ index is about 720 GB, which fits in cluster RAM.
Structure: IVF with a coarse quantizer (about 2^18 centroids), nprobe of 32-64, asymmetric distance computation with lookup tables (FAISS/ScaNN style).
Sharding and replication: about 64 shards of ~12 GB each. At 3-5k QPS per replica, serving 80k QPS needs roughly 20 replicas per shard. Use hedged requests to trim tail latency.
Freshness: each shard also has a small flat or HNSW fresh index covering recent inserts, queried in parallel. A background job merges them into the IVF lists (cheap; only codebook retraining is expensive, done occasionally).
Race condition: two identical uploads seconds apart. Write each checked video's embeddings to a pending buffer immediately, and remove them if the video is rejected, so the second upload can match the first.
5. Verification and decisions
- Re-rank: fetch full-precision vectors for the top ~200 candidates from SSD, compute exact cosine, keep ~20 videos.
- Temporal alignment: build a frame-similarity matrix between query and candidate, then find a consistent diagonal (offset voting, DTW, or a learned temporal model). This tolerates speed changes and yields aligned ranges and fraction covered. One similar frame is weak evidence; twenty aligned seconds is strong.
- Geometric check (optional): local keypoints plus RANSAC on a few frames to confirm crops and picture-in-picture.
- Discount generic content: black frames, static slides, shared intros and logos match millions of items. Keep an IDF-like match-frequency table and down-weight such segments.
- Tiered policy, thresholds calibrated on a labeled set: high score with long coverage leads to automated action per rights policy; medium goes to human review; low passes. Allowlist same-owner and licensed content, and always provide an appeal path.
6. Trade-offs
- Dimensionality: 128-d is cheaper but loses discrimination as distractors grow; 512-d adds little after quantization. 256-d with OPQ is a good default.
- PQ code size: 32 B halves RAM but lowers recall. Exact re-ranking restores precision, not recall lost in stage one, so spend bytes where recall drops.
- nprobe and K: scan cost grows about linearly with codes scanned. Tune against recall@K on a held-out benchmark of edited copies. Larger K helps recall but adds verification load.
- Sampling rate: more frames help with short clips but increase GPU cost.
7. Common mistakes and monitoring
- Using semantic embeddings (CLIP-style) and flagging merely similar content.
- Matching on single frames without temporal consistency.
- Ignoring the freshness race between near-simultaneous uploads.
- Forgetting that codebook or model changes require re-embedding or dual-index migration; version your embeddings.
- Not handling hot or generic content that floods candidate lists.
Monitor precision via sampled human audits, recall via injected synthetic edits, p99 latency per stage, index lag, and the review-queue rate.