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
| Component | What it stores | Memory pressure | Failure signal |
|---|---|---|---|
| Vector payload | Embedding coordinates | Scales with N x dimensions | Instance RAM fills quickly |
| Graph links | Neighbor IDs per node | Scales with connectivity | High RAM and build cost |
| Metadata filters | Payload indexes and constraints | Workload dependent | Filtered recall drops |
| Headroom | Rebuilds, replicas, allocator margin | Operational | OOM 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
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.