IVF vs HNSW: Vector Index Algorithms Compared
IVF clusters vectors into partitions to narrow a search; HNSW builds a navigable graph. Both trade recall for speed differently at scale.
IVF (inverted file index) and HNSW (hierarchical navigable small world) are the two dominant algorithms behind approximate nearest neighbor search in vector databases. Both exist to solve the same problem — finding the vectors closest to a query without comparing it to every vector in the collection — but they get there through very different structures: IVF partitions the space into clusters and searches only the most promising ones, while HNSW builds a layered graph that lets a search walk toward the nearest neighbors a few hops at a time.
The problem both are solving
An exact nearest-neighbor search compares a query vector against every vector in the dataset and sorts by distance — a brute-force scan that costs O(n) per query. That’s fine for a few thousand vectors, but it doesn’t scale to the millions or billions of embeddings a production RAG system or recommendation engine might index. Approximate nearest neighbor (ANN) algorithms trade a small, usually tunable amount of accuracy — measured as recall, the fraction of true nearest neighbors actually returned — for a large speedup, typically sublinear in the size of the dataset.
IVF: cluster first, search narrower
IVF’s approach is to partition the vector space before any query arrives:
- Training — run a clustering algorithm (commonly k-means) over a sample of the dataset to produce a fixed number of cluster centroids.
- Indexing — assign every vector in the dataset to its nearest centroid, building an inverted list per cluster (hence “inverted file,” borrowing the term from text search indexes).
- Querying — find the
nprobecentroids closest to the query vector, then search only the vectors assigned to those clusters, ignoring everything else.
The nprobe parameter is IVF’s main recall/speed knob: searching more clusters improves recall (you’re less likely to miss a true neighbor that landed in a cluster you skipped) at the cost of scanning more vectors. IVF is often combined with vector compression — product quantization is the common pairing — to shrink each vector’s memory footprint, trading some additional accuracy for a much smaller index that can fit in memory at a larger scale.
HNSW: navigate a layered graph
HNSW takes a completely different structural approach. It builds a multi-layer graph where each vector is a node, and edges connect a vector to a set of its approximate nearest neighbors:
- Layered structure — the top layer has very few nodes and long-range edges, acting like an express lane across the vector space. Each layer below has progressively more nodes and shorter, denser edges, down to the bottom layer, which contains every vector.
- Insertion — a new vector is inserted at a randomly chosen top layer (following a probability distribution that keeps higher layers sparse) and linked to its nearest existing neighbors at each layer it appears in, down to the bottom.
- Querying — a search starts at the top layer’s entry point and greedily walks toward the query vector, hopping to a closer neighbor at each step. When no closer neighbor exists at the current layer, the search drops down a layer and continues, repeating until it reaches the bottom layer and converges on the nearest neighbors.
This resembles a skip list’s layered shortcut structure — see what a skip list is — applied to a graph instead of a sorted sequence: coarse long-range hops at the top narrow the search fast, and finer hops near the bottom refine it. HNSW’s main tuning knobs are the number of connections per node (M) and the search breadth at query time (ef), both trading memory and speed against recall in a similar way to IVF’s nprobe. For a deeper look at how the graph itself is constructed and searched, see HNSW explained.
Comparing the two
| IVF | HNSW | |
|---|---|---|
| Core structure | Clustered inverted lists | Multi-layer navigable graph |
| Build step | K-means clustering (needs training data) | Incremental graph construction, no separate training pass |
| Memory overhead | Lower, especially with quantization | Higher — graph edges add overhead per vector |
| Query speed at high recall | Slower than HNSW at comparable recall | Generally faster at comparable recall |
| Updates (inserts/deletes) | Efficient — new vectors just join a cluster | More expensive — graph edges need rebalancing |
| Typical use case | Very large datasets where memory is the binding constraint | Latency-sensitive search where memory budget allows a larger index |
Which one to reach for
IVF tends to win when a dataset is large enough that memory becomes the primary constraint — its ability to pair with quantization to shrink vectors makes it a common choice for billion-scale collections where keeping the whole index resident in memory would otherwise be prohibitive. It also handles inserts more gracefully, since adding a vector just means assigning it to an existing cluster rather than rewiring graph edges.
HNSW tends to win when query latency matters most and the dataset comfortably fits the available memory budget — it generally achieves higher recall at a given query speed than IVF, which is why it’s the default index type in many vector database and search library implementations. Its weaker spot is high-throughput insert/delete workloads, where the graph’s edge structure adds rebalancing overhead that IVF’s cluster assignment avoids.
In practice, many production systems don’t pick purely one or the other — hybrid approaches that combine IVF-style clustering with a graph or quantization layer inside each cluster are common in large-scale deployments, aiming to get IVF’s memory efficiency with graph-search-like recall.
The takeaway
IVF and HNSW both turn an expensive exact nearest-neighbor scan into a fast approximate one, but through opposite structural strategies: IVF narrows the search space by clustering vectors ahead of time, while HNSW builds a navigable graph that lets a query walk directly toward its nearest neighbors. IVF generally wins on memory efficiency and update cost at very large scale; HNSW generally wins on query latency and recall when memory isn’t the binding constraint. The right choice comes down to which resource — memory or latency — is actually scarce in your deployment.
Tagged
Keep reading
Chisato · · 4 min read Hybrid Search: Combining BM25 and Vector Search
Hybrid search blends keyword-based BM25 ranking with semantic vector search, fixing the blind spots each method has on its own.
Chisato · · 4 min read HNSW Explained: How Vector Search Finds Neighbors Fast
HNSW builds a multi-layer graph of vectors so nearest-neighbor search runs in roughly logarithmic time instead of scanning every row.
Chisato · · 4 min read What Is a Knowledge Graph?
A knowledge graph stores facts as entities and labeled relationships instead of rows or documents, letting queries traverse connections directly.