Skip to main content
System Designhard•DECEMBER 2026•18 MIN

Design a Billion-Scale Vector Search Index (HNSW + Sharding)

Approximate nearest neighbors with HNSW graph layers, quantization, scatter-gather sharding, and recall vs latency budgets.

graph traversalscatter-gatherquantizationhybrid searchml-systemsretrievalvector-db

1. Clarifying questions & SLO contract

  • Corpus scale: 1 billion vectors, D = 768 dimensions (float32), 20K QPS read, 2K/sec streaming upserts/deletes.
  • SLO: p99 ≤ 25 ms, Recall@10 ≥ 0.95.
  • Filtering: Metadata filters (e.g. tenant_id, lang = 'en') combined with vector similarity.

2. Back-of-envelope sizing

  • Raw float32 vectors: 10^9 × 768 × 4 bytes ≈ 3.07 TB RAM just for vectors!
  • HNSW graph links (M = 16): ~32 edges × 4 bytes × 1.3 layer overhead ≈ 166 GB.
  • With SQ8 / PQ compression (4×–8×): float32 → int8 SQ8 drops vector RAM to ~768 GB (or ~192 GB with PQ16), fitting comfortably across 16–32 memory-optimized nodes (64 GB RAM each) with asymmetric re-scoring of top-100 candidates from NVMe.

3. Interactive HNSW multi-layer search simulator

Step through Layer 2 → Layer 1 → Layer 0 and compare efSearch = 1 (trapped in local minimum #7) against efSearch = 2 (beam crosses bridge to true nearest neighbor #11):

LOADING HNSW GRAPH SEARCH SIM...

4. Deep dive: sharding, deletes & metadata filtering

  1. Scatter-gather vs IVF-routed sharding: Partition by hash(doc_id) % S for uniform write/storage balance (S = 16 shards). Queries scatter to all S shards in parallel (p99 = slowest shard, hedged requests at p95) and merge top-k results. For multi-tenant workloads, route by tenant_id so queries touch only 1 shard.
  2. Soft deletes + background compaction: Removing nodes from HNSW breaks graph navigability. Mark deleted IDs in a Roaring Bitmap checked during traversal, and rebuild/compact segments asynchronously when tombstones exceed 15%.
  3. Pre-filter vs post-filter: Post-filtering (top-k then filter) returns empty results on selective filters (1% match). Use inline filtered HNSW (check predicate bitmap during beam expansion) or fallback to exact scan when filter selectivity is <1%.

5. Failure modes & trade-offs

TensionTrade-off & Mitigation
Recall vs latency (efSearch)Higher efSearch widens Layer-0 beam linearly; tune per query tier (ef=64 default, ef=128 high-precision).
Quantization recall lossSQ8/PQ loses fine ordering; oversample 4× (top-400) in memory and re-score with exact float32 vectors from SSD.
Index build memory spikeBuild immutable segments offline or on dedicated indexer workers and ship snapshots to read replicas.

Related Lab Entries