HNSW vs IVF, when do you pick each for a production vector index?
Pick HNSW when latency and recall matter and memory is cheap (sub 100M vectors). Pick IVF (usually IVF-PQ) when scale is huge or memory is the binding cost.
Imagine you have a giant photo library and you want to find similar photos to one you just took. **HNSW** is like building a smart web of links between photos: every photo points to a few of its most similar neighbors, and a few of those neighbors point to far-away clusters. When you search, you hop along the links, getting closer and closer to matches in just a few jumps. It is very fast but the link directory takes up a lot of room. **IVF** is like sorting all your photos into labeled bins first. When you search, you only open a few bins that look promising. That is slower per search but the bins take far less storage, and you can shrink each photo into a tiny thumbnail to save even more space.
Detailed answer & concept explanation~4 min readEverything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
4 min: HNSW graph mechanics + IVF partitioning + Product Quantization composition + memory and recall tradeoffs + 2026 production choices (Pinecone, Weaviate, Qdrant, Milvus).
| Property | HNSW | IVF (often IVF-PQ) |
|---|---|---|
| Index type | Hierarchical graph | Partitioned (k-means) inverted file |
| Latency at high recall | Sub millisecond | 1-10 ms typical |
| Memory overhead | ~2-4x raw vectors | Small; PQ shrinks vectors 10-50x |
| Sweet spot scale | 1M to 100M vectors | 100M to billions |
| Recall ceiling | 0.99+ achievable | 0.85-0.95 typical at low cost |
| Build time | Slow (O(N log N)) | Faster (k-means + assign) |
Real products, models, and research that use this idea.
- Pinecone's serverless tier defaults to HNSW for indexes under 50M vectors, switching to compressed variants at higher scale.
- Weaviate exposes HNSW as the default index and offers Product Quantization as a per-collection option.
- Qdrant uses HNSW with optional scalar and binary quantization, popular for cost sensitive RAG deployments.
- Milvus implements both IVF-PQ and HNSW; LinkedIn and Salesforce have published billion vector deployments on it.
- Meta's Faiss library is the reference implementation for IVF-PQ at billion scale, used inside many in house search systems.
What an interviewer would ask next. Try answering before peeking at the approach.
QHow would you decide nprobe for an IVF index?
QWhy might you combine IVF-PQ recall with HNSW re-ranking?
Don't say thisRed flags and common mistakes that signal junior thinking. Click to expand.
Red flags and common mistakes that signal junior thinking. Click to expand.
Picking HNSW for billion scale corpora without budgeting the memory blowup; the graph index alone can dwarf the raw vector storage.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.
Same topic, related formats. Practice these next.