HNSW Tuning: Find the Recall, Latency, and Cost Operating Point
[Definition] HNSW tuning is the process of selecting graph connectivity and search breadth so approximate nearest-neighbour retrieval meets a required Recall@k and tail-latency target at an acceptable memory and indexing cost.
The system-design triangle
Vector retrieval has three competing outcomes:
Recall
▲
/ \
/ \
/ \
Cost ───── LatencyHigher recall usually means exploring more graph candidates, building denser connections, or storing higher-precision vectors. Each can increase latency, memory, build time, or all three. There is no universal best configuration—only a defensible operating point for a workload.
The three HNSW parameters that matter
| Parameter | When it acts | What increasing it does | Cost |
|---|---|---|---|
M | index construction and storage | more links per node; better graph connectivity | more memory and slower indexing |
ef_construction | index construction only | considers more candidate neighbours while building | slower indexing; better graph quality |
ef_search | every query | explores more candidates before returning top-k | higher query latency; better recall |
M: graph connectivity
M is the maximum number of neighbour links a node retains. A common starting point is 16. Consider 32 when high recall matters more than memory.
memory per vector ≈ vector bytes + (M × link bytes)Increasing M can help difficult, high-dimensional or clustered data, but it is an index rebuild decision. Do not change it first if query-time recall is slightly low.
ef_construction: build-time quality
During insertion, HNSW considers ef_construction candidates before choosing neighbours. A practical baseline is often 100–256. More candidates usually produce a navigable graph, but indexing takes longer and the benefit eventually plateaus.
ef_search: the first query-time lever
ef_search controls how many candidates are explored for a query. It is normally the safest first tuning knob because it changes query quality without rebuilding the index.
ef_search | Typical result | Use case |
|---|---|---|
| 50 | low latency, lower recall | high-volume tolerant search |
| 100 | balanced baseline | most production workloads |
| 200 | stronger recall, more work | precision-sensitive knowledge search |
| 500 | near-exhaustive behaviour | offline research or small traffic |
The actual numbers depend on data, dimensions, filters, model, and hardware. Measure them; do not copy them as a service-level objective.
Tune with exact ground truth
Approximate recall cannot be measured from approximate results alone. Build a held-out query set, run exact kNN for its ground truth, then compare HNSW output:
Recall@10 = |top10_HNSW ∩ top10_exact| / 10For graded relevance, also report NDCG@10. Exact kNN can be too expensive for every production request, but it is essential for an offline benchmark sample.
A repeatable tuning loop
- ▸Freeze the variables. Pin the embedding model, normalization rule, document corpus, chunking scheme, metadata filters, and top-k.
- ▸Create a representative query set. Include head queries, rare terminology, multi-document questions, and important business slices.
- ▸Build the baseline. Start with
M=16,ef_construction=200–256, andef_search=100unless the engine has stronger defaults. - ▸Sweep
ef_search. Test values such as 50, 100, 150, 200, and 300; record Recall@k, NDCG, P50/P95/P99, and QPS. - ▸Pick the lowest value that reaches the quality target. If recall plateaus below target, rebuild with higher
Moref_construction. - ▸Re-test filters and load. Metadata filters, concurrency, cache state, segment count, and tail load can invalidate a quiet benchmark.
- ▸Version the result. Record index parameters, corpus version, embedding model, benchmark set, and acceptance thresholds.
Read the curve, not one score
Recall@10
1.00 | ●
0.98 | ●
0.96 | ●
0.94 | ●
0.92 | ●
+-----+-----+-----+-----+---- ef_search
50 100 150 200 300If recall rises sharply from 50 to 100 but barely changes after 150, the extra query work above 150 is likely not useful. This plateau is more informative than a generic recommendation such as “set ef_search to 500.”
Filters, segments, and the hidden latency costs
HNSW settings are not the only levers.
Pre-filter versus post-filter
Highly selective metadata filters can reduce the candidate set before vector traversal. Post-filtering may return too few eligible neighbours even when the underlying vector result is good. Test filter-heavy query slices separately.
Segment management
After a bulk load, merged/optimized segments can improve search latency substantially. Many small segments multiply graph traversal overhead. Treat index lifecycle operations as part of the benchmark, not an unrelated operations task.
Request caching
Caching repeated semantic queries improves latency and cost but says nothing about raw HNSW quality. Report cached and uncached performance separately.
Memory and storage choices
Before increasing infrastructure, check whether vector representation is the real cost driver.
| Choice | Memory impact | Typical tradeoff |
|---|---|---|
| FP32 vectors | baseline | maximum fidelity, highest memory |
| FP16 | roughly half vector memory | small quality impact for many workloads |
| INT8 | roughly quarter vector memory | evaluate recall; rescoring may help |
| binary / aggressive quantization | large savings | requires careful quality validation |
| disk-backed vectors | less RAM pressure | higher latency than in-memory HNSW |
Quantization changes the retrieval problem. Re-run the same ground-truth suite after every representation change.
Workload decisions
| Workload | Prefer | Rationale |
|---|---|---|
| E-commerce discovery | moderate latency + high recall | hybrid retrieval, caching, selective filters |
| Internal knowledge base | balanced cost and quality | HNSW baseline, evaluate answer quality too |
| Legal/compliance | high recall + citations | larger ef_search, reranking, explicit abstention |
| Write-heavy streaming data | ingestion resilience | consider IVF/disk options or batch rebuilds |
| Tiny corpus | simplicity | exact kNN may be fast enough |
Free concepts, Pro tuning workflow
The article and its benchmark method are public. The Pro search path turns the method into a guided practice: build the ground-truth set, sweep parameters, inspect recall/latency curves, choose a storage tier, and document the resulting service-level objective. A Live Cohort capstone adds review of the index design and production runbook.
Next steps
- ▸Read kNN and HNSW for graph mechanics.
- ▸Read Measuring Search Quality for NDCG and benchmark design.
- ▸Read Quantization before changing vector precision.
- ▸Use the learning roadmap to see where index tuning fits the Pro production-search path.