HNSW
A multi-layer proximity graph algorithm (combining Skip Lists and Small World graphs) that achieves fast, high-recall Approximate Nearest Neighbor search in vector databases
Table of Contents
Structure Diagram
What is HNSW?
A multi-layer proximity graph algorithm (combining Skip Lists and Small World graphs) that achieves fast, high-recall Approximate Nearest Neighbor search in vector databases. It is a core concept in Retrieval & RAG within AI / ML.
Core Principles
HNSW (Hierarchical Navigable Small World) is a graph-based ANN index that fuses multi-layer Skip Lists with Small World proximity graphs.
Sparse upper layers act like express highways to leap rapidly toward the query neighborhood, while dense lower layers refine local candidates—achieving near-logarithmic $O(\log N)$ query latency with 95–99% recall.
Practical Takeaways
- Master the three tuning knobs:
M(max edges per node, typically 16–32),efConstruction(build-time search breadth), andefSearch(query-time search breadth) directly govern the recall–latency–memory triangle. - Plan for RAM footprint: In-memory HNSW graphs consume substantial RAM at tens of millions of vectors. Pair HNSW with Scalar (INT8) or Binary Quantization to slash memory usage by 4–32×.
- Monitor deletes and compaction: Heavy deletion churn leaves tombstones that degrade graph traversal unless periodic segment compaction is enabled.
Notable Examples
- Qdrant, Weaviate, Milvus, pgvector & Faiss: Rely on HNSW as the go-to index for low-latency, high-recall vector search.
- High-QPS RAG Backends: Retrieve Top-20 chunks across 10M+ embeddings in single-digit milliseconds.
- Quantized HNSW + Rescoring: Traverse compressed vectors in RAM, then rescore the top candidates against full-precision vectors on disk.