Topic 222 / Specialist
Vector databases and vector search
Chapter 13.14 · Vector Databases and Vector Search
What and why
RAG's retrieve step (→ 221) needs to find the chunks most similar in meaning to a query, which requires representing text (or other data) as vectors (embeddings) and finding the nearest vectors to a query vector by some similarity metric. Exact nearest-neighbor search over a large collection of high-dimensional vectors is too slow for most latency budgets, so vector databases exist to make approximate nearest-neighbor search fast enough for production, trading a small amount of accuracy for a large gain in speed. Builds on: → 221 (retrieval-augmented generation — the retrieve step this topic explains in depth).
Diagram
| Exact nearest neighbor | Approximate nearest neighbor (ANN) | |
|---|---|---|
| Cost | Grows linearly with corpus size | Narrowed by an index structure |
| Accuracy | Always the true closest match | A small, tunable chance of missing it |
| Fits a tight latency budget | Only for small corpora | Yes, by design |
How it works
- Embeddings represent items as points in a high-dimensional vector space such that similar items end up close together; a similarity metric (commonly cosine similarity or dot product) measures how close two vectors are.
- Exact nearest neighbor search compares a query vector against every stored vector and returns the truly closest ones — correct, but its cost grows linearly with the number of stored vectors, which doesn't fit a tight latency budget at large scale.
- Approximate nearest neighbor (ANN) trades a small, usually acceptable chance of missing the absolute closest vector for a large speedup, by building an index structure that narrows the search instead of scanning everything.
- HNSW (hierarchical navigable small world) builds a multi-layer graph where coarser layers allow long jumps toward the right neighborhood quickly and the finest layer refines the search locally; the recall/latency/memory trade-off is set by how many layers and connections the graph keeps — more raises recall and memory use, fewer lowers latency and memory at the cost of occasionally missing a true neighbor.
- IVF (inverted file index, applied to vectors) partitions the vector space into clusters; a query searches only the clusters nearest the query vector instead of the whole space — the trade-off is the number of clusters searched: more raises recall and latency, fewer lowers both.
- Hybrid keyword-plus-vector search combines a traditional inverted index (→ 209) for exact-term matches with vector similarity for semantic matches, then merges both result sets — useful because vector similarity alone can miss a query that depends on an exact term (a product code, a specific name) not well captured by meaning-based similarity.
- A pgvector-style extension adds vector columns and ANN indexing directly inside an existing relational database, avoiding a separate system; a dedicated vector database instead specializes entirely in vector storage and search, typically scaling further and offering more ANN index tuning.
Practical example
(illustrative). A corpus of 1,000,000 embedded passages: exact nearest-neighbor search compares a query against all 1,000,000 vectors every time. An IVF index with 1,000 clusters instead narrows the search to the 10 clusters nearest the query — roughly 1% of the corpus scanned per query — at the cost of occasionally missing a true nearest neighbor that happened to land in an unsearched cluster.
When to use it
- Use when: similarity search runs over a corpus large enough that exact comparison doesn't fit the latency budget — the common case for RAG retrieval (→ 221) and any "find similar items" feature.
- Avoid when: the corpus is small enough that exact nearest-neighbor search is already fast enough, or an existing relational database's pgvector-style extension already meets the scale and latency needs without adding a new system.
- Failure scenarios: tuning an ANN index for latency so aggressively that recall drops low enough to visibly degrade retrieval quality; relying on vector similarity alone and missing exact-term queries a hybrid keyword index would have caught; treating a pgvector-style extension and a dedicated vector database as interchangeable at every scale, when one may stop meeting latency or memory needs well before the other.
- Simpler alternative: exact nearest-neighbor search for small corpora, or keyword search alone (→ 209) when queries are dominated by exact terms rather than semantic similarity.
Practice
(Numeric kind — drills; 15–20 min, no lab services). Given a stated corpus size and a stated ANN index that searches a stated fraction of the corpus per query, compute how many vectors are scanned per query under exact search versus the ANN index, and compute the resulting speedup factor. Repeat for 2 more fraction values and tabulate how recall risk (qualitatively: higher or lower) trades against the speedup. Done when your 3-row table shows speedup and recall risk moving in opposite directions.
Key takeaway
Vector search trades a small, tunable chance of missing the true nearest neighbor for speed that scales to large corpora; hybrid search exists because meaning-based similarity and exact-term matching fail in different cases. Done when you can: compute the speedup an ANN index buys and name what it costs in recall. Used next in: nowhere else in this book — the terminal mechanism behind RAG's retrieval step and similarity-search features generally.
References
None — this topic makes no external claims.
Source directory