Guide / HNSW memory usage explained

HNSW Memory Usage Explained

HNSW is fast because it stores a navigable graph in memory. That graph is a real budget item, not an implementation detail.

Why HNSW uses extra memory

HNSW stores vectors plus graph edges that connect nearby points. Search is fast because it walks those links instead of scanning every vector.

The graph is the cost of that speed. Higher connectivity can improve recall and latency, but it also increases memory and build cost.

Memory components

The main pieces are raw vector payload, neighbor links, node IDs, level metadata, deleted flags, payload or filter indexes, candidate queues, and allocator overhead.

Production systems also need replicas, shard headroom, and rebuild buffers. If those are ignored, HNSW can look affordable on paper and fail under real ingestion or filtering.

Tuning memory against recall

M controls graph connectivity. efConstruction controls build-time exploration. efSearch controls query-time exploration. These settings affect recall, latency, and memory in different ways.

Measure HNSW against exact search ground truth. Do not tune only for p50 latency; memory pressure and filtered queries usually show up in tail latency and recall misses.

Complexity Table

ComponentWhat it storesMemory pressureFailure signal
Vector payloadEmbedding coordinatesScales with N x dimensionsInstance RAM fills quickly
Graph linksNeighbor IDs per nodeScales with connectivityHigh RAM and build cost
Metadata filtersPayload indexes and constraintsWorkload dependentFiltered recall drops
HeadroomRebuilds, replicas, allocator marginOperationalOOM during ingestion or compaction

When to Use This

  • Use HNSW when low latency and high recall matter and the index can fit comfortably in RAM.
  • Use it when the workload is read-heavy enough to justify graph build and maintenance cost.
  • Use this page to budget graph overhead separately from raw vectors.

When Not to Use This

  • Do not choose HNSW blindly when RAM is the hard constraint.
  • Do not ignore delete-heavy or filter-heavy workloads; both can stress graph quality and candidate selection.

Production Failure Modes

HNSW fails when graph links, metadata, deleted markers, and replicas compete with raw vector payload for the same RAM budget.

Filtered search can fail when many nearby graph candidates are rejected after traversal, leaving too few valid results unless the system compensates.

Animated SVG Diagram

HNSW memory layers beside vector payload Raw vectors, graph links, metadata filters, deleted markers, and headroom combine into the HNSW memory footprint. Query intent + constraints Lexical Signal terms, filters, IDs Vector Signal embeddings, ANN Ranked Context candidates + evidence
Raw vectors, graph links, metadata filters, deleted markers, and headroom combine into the HNSW memory footprint.

Next Topics

FAQ

Why does HNSW need more memory than Flat search?

HNSW stores graph neighbor links in addition to the vector payload. Those links let search navigate quickly, but they add memory overhead.

Which HNSW setting affects memory most?

M is the most direct connectivity setting for graph memory. Implementation details also affect IDs, levels, deleted markers, and allocator overhead.

Can product quantization reduce HNSW memory?

It can reduce vector payload memory, but graph links and metadata still remain. Measure recall because compression adds approximation error.