RAG & Retrieval·Question 21 of 100Intermediate

Compare HNSW, IVF, and product quantization - what are the recall/latency/memory tradeoffs?

Type it the way you would explain it to an interviewer. A few sentences is enough; you can't break anything.

Model answer: the part to lead with

They're different tools, and PQ isn't even an index - it's a compression scheme that composes with the other two.

Full worked answer with diagram

They're different tools, and PQ isn't even an index - it's a compression scheme that composes with the other two.

HNSW (graph): best recall-latency frontier for most workloads - 95-99% recall at millisecond latency. Costs: highest memory (all full-precision vectors + graph edges in RAM), slower to build, awkward deletes (tombstones, periodic rebuild). Incremental inserts are natural. Default choice when the corpus fits in RAM.

IVF (inverted file / clustering): k-means the corpus into nlist cells (a small multiple of √N is the usual starting heuristic); at query time probe only the nprobe nearest cells. Simpler and cheaper to build than HNSW, memory-light (just cluster assignments over your vector storage), and cell lists map naturally to disk or object storage - which is why disk-based and serverless vector stores lean on IVF-style partitioning. Weaknesses: needs a training pass (cluster quality decays if data drifts - plan re-clustering), and recall suffers on boundary cases - a query near a cell edge misses neighbours in unprobed cells, so you raise nprobe and pay linearly in latency.

PQ (product quantization): chop each vector into m subvectors, k-means each subspace into 256 centroids, store each subvector as a 1-byte code → a 768-dim float32 vector (3KB) becomes e.g. 96 bytes, a 10-50× compression. Distances are computed on codes via lookup tables - fast and tiny, but lossy: recall drops, so production setups rescore the top candidates with full-precision vectors ("refinement"). The classic large-scale combo is IVF+PQ (FAISS's IVFPQ): partition to prune the search space, quantize to fit a billion vectors in memory. Simpler cousins - scalar int8 quantization (4×) and binary quantization (32×) - are increasingly used with HNSW too.

Decision sketch: ≤1M vectors - brute force. RAM-resident, latency-sensitive, tens of millions - HNSW (optionally quantized). Hundreds of millions to billions, or disk/cost-bound - IVF+PQ with refinement. Always report recall@k vs exact search when tuning; the knobs (M/ef, nlist/nprobe, PQ bits) are all just positions on the same recall-latency-memory triangle.

Worth sketching. Putting all three on one axis of scale shows they are positions on a tradeoff, not competitors, and that PQ hangs off IVF rather than replacing it.

flowchart TD
    N{"Vectors to serve"} -->|"under ~1M"| F["Brute force,<br/>100% recall, filters free"]
    N -->|"tens of millions,<br/>fits in RAM"| H["HNSW"]
    N -->|"hundreds of millions<br/>to billions"| I["IVF partitioning"]
    H --> T["Tune M and ef_search<br/>against exact search"]
    I --> P["Add PQ codes:<br/>3KB per vector to ~96 bytes"]
    P --> R["Rescore top candidates<br/>on full-precision vectors"]

What a good answer covers

  • They're different tools, and PQ isn't even an index - it's a compression scheme that composes with the…
  • HNSW (graph): best recall-latency frontier for most workloads - 95-99% recall at millisecond latency
  • Costs: highest memory (all full-precision vectors + graph edges in RAM), slower to build, awkward…
  • Default choice when the corpus fits in RAM
  • IVF (inverted file / clustering)

Key terms: HNSW · IVF · boundary cases · PQ · lossy · IVF+PQ

Interviewer follow-ups to expect

  1. Why does IVF need retraining as data drifts but HNSW doesn't?

  2. Sketch the memory math for 1B × 768-dim vectors under float32, int8, and PQ-96. When would binary quantization + rescoring beat PQ?

Worked answer from AI-Engineer-Interview-Questions by Om (MIT), reformatted for LLM Prep · answer text last updated 6 November 2026 · scoring and review schedule by LLM Prep