Index Structures
Flat (exact), IVF (partition-based), HNSW (graph-based), DiskANN (SSD-resident) β the algorithms behind vector search.
Vector search finds the most similar items to a query vector. The index structure determines the speed-recall-memory tradeoff β from exact brute-force to billion-scale approximate search.
1. The Search Problem
Given a query vector and a database of vectors, find the nearest neighbors:
| Approach | Time complexity | Recall | Memory | Scale |
|---|---|---|---|---|
| Flat (brute-force) | 100% (exact) | <1M vectors | ||
| IVF | 95-99% | 1M-100M | ||
| HNSW | 95-99% | 1M-100M | ||
| DiskANN | 95-99% | on SSD | 1B+ |
2. IVF (Inverted File Index)
Partition vectors into clusters using k-means, then search only nearby clusters:
Build: Run k-means on all vectors β C centroids
Each vector assigned to nearest centroid's partition
Search: Find nprobe closest centroids to query
Search only vectors in those nprobe partitions
nprobe=1: fastest, lowest recall
nprobe=C: equivalent to brute force (100% recall)
Tuning: More partitions (C) = faster search per partition, but need more nprobe for recall. Typical: C=1024-4096, nprobe=16-64.
3. HNSW (Hierarchical Navigable Small World)
A multi-layer graph where each layer is a βskip listβ for the next:
Layer 2: [sparse graph β few nodes, long-range connections]
Layer 1: [medium graph β more nodes, medium connections]
Layer 0: [dense graph β all nodes, short-range connections]
Search: Start at top layer, greedily walk toward query
β Drop to lower layer at each step for finer resolution
β Layer 0 gives the final nearest neighbors
Pros: Very fast (), high recall, no training needed. Cons: High memory (graph edges stored alongside vectors), expensive to build.
4. DiskANN
Store vectors on SSD instead of RAM β enabling billion-scale search on commodity hardware:
In-memory: compressed vector representations + graph edges
On SSD: full-precision vectors (read on demand during search)
Search: Navigate graph in memory β load candidate vectors from SSD β rerank
Key insight: The graph navigation (cheap, in memory) identifies candidates. Only the final reranking reads from SSD β minimizing I/O.
5. Choosing an Index
| Your data | Best index | Why |
|---|---|---|
| < 100K vectors | Flat | Exact, simple, fast enough |
| 100K - 10M | HNSW | Best recall/speed tradeoff |
| 10M - 100M | IVF + HNSW | Combined benefits |
| > 100M, fits in RAM | HNSW with compression (PQ) | Compress vectors to fit |
| > 1B, wonβt fit in RAM | DiskANN | SSD-resident, low memory |
For the systems built on these indexes, see System Architecture.
Self-Check
- Explain IVF partitioning and the nprobe-recall tradeoff
- Explain HNSWβs multi-layer graph and why itβs O(log N)
- Explain DiskANNβs split between in-memory graph and SSD vectors
- Choose an index type for 50M vectors with 99% recall requirement