HNSW Indexes
A city map of shortcuts
HNSW builds a hierarchical graph of vectors where short links connect near neighbors and long links skip across the space. It's the default choice for low-latency semantic search — if you can stomach the RAM.
What you will learn
- Explain HNSW's layered graph
- Tune M and ef for your triangle
- Budget HNSW memory
What is this?
HNSW (Hierarchical Navigable Small World) organizes vectors in a multi-layer proximity graph: layer 0 has dense neighbor links, higher layers have sparser 'express lanes'. A query starts at the top, descends layers, and walks small neighborhoods to the answer — millisecond latency with excellent recall at the cost of RAM-hungry graph structure.
Why do you need it?
- Best-in-class latency/recall balance in practice.
- Understandable behavior: M controls links, ef controls search breadth.
- Your RAG stack likely uses HNSW by default — tune it deliberately.
- Memory math (M × 8 bytes × nodes) decides RAM budget before you buy.
Mental model
A city with express highways and local streets
HNSW layers
Highways (upper layers) carry you quickly across town via a few big hops; local streets (layer 0) refine to your exact destination. You enter a freeway, exit near the target, then walk street-by-street to the door — fast, reliable, but the map itself is huge.
How it works
- Insert: each new vector joins layer 0, and with probability p also higher layers (fewer).
- Layer 0 links each node to ~M nearest neighbors; upper layers fewer/different links.
- Search: from the top layer's entry, greedily descend to layer 0, expanding ef candidates.
- efConstruction (build time) vs efSearch (query time) control accuracy.
- Memory ≈ nodes × avg-links × 8 bytes (roughly (M+4)×8 × nodes).
Navigating down the layers.
In production
- Default for RAG semantic search: HNSW M=16-64, ef=query-latency tuned.
- Hybrid stacks filter inside the graph walk when the DB supports it.
- Multi-million-row services split the graph (shards) — HNSW memory per node stays sane.
Best practices
- Tune efSearch dial from 10 upwards until recall@k plateaus.
- Set M considering RAM: doubling M roughly doubles link memory.
- Batch rebuilds during off-peak to fight insert-driven graph rot.
- Monitor recall@k on a fixed sample weekly.
Common mistakes
- Cranking ef to 800 'for accuracy' and paying 20x latency.
- Ignoring that small corpus = simple linear search may beat HNSW entirely.
- Inserting millions one-off into a graph without periodic rebuild.
Common issues
Recall@k drops after mass insertsHNSW graph degeneration
Increase ef temporarily; schedule graph rebuilds.
HNSW eats the server RAMM/ef too high or node dims underestimated
Model memory; drop to smaller M or move to IVF/PQ.
Debugging tips
- Recall varies wildly per query → locality of query embeddings; sample more.
- RAM spike at ingest → efConstruction high and parallel inserts forking graphs.
- Searches slower after deletions → graph rot; rebuild.
Interview checkpoint
Quick quiz
In HNSW, higher layers contain…
Doubling the M parameter roughly doubles HNSW link memory.