HNSW: Navigable Small World Graphs

HNSW is the algorithm behind most modern vector databases, and its idea is borrowed from the "six degrees of separation" that connects any two people through a short chain of acquaintances. Build the right graph of vectors, and you can walk from a random entry point to a query's nearest neighbors in a handful of hops — searching millions of vectors while touching only a few hundred.

IVF beats brute force by partitioning space. HNSW — Hierarchical Navigable Small World graphs — beats it a different way: by building a graph you can navigate greedily toward any query’s neighborhood. It typically achieves higher recall at the same latency than IVF, which is why it’s the default index in most vector databases. This post explains the small-world idea, the hierarchy that makes it fast, and the parameters that control its trade-offs.

The small-world idea

Imagine a graph where each vector is a node connected to some of its nearest neighbors. To find the neighbors of a query, you could start at any node and greedily walk: repeatedly move to whichever neighbor is closest to the query, until you can’t get closer. On a purely local graph (only short-range links), this walk is slow — you shuffle step by tiny step across the space, like traveling the world one town at a time.

The small-world insight fixes this. Real social networks connect any two people in a short chain (“six degrees of separation”) because, alongside many local connections, there are a few long-range ones. A graph with both short- and long-range links is a navigable small world: the long-range links let a greedy walk take big jumps across the space to get near the target fast, then the short-range links refine to the exact neighborhood. That combination — long hops to approach, short hops to home in — lets you reach any query’s vicinity in a small number of steps even in a huge graph.

The hierarchy: the “H” in HNSW

HNSW adds a hierarchy of layers to make the “big jump, then refine” structure explicit and efficient — the idea that gives it the edge over a flat navigable graph. Think of it like a transport network with express and local layers:

Layer 2 (sparse):   A ─────────────── D          few nodes, long links
                     │                 │
Layer 1 (medium):   A ──── B ───── C ─ D          more nodes, medium links
                     │      │       │   │
Layer 0 (all nodes): A-B-C-D-E-F-G-...           every vector, short links

This is analogous to how a skip list (or a coarse-to-fine map) works: the top layers get you to the right region in a few big moves, and the bottom layer pins down the precise neighbors. The result is search that scales roughly logarithmically — searching millions of vectors touches only hundreds of nodes, not millions.

The parameters

HNSW’s trade-offs are controlled by three parameters, split between build time and query time:

The practical mental model: M and efConstruction set the graph’s quality (and memory) at build time; efSearch dials recall vs. speed at query time. You tune efSearch per workload to hit your recall target — measured, as always, against brute-force ground truth.

Strengths and weaknesses

HNSW’s profile explains its popularity and its costs:

Strengths:

Weaknesses:

HNSW plus quantization

HNSW’s memory appetite is often addressed by combining it with quantization (the next post): store compressed vectors to cut the memory cost while keeping the graph’s fast navigation. Variants that pair HNSW with product quantization or scalar quantization are common in production vector databases precisely to tame the memory weakness. So the two dominant algorithms both lean on quantization to scale — IVF-PQ for partition-based, HNSW+quantization for graph-based.

Choosing HNSW

HNSW is the right default for most vector search when memory allows:

Reach for IVF instead when memory is the binding constraint at very large scale (IVF-PQ compresses better), or when HNSW’s build cost and deletion awkwardness don’t fit your workload. With both dominant algorithms covered, the next post tackles the memory problem they both face — quantization — which is how either scales to billions of vectors.

Key takeaways

Further reading

Sources & References