InterviewHubPRO
Section Overview
Q4

Question 4 of 50

Part 1: RAG Architecture, Chunking & Vector Search

8%
AI Engineering & LLMs (50 Qs)•Part 1: RAG Architecture, Chunking & Vector Search

Vector Database Indexing Internals: HNSW vs. IVF-PQ Trade-Offs

Exact k-Nearest Neighbors ($k$-NN) computes distance against every vector in the database, yielding $O(N \cdot D)$ complexity. For millions of vectors, exact search introduces hundreds of milliseconds of latency. Production vector databases use Approximate Nearest Neighbor (ANN) indexing:

scss
                        ┌───────────────────────────────────────────┐
                        │        ANN Indexing Architectural Trade   │
                        └─────────────────────┬─────────────────────┘
                                              │
                     ┌────────────────────────┴────────────────────────┐
                     ▼                                                 ▼
        ┌─────────────────────────┐                       ┌─────────────────────────┐
        │          HNSW           │                       │         IVF-PQ          │
        │ Hierarchical Navigable  │                       │ Inverted File with      │
        │      Small World        │                       │ Product Quantization    │
        └────────────┬────────────┘                       └────────────┬────────────┘
                     │                                                 │
        • Multi-layer graph skip-list                     • Voronoi cluster partitions
        • Top-tier recall (95–99%)                        • 80–90% RAM reduction (compression)
        • Heavy RAM memory usage                          • Lower recall, higher build time

2. Comprehensive Index Comparison

FeatureFlat (Exact k-NN)HNSW (Hierarchical Graph)IVF-PQ (Quantized Inverted File)
Search Complexity$O(N)$$O(\log N)$$O(\text{nprobe} \cdot \frac{N}{K})$
Recall / Accuracy100% (Ground Truth)95% – 99% (State-of-the-art)80% – 92%
Query LatencyVery High (100–1000ms)Ultra Low (< 5ms)Low (< 15ms)
RAM ConsumptionHigh (raw vectors)Very High (raw vectors + graph edges)Extremely Low (8x to 32x compressed)
Index Build SpeedInstant (no index)Slow ($O(N \log N)$ graph construction)Medium (K-Means training + centroid assignment)
Dynamic UpdatesInstantSupported (node insertions)Expensive (requires periodic retraining of centroids)

3. Production Recommendation Rule

  • Choose HNSW for systems with < 10 million vectors where recall accuracy and sub-10ms query latency are paramount and RAM budget is flexible.
  • Choose IVF-PQ for large-scale enterprise deployments (> 50 million vectors) where fitting the entire vector index into server RAM without exceeding cloud infrastructure budgets is the primary constraint.