Skip to main content
databases

Vector databases — HNSW, IVF, ScaNN

13 min read
Fully authored

The similarity-search index behind every RAG system in 2025.

You have a billion documents, each embedded as a 1536-dimensional vector. A user query arrives; you must find the top-10 most similar vectors in under 100ms. A brute-force scan takes minutes. The answer is an approximate nearest neighbor (ANN) index — the algorithm every vector database is built around.

Interactive latency calculator

Brute-force
6.1 s
IVF (nprobe=32)
49 ms
HNSW
6.0 ms

How brute-force works

Compute cosine similarity (or L2 distance) between the query vector and every stored vector. Return top-K. Trivially parallelizable on GPU with matrix multiplication.

  • Query complexity: O(N × D) — linear in corpus size.
  • Recall: 100% — this is the ground truth.
  • Use case: corpora up to ~100K vectors, or GPU-backed at 1M-10M, or as evaluation baseline for ANN algorithms.
  • Reference impl: FAISS IndexFlatIP or numpy @ operator.

The 2025 vector DB landscape

DatabasePrimary algoBest forNotes
PineconeProprietary (HNSW-based)Managed cloudServerless, per-query pricing, easiest to start
QdrantHNSWSelf-host + filteringExcellent metadata filtering, Rust core
WeaviateHNSWHybrid retrievalBuilt-in BM25 + reranking
MilvusHNSW / IVF / DiskANNLarge scale (100M+)GPU support, tunable per collection
pgvectorIVFFlat / HNSWPostgres integrationYou already have Postgres? Start here.
FAISSAll above + moreResearch + on-premLibrary only, not a service. Backs many others.
VespaHNSW + ANNMulti-stage retrievalYahoo internal → open source, full search stack
ChromaHNSWPrototypingSimplest API, small corpora

The senior insight — recall vs latency is a knob, not a fixed number

Every ANN algorithm has a tuning parameter (nprobe for IVF, ef for HNSW). Higher value = more candidates checked = higher recall = more latency. In production you tune this per QUERY class — high-value queries get higher recall, cheap queries get lower. This is why RAG systems often surface a “quality mode” toggle.

References

  • Malkov, Yu. A. and Yashunin, D. A. (2018) — “Efficient and robust approximate nearest neighbor search using hierarchical navigable small world graphs.” The HNSW paper.
  • Jégou, H. et al. (2011) — “Product quantization for nearest neighbor search.” Foundational IVF paper.
  • Guo, R. et al. (2020) — “Accelerating large-scale inference with anisotropic vector quantization.” ScaNN paper from Google.
  • ANN Benchmarks: ann-benchmarks.com — reproducible latency/recall comparison of every algorithm on standard datasets.

Practice what you just read

Every foundation concept has a companion quiz to close the loop.