Skip to content
tezvyn:
Menu

Tuning HNSW for recall vs latency

Source: interviewHardHow cards are made

Summary

tuning ANN index parameters.

Key points

ANN trades exactness for speed, and HNSW knobs M and efConstruction shape graph quality while efSearch trades query latency for recall at runtime.

What's really being asked

This evaluates whether you understand that approximate nearest neighbor search trades accuracy for speed, and can intelligently tune HNSW parameters against an application's latency and recall budget.

The core trade-off

Exact nearest neighbor search over millions of high-dimensional vectors is too slow because it must compare against every vector. ANN indexes accept a small chance of missing the true nearest neighbors, measured as recall, in exchange for sublinear query time. So the fundamental dial is recall versus latency: more thorough search raises recall but costs time, and you cannot have maximal recall and minimal latency at once.

Hnsw parameters

HNSW builds a multi-layer navigable small-world graph. M controls how many neighbor links each node keeps; higher M produces a richer graph with better recall and faster convergence but uses more memory and slows index building. efConstruction sets how many candidates are explored when inserting each node at build time; higher values build a higher-quality graph improving achievable recall, again at the cost of longer build time. efSearch, the key runtime parameter, sets the size of the dynamic candidate list during a query; raising it explores more of the graph, increasing recall but directly increasing query latency.

How to tune

Fix M and efConstruction at build time to set the quality ceiling and memory footprint, larger for high-recall needs. Then, at query time, choose efSearch by sweeping it and measuring the recall-latency curve on a representative dataset, picking the smallest efSearch that meets your recall target within your latency budget. For a latency-critical feature, accept lower efSearch and recall; for an accuracy-critical batch job, raise efSearch.

What usually comes next

How do you measure recall. Why does higher M cost memory. How does IVF or product quantization compare. How do filters interact with HNSW.

A concrete example

For an interactive image-similarity feature with a 50 ms budget, you build with moderate M and efConstruction, then sweep efSearch: efSearch 64 gives 95 percent recall at 20 ms, efSearch 256 gives 99 percent at 70 ms. You pick efSearch around 100 for about 97 percent recall within budget.

Interview question

To raise recall on an existing HNSW index without rebuilding it, accepting higher query latency, which parameter do you increase?

  • a.M, the number of links per node
  • b.efSearch, the runtime candidate-list sizeCorrect
  • c.efConstruction, the build-time search breadth
  • d.The vector dimensionality
Why?

efSearch is the runtime dial: increasing it explores more of the graph, raising recall at the cost of latency, with no rebuild. M and efConstruction are build-time parameters that require reindexing.

Just read this? Test yourself on what you have been reading.

Read the original → zilliz.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles