How it works
An embedding model converts text, images, or other inputs into fixed-length vectors. Store each vector with an item ID, an embedding-model version, and filterable metadata such as tenant or category; keep the original content in its source store. Queries and indexed items must use compatible embedding models.
Choose a similarity measure that matches the model: cosine similarity, dot product, or Euclidean distance. An exact search scores every eligible vector and returns the nearest k items. This is a useful baseline and can be sufficient for small collections or highly selective filters.
ANN indexes avoid scoring every vector. HNSW uses a layered graph to navigate toward nearby vectors; IVF groups vectors into clusters and searches selected clusters. Product quantization compresses vectors to reduce memory and distance-computation costs, usually at the cost of accuracy.
At query time, generate a query vector and search the index for candidates. Increasing HNSW's search breadth or the number of IVF clusters visited generally improves recall but adds work. For example, retrieve 200 candidate videos, then use a separate ranking model to select 20 based on relevance, freshness, and user preferences.
Combine similarity retrieval with eligibility filters. A tenant or category filter can be applied during retrieval when supported; filtering only afterward may leave too few results. Permission checks must still prevent unauthorized items from reaching the user, even when index metadata is stale.
At larger scale, partition the collection, search the relevant shards, and merge their candidates using comparable scores. Updates arrive through an indexing pipeline, so source writes and search visibility may differ in time. Track indexing lag, process deletions, and rebuild or replace indexes when embedding models change.
A worked example
Retrieving candidates for a short-video feed
Suppose a catalog has 10 million videos, each with a 768-dimensional float32 embedding. Raw vectors alone require 30.72 GB in decimal units, before IDs, metadata, replicas, or ANN index overhead. For each feed request, a user embedding retrieves 500 candidates; eligibility checks and a ranking model reduce those to 20 videos. Treat recall@100 of at least 95% as a proposed acceptance target, not a promised ANN property. Compare candidate settings against exact search on sampled, representative queries, and measure filtered-query latency. If exclusions leave too few candidates, expand retrieval within a bounded budget or fall back to another candidate source. Recheck current visibility before returning videos.
Trade-offs to say out loud
- Recall versus latency
- A wider ANN search usually finds more true nearest neighbors but consumes more CPU and increases query latency.
- Memory versus accuracy
- Graph indexes add memory overhead, while vector compression reduces storage and memory needs but can change which neighbors are returned.
- Filter selectivity versus retrieval efficiency
- Post-filtering can exhaust candidates, while selective prefilters can make exact search cheaper or require a filter-aware ANN strategy.
- Freshness versus indexing cost
- Frequent inserts, updates, and deletions require ongoing index maintenance, and some index designs also need periodic rebuilding.
- Sharding versus query fan-out
- Partitioning adds capacity, but queries that cannot be routed narrowly must contact many shards and wait on their tail latency.
- Specialized service versus operational simplicity
- A dedicated vector database may offer useful indexing and scaling features, while vector search in an existing database can avoid another service when its capabilities meet the workload.
When to reach for it
- The requirement is semantic similarity, such as finding content with the same meaning, not just matching keywords or structured fields.
- A recommendation system needs a bounded candidate set from a catalog too large to score fully on every request.
- Exact vector search misses the latency or throughput target at the expected collection size and filter selectivity.
- The product can tolerate missing some nearest neighbors, with a measurable recall target and a fallback for weak results.
How to bring it up in the interview
- Start with the retrieval requirement, not a database name: define the collection size, vector dimensions, query rate, filters, latency budget, and freshness target.
- Propose exact search as the baseline, then justify ANN with measured or estimated work; interviewers may ask whether a filtered subset is small enough to scan.
- Define recall@k as the fraction of the exact top-k neighbors recovered, and benchmark it alongside p95/p99 latency on representative filtered queries.
- Separate ANN retrieval quality from product relevance: a perfect nearest-neighbor result can still be unhelpful if the embeddings or ranking model are poor.
- Be ready to explain tenant isolation, permission revocation, shard fan-out, and embedding-model migrations; reranking cannot recover relevant items that retrieval never found.
Common mistakes
- Treating a vector database as the embedding model, the source of truth, or a complete recommendation system.
- Claiming ANN returns the exact nearest neighbors or assuming a tuning parameter guarantees a fixed recall level.
- Retrieving only k candidates and then applying restrictive filters, leaving fewer than k usable results.
- Mixing incompatible embedding versions in one search space or rebuilding an index without planning a versioned cutover.
- Replacing keyword search with vectors everywhere, even when exact names, identifiers, and rare terms call for lexical or hybrid retrieval.
Where it comes up
Interview questions where vector databases and ann search is part of a strong answer, and why.
- Design TikTokANN can provide personalized candidates from a large video catalog, making retrieval quality, exclusion handling, and embedding freshness central to the recommendation path.
- Design a dating app like TinderEmbedding-based discovery must combine candidate retrieval with location, large exclusion histories, and current privacy rules without unbounded scanning.
- Design an ads serving systemWhen embeddings drive ad candidate retrieval, ANN must find relevant candidates within the serving deadline while preserving targeting and campaign eligibility checks.
Questions people ask
What is the difference between a vector database and ANN search?
A vector database manages vectors, metadata, updates, persistence, and queries. ANN is a family of retrieval algorithms it may use to find similar vectors without an exhaustive scan. Existing relational databases and search engines can also support vector indexes.
How do you measure approximate nearest neighbor search accuracy?
Run exact nearest-neighbor search on a representative evaluation set and compare the ANN results using recall@k. Measure latency, throughput, memory, and filtered-query behavior at the same time. Evaluate product relevance separately, because closeness in embedding space is not the same as usefulness.
Should I use vector search or full-text search?
Vector search is useful for semantic similarity, while full-text search is useful for exact terms, names, and lexical relevance. Hybrid search retrieves candidates from both and combines them through rank fusion or reranking. It adds retrieval work and tuning, so justify it with the kinds of queries the product must handle.