data-infra

HNSW (Hierarchical Navigable Small World)

HNSW is the most widely used algorithm for approximate nearest-neighbor search — the core operation behind vector databases and semantic search. Given a query embedding, it finds the closest vectors among millions without scanning all of them. It builds a multi-layer graph where each vector links to its neighbors; search starts at a sparse top layer for coarse navigation and descends into denser layers to refine, giving near-logarithmic search time. Why it matters for SaaS builders: HNSW is what makes RAG and semantic search feel instant at scale. It's the default index in Pinecone, Weaviate, Qdrant, Milvus, and pgvector. The trade-offs live in two build parameters. M (links per node) and ef_construction control index quality; ef_search controls the accuracy/speed trade-off at query time — higher values find more true neighbors but cost latency. Practical note: HNSW is memory-hungry because the graph lives in RAM, and deletes are handled as soft tombstones, so heavy churn eventually needs a rebuild. Tune ef_search against a labeled recall test rather than guessing.

Related terms

More Data & Infra terms