Guide / HNSW vs IVF

HNSW vs IVF

HNSW spends memory on graph navigation. IVF spends training and probing effort on coarse partitions.

The core difference

HNSW builds a proximity graph. Search walks from node to node through neighbor links, using upper layers for fast movement and a dense base layer for local refinement.

IVF clusters vectors around centroids. Search finds the nearest centroids, probes selected inverted lists, and compares candidates inside those lists.

Tuning controls

HNSW recall and latency are mainly shaped by M, efConstruction, and efSearch. IVF recall and latency are mainly shaped by nlist, nprobe, and list balance.

The knobs are not interchangeable. Raising efSearch explores more graph candidates. Raising nprobe searches more clusters.

Filtering and updates

HNSW can suffer when many graph candidates fail metadata filters. IVF can suffer when relevant filtered items are spread across unprobed lists.

For update-heavy workloads, both need implementation-specific scrutiny. HNSW graph maintenance and IVF centroid drift create different operational costs.

Complexity Table

IndexQuery shapeMemory shapeFailure mode
HNSWGraph traversal with efSearchVectors plus graph edgesRAM pressure, stale graph links, filter rejection
IVFProbe centroid-owned listsVectors plus centroid/list overheadBoundary misses, unbalanced lists, stale centroids
IVF-PQProbe lists and scan compressed codesLow vector payloadBoundary miss plus quantization error

When to Use This

  • Use HNSW when low latency and high recall matter and the index fits comfortably in RAM.
  • Use IVF when memory and scale matter, batch training is acceptable, and recall can be tuned through probing.

When Not to Use This

  • Avoid HNSW when RAM is the hard constraint or deletes and filters dominate the workload.
  • Avoid IVF when boundary misses are unacceptable and you cannot afford higher nprobe or reranking.

Production Failure Modes

HNSW fails noisily under RAM pressure because graph links, tombstones, and allocator overhead compete with vector payload.

IVF fails through routing mistakes: the right neighbor may sit in a cluster that was not probed, especially near centroid boundaries.

Animated SVG Diagram

HNSW graph traversal compared with IVF cluster probing One side shows graph hops through neighbors, while the other shows a query probing selected centroid cells. Query intent + constraints Lexical Signal terms, filters, IDs Vector Signal embeddings, ANN Ranked Context candidates + evidence
One side shows graph hops through neighbors, while the other shows a query probing selected centroid cells.

Next Topics

FAQ

Is HNSW better than IVF?

It depends on the workload. HNSW often wins on latency and recall when memory is available. IVF and IVF-PQ often win when scale and memory dominate.

Can HNSW and IVF use product quantization?

Yes. PQ can compress vector representations under either style, but it adds approximation error that may require reranking.

Which index should I test first?

Use exact search as ground truth, then test HNSW and IVF against recall, p95 latency, memory, filtering, and update behavior on your query set.