Managing billion-scale vector similarity search for Retrieval-Augmented Generation (RAG) and recommendation systems requires indexing high-dimensional embeddings (e.g. 1,536-dimensional vectors) with sub-10 millisecond retrieval latencies. Linear k-NN scan ($O(N \cdot d)$) is computationally prohibitive at scale.
Vector search databases rely on Approximate Nearest Neighbor (ANN) indexing structures. In this engineering reference guide, we examine the internal graph topology of Hierarchical Navigable Small World (HNSW), explore out-of-core SSD indexing with DiskANN, mathematically break down Product Quantization (PQ) compression, and construct a Python Faiss benchmark pipeline.
1. HNSW Graph Architecture: Multi-Layer Skip-List Mechanics
HNSW (Malkov & Yashunin) constructs a multi-layer graph hierarchy inspired by 1D skip-lists. The top layer $L_{\text{max}}$ contains sparse long-range highway edges across cluster centroids, while the bottom layer $L_0$ contains all vectors with short-range local connections.
Search proceeds greedily from top layer $L_{\text{max}}$ down to layer $L_0$. The maximum layer level for a newly inserted vector is assigned stochastically via an exponential decay probability distribution:
Where $m_L = \frac{1}{\ln(M)}$ scales the average layer height based on target degree parameter $M$ (typically $M \in [16, 64]$).
2. Product Quantization (PQ) Vector Compression Math
For a 1,536-dimensional FP32 vector (6,144 bytes per vector), storing 1 billion vectors in RAM requires over 6 Terabytes of memory. Product Quantization (PQ) compresses high-dimensional vectors by 16x–64x with minimal loss in search recall.
PQ breaks a $D$-dimensional vector into $m$ distinct sub-vectors of dimension $d' = D / m$. A k-means clustering step builds $K^*$ centroids (usually $K^* = 256$) for each sub-space:
Each 1,536-dim vector is replaced by $m$ 8-bit byte indices ($m$ bytes total), reducing storage from 6,144 bytes to 96 bytes!
3. DiskANN: Out-Of-Core SSD Indexing for Billion-Scale Datasets
While HNSW requires holding the entire graph in expensive RAM, DiskANN (Subramanya et al.) stores compressed PQ vectors in RAM while maintaining the full uncompressed Vamana graph on NVMe SSD drives.
During query execution, DiskANN uses asynchronous `io_uring` direct I/O to fetch graph neighbor pages from SSD in a single I/O request, achieving 95%+ recall@10 at 5,000 QPS on a single workstation node.
Join the Technical Discussion
Have questions about this architecture? Drop a comment below.