Billion-Scale Vector Search: Why DiskANN is Replacing HNSW for Memory-Constrained Production

Title: Billion-Scale Vector Search: Why DiskANN is Replacing HNSW for Memory-Constrained Production Slug: hnsw-vs-diskann-billion-scale-vector-search Category: AI for Developers MetaDescription: Deep dive into HNSW vs. DiskANN for 1B+ vector scales. Learn the memory trade-offs, Vamana graph mechanics, and production deployment strategies.
If you are trying to scale a vector database to a billion embeddings using HNSW (Hierarchical Navigable Small World), you are likely staring at a cloud bill that makes your CFO wince. I’ve seen teams attempt to shove a billion 768-dimensional vectors into RAM, only to realize they need 3-4 TB of memory just to keep the index alive. In production, RAM is your most expensive resource.
The industry is currently shifting. While HNSW was the gold standard for million-scale search, DiskANN (and its underlying Vamana graph) has become the pragmatic choice for billion-scale deployments where you can't justify spending $20,000 a month on high-memory EC2 instances. This isn't just about saving money; it’s about the structural limits of graph-based indexing when the data no longer fits in the processor's reach.
Quick Summary
- HNSW is unbeatable for low-latency (sub-5ms) requirements but requires the entire index to reside in RAM. At billion-scale, the RAM costs are often prohibitive.
- DiskANN utilizes the Vamana graph and sophisticated compression (PQ) to store the bulk of the index on NVMe SSDs, requiring only a fraction of the RAM (often 1/10th or less) for a manageable latency penalty (10-50ms).
- The Verdict: If you have <10M vectors, stick with HNSW. If you are building Optimizing RAG Pipelines: Hybrid Search and Reranking at a billion-scale, DiskANN is the only way to stay solvent.
The Infrastructure Wall: Why HNSW Fails at Billion-Scale
To understand why we need DiskANN, we have to look at the math of HNSW. HNSW builds a multi-layered graph where each node is a vector and edges connect "neighbors."
For a billion vectors with 768 dimensions (FP32):
- Raw Data: 1,000,000,000 * 768 * 4 bytes = 3.072 TB.
- Graph Overhead: HNSW needs to store adjacency lists. If your $M$ (max connections) is 32, that’s another ~250 GB of pointers.
- Total RAM Required: Roughly 3.4 TB.
In a production environment, you don't just need 3.4 TB; you need redundancy, shards, and buffer room for the OS. You are looking at a cluster of r6id.32xlarge instances or similar. For many of us, that is a non-starter. Even if you use Fine-Tuning Open-Source LLMs for Domain-Specific RAG to get high-quality embeddings, if you can’t retrieve them efficiently, your RAG pipeline stalls.
HNSW: The High-Speed Memory Hog
HNSW is a "Small World" graph. It works by performing a greedy search on the top layer (coarse) and zooming in through layers until it reaches the bottom layer (fine).
The Performance "Gotcha"
The problem is that HNSW’s search path is unpredictable in terms of memory locality. Every hop in the graph is a potential cache miss. When the index is in RAM, this is fine because RAM latency is ~100ns. But the moment the OS starts swapping HNSW to disk because you've run out of memory, performance collapses by orders of magnitude. HNSW was never designed to handle the latency of an SSD.
DiskANN: The SSD-Native Alternative
DiskANN was developed by Microsoft Research to solve exactly this. It relies on a graph structure called Vamana. Unlike HNSW’s hierarchical approach, Vamana is a single-layer graph with a specific property: it has a small diameter and highly optimized edge selection that minimizes the number of disk seeks.
How DiskANN Cheats the Memory Limit
DiskANN uses a three-pronged strategy to stay efficient:
- The Vamana Graph: A robust graph that handles long-range edges better than HNSW, allowing the search to "jump" across the billion-scale manifold with fewer hops.
- Product Quantization (PQ): It keeps a compressed version of the vectors in RAM. When you search, the initial distance calculations happen in memory using these compressed sketches.
- SSD-Resident Full Vectors: The full-resolution vectors and the graph edges live on the NVMe SSD. Only when the search narrows down to the most likely candidates does the system perform a "beam search" that reads the full-precision data from disk.
This allows you to run a billion-scale index on a machine with 64GB of RAM and a fast 4TB NVMe drive.
Technical Comparison: By the Numbers
| Feature | HNSW | DiskANN (Vamana) |
|---|---|---|
| Storage Medium | 100% RAM | SSD (Data) + RAM (Cache) |
| Latency (1B vectors) | 1-5ms | 15-50ms |
| RAM Requirement | ~3.5 TB | ~64-128 GB |
| Indexing Speed | Fast (Parallelizable) | Slow (Heavy SSD I/O) |
| Recall@10 | 95-99% | 95-99% |
| Cost | $$$$$ | $ |
Step-by-Step: Implementing DiskANN (via Milvus or DiskANN Lib)
If you're ready to implement this, you shouldn't write it from scratch. You’ll likely use the DiskANN library directly or a vector DB like Milvus or Weaviate that abstracts it. Here is the conceptual flow for a high-performance DiskANN setup.
1. Data Pre-processing
You must quantize your data. DiskANN relies on PQ to create the in-memory index.
import numpy as np
from diskannpy import build_disk_index
# Assume 'data.bin' is your 1B vector file in float32
# We configure the index for a 1B scale
data_path = "vectors_1B.bin"
index_prefix = "diskann_index"
# Parameters:
# R: Max degree of the graph
# L: Search list size during construction
# B: Memory limit for the buffer (GB)
# M: Memory limit for the search (GB)
build_disk_index(
data_path=data_path,
index_prefix=index_prefix,
distance_metric="l2",
R=32,
L=100,
B=64.0,
M=16.0
)
2. Tuning the Beam Search
The "Beam Width" is the most critical parameter during search. It determines how many nodes the algorithm explores in parallel on the disk.
- Higher Beam Width: Better recall, higher latency (more IOPS).
- Lower Beam Width: Lower latency, lower recall.
In a production environment, you should dynamically adjust beam width based on your AI-Powered Personalization: A Guide for Small E-commerce requirements. For high-stakes queries, bump it up; for "more like this" features, keep it low.
Real-World Gotchas: What They Don't Tell You
1. The SSD Lifecycle Trap
DiskANN is heavy on random reads. If you are using a standard consumer-grade NVMe, you will burn through the TBW (Total Bytes Written) faster than you think, but more importantly, the IOPS will throttle. In production, you must use Enterprise-grade SSDs (like the Samsung PM1733 or AWS i3en instances) that support high sustained IOPS. If your disk latency spikes, your vector search will hang.
2. The Indexing Time Wall
Building an HNSW index is relatively fast. Building a DiskANN index for a billion vectors can take days on a single machine. It is an I/O bound process that involves multiple passes over the data to build the Vamana graph and calculate the PQ clusters. Plan your CI/CD pipeline accordingly; you cannot re-index on the fly.
3. RAM for the OS Page Cache
While DiskANN claims low memory usage, it heavily benefits from the OS page cache. If you give DiskANN 64GB of RAM and the index is 3TB, the OS will try to cache the most frequently accessed graph nodes. If you starve the OS of this "slack" memory by running other heavy processes (like an LLM) on the same box, your DiskANN performance will degrade.
When to Choose Which?
Choose HNSW if:
- Your dataset is < 50 million vectors.
- You have a "money is no object" approach to latency.
- You are performing frequent updates or deletions (HNSW handles dynamic updates better than DiskANN).
- You are building real-time trading systems where 10ms is too slow.
Choose DiskANN if:
- You are hitting the billion-scale mark.
- Your infrastructure budget is a primary constraint.
- You are using RAG with Vector Databases for Real-Time Financial Sentiment and can tolerate 30ms of latency for the retrieval step.
- The dataset is relatively static (bulk loads rather than constant streaming updates).
Optimizing DiskANN for Production
To get the most out of DiskANN, don't just use the default settings. You need to align your vector dimensions with your disk's sector size.
- Vector Dimensionality: If you are using 1536-dim vectors (OpenAI), consider using a bottleneck layer or PCA to reduce them to 768 or 512 before indexing. The decrease in accuracy is often negligible compared to the massive gains in IOPS efficiency.
- Asynchronous Prefetching: If your implementation allows it, use asynchronous I/O to fetch candidates from disk. This allows the CPU to calculate distances for one "beam" while the disk is seeking the next.
Scaling Test-Time Compute
As we look at Scaling Test-Time Compute: Boosting LLM Reasoning Accuracy, the retrieval step becomes a bottleneck. If your vector search takes 100ms, your total reasoning loop might exceed 2 seconds. DiskANN needs to be tuned specifically to the hardware it's running on. I recommend benchmarking L (search list size) and BeamWidth on your actual production hardware before locking in your SLA.
Next Steps for Engineers
If you are moving from a prototype to a billion-scale production system:
- Audit your RAM usage: If you're spending more than $2k/month on RAM alone, start a DiskANN PoC.
- Benchmark IOPS: Use
fioto test your NVMe's random read performance. You need at least 100k-200k IOPS for decent DiskANN performance at scale. - Evaluate Managed Services: Some managed vector databases (like Milvus/Zilliz or Pinecone's s2 pods) handle the HNSW vs. DiskANN switch under the hood. Check if they allow you to specify disk-optimized index types.
Scaling to a billion is a hardware problem as much as it is an algorithmic one. Don't let HNSW's simplicity trick you into a massive cloud bill when DiskANN is sitting there ready to do the heavy lifting on a budget.
Practical FAQ
1. Can I update a DiskANN index in real-time?
Technically, yes, but it’s expensive. DiskANN is optimized for static or append-heavy workloads. Frequent deletions require "marking" nodes and periodic re-compacting of the graph, which is much more taxing on an SSD than it is in RAM with HNSW. If your data changes every second, stick to HNSW or a hybrid approach.
2. How does Product Quantization (PQ) affect recall in DiskANN?
PQ is a lossy compression. In DiskANN, the PQ-compressed vectors are only used to find the candidates. Once candidates are identified, DiskANN fetches the full-precision vectors from the disk to compute the final distance. This means DiskANN can achieve much higher recall than a purely in-memory PQ-based index because the final ranking is done on the original data.
3. Is NVMe required, or will a standard SSD/HDD work?
Do not attempt DiskANN on a standard SATA SSD or, heaven forbid, a spinning HDD. The algorithm relies on the massive parallel random-read capability of NVMe (using io_uring on Linux). On an HDD, the seek time would result in query latencies measured in seconds, not milliseconds.
4. What is the "Vamana" graph, and why is it better than the HNSW graph?
Vamana is a single-layer graph that uses a "relaxed" neighbor selection rule. This results in a graph with a smaller diameter and more "long-range" edges than a single layer of HNSW. Because every "hop" in a disk-based graph costs an I/O operation, minimizing the number of hops to reach the target neighborhood is more important than the hierarchical structure HNSW uses.
Gulshan Sharma
AI/ML Engineer, Full-Stack Developer
AI engineer and technical writer passionate about making artificial intelligence accessible. Building tools and sharing knowledge at the intersection of ML engineering and practical software development.
Continue Reading

Architecting Multi-Modal RAG Systems for Forensic Analysis
Learn to build multi-modal RAG systems for real-time audio-visual forensic analysis. A technical guide for developers on processing evidence with AI.
5 min read
Building AI Companions with Long-Term Memory
Learn how to build persistent AI companions with long-term episodic memory using vector databases. A practical guide for developers.
7 min read
On-Device RAG: Privacy-Preserving AI for Local Data
Learn how to build secure, private, on-device RAG systems using local vector databases. Protect your data without sacrificing AI performance.
6 min read