Retrieval
HNSW
A layered graph. Search walks sparse highways first, then a dense bottom layer. Tune M, efConstruction and efSearch.
HNSW (Hierarchical Navigable Small World) is the default ANN index in most modern vector stores. It builds a multi-layer graph. The bottom layer contains every vector, densely linked to nearby neighbors. Upper layers are successively sparser "highways" with long-range edges.
Search starts at the top, greedily walks to the neighbor closest to the query, then drops a layer and repeats. Upper layers jump across the space. The bottom layer refines locally.
Concept
Before you start: ANN trades a little recall for speed. HNSW is the usual way to make that trade on one machine.
A highway system is the usual picture. You take the interstate to the right city, then local streets to the address. The analogy breaks because the graph is built from your vectors, not from a map, and a bad build (efConstruction too low) leaves missing on-ramps.
Parameters
| Parameter | Role | Effect of increasing |
|---|---|---|
M | Max edges per node (per layer) | Better recall, more RAM, slower build |
efConstruction | Candidate list size while building | Better graph quality, much slower inserts |
efSearch | Candidate list size at query time | Better recall, higher latency |
Rule of thumb: start with M around 16–32, efConstruction 100–200, tune efSearch online (32–128+) until recall plateaus. OpenSearch and others publish configuration portfolios. Always validate on your vectors.
Example
You measure exact top-10 neighbors for 200 queries offline. With efSearch=32, recall@10 = 0.88 and p95 = 8 ms. Raising efSearch to 64 yields recall@10 = 0.96 and p95 = 14 ms. Raising to 256 yields 0.98 recall but p95 = 40 ms. If the LLM already needs 800 ms to generate, paying 14 ms for 0.96 recall is usually right. Paying 40 ms for another two points may not be.
Warning
HNSW is memory-hungry. Graph edges plus full-precision vectors often take 2–4× the raw float32 footprint. If you are RAM-bound, look at PQ compression or disk-oriented indexes before blindly raising M.
Inserts are not free. Each new point must be connected into multiple layers. Bulk-building from a static snapshot is faster and usually higher quality than inserting one vector at a time with a low efConstruction. For streaming corpora, batch upserts (every N minutes or N documents) and periodically rebuild if recall drifts.
Walking through memory
Vectors: 10,000,000
Dimensions: 1,024
Precision: float32 (4 bytes)
Raw payload = 10e6 × 1024 × 4
= 40,960,000,000 bytes
≈ 38.1 GiB (often quoted as "~40 GB")
HNSW overhead (edges, links): commonly another ~1–2×
→ plan on ~80–120 GiB RAM for a comfortable in-memory HNSW of this sizeParameters tuned at 1M vectors may under-recall at 20M with the same efSearch. Re-measure after growth.
Common mistakes
- Raising
Mto fix recall when you are already RAM-bound. TryefSearchfirst, then PQ or disk. - Inserting one vector at a time with a low
efConstruction. Bulk-build when you can. - Never rechecking recall after the corpus grows 10×.
Check yourself
efSearch=32gives recall@10 = 0.88 at 8 ms. You need ≥ 0.95. What do you try first? RaiseefSearch(to around 64 in the example) and re-measure latency.- Why can 10M × 1,024 float32 vectors need ~80–120 GiB with HNSW? Raw vectors are ~40 GB. Graph edges often add another 1–2×.
Key takeaways
- HNSW walks sparse upper layers, then a dense bottom layer.
- Tune
M,efConstructionandefSearch. Start withM16–32. - Budget several times the raw vector RAM.
- Bulk-build when you can. Re-measure recall after the corpus grows.
Further learning
OpenSearch: practical HNSW hyperparametersStruggling to balance search quality and speed in your LLM applications? This post dives into optimizing HNSW for vector search, offering pre-computed configurations to help you achieve better recall and throughput wi…OpenSearch
Next, indexes for when HNSW no longer fits: IVF, PQ and DiskANN.
Related
Vector search and vector databases
Approximate nearest neighbor algorithms (HNSW, IVF, PQ) and choosing between pgvector and dedicated stores.
Exact vs approximate search
Exact kNN checks every vector. ANN misses a few neighbors on purpose so a 10-million-row index can answer in milliseconds.
IVF, PQ and DiskANN
Cluster, compress or spill to SSD when a pure HNSW graph no longer fits in RAM.