Vector DB Deep Dive
High-Scale Retrieval. Master HNSW Graph Indexing, Product Quantization (PQ), platform selection (Pinecone, Qdrant, Weaviate, Milvus, pgvector), and the math of trillion-vector search.
High-Scale Retrieval. Master HNSW Graph Indexing, Product Quantization (PQ), platform selection (Pinecone, Qdrant, Weaviate, Milvus, pgvector), and the math of trillion-vector search. This hands-on tutorial focuses on practical implementation of vector db deep dive concepts.
Vector DB Deep Dive
A production AI system might index billions of documents. If you calculated the distance for every document manually, a single search would take minutes. In this chapter, we explore the algorithms that make sub-second search possible at massive scale β and the platforms you'll use to deploy them.
1. HNSW: The Graph that Scales πΈοΈ
HNSW (Hierarchical Navigable Small World) is the industry standard for fast vector retrieval. It organizes vectors into a multi-layer graph.
- Top Layers: Sparse graphs with few nodes. Fast "jumps" across the dataset.
- Bottom Layers: Dense graphs with many nodes. High-precision "local" searches.
How it works: You start at the top, find the closest "neighbor," move down a layer, and repeat until you find the exact vector. This is much faster than checking every point!
Key HNSW Parameters:
ef_construction: Search breadth during index building. Higher = better recall but slower build.M: Number of connections per node. Higher = better recall, more memory.ef_search: Search breadth at query time. Controls the speed/accuracy tradeoff.
[!TIP] In production, start with
M=16andef_construction=128. Then tuneef_searchat query time to hit your latency SLA.
2. Compressing Data: Product Quantization (PQ) π¦
Vector embeddings are large (e.g., 1536 dimensions). Storing a billion of them would require Terabytes of expensive RAM. Product Quantization (PQ) shrinks them by 90%+.
- Split: Break a long vector into smaller "chunks" (sub-vectors).
- Cluster: For each chunk, run k-means to build a "codebook" of ~256 centroids.
- Encode: Replace each chunk with the index of its nearest centroid (just 1 byte!).
- Reconstruct: Reassemble using only the small index numbers.
[!NOTE] PQ slightly reduces "Recall" (accuracy) but allows you to fit 10x more data on the same hardware. IVF+PQ (clustering + quantization) is the standard for billion-scale datasets.
Quantization Variants Comparison
| Method | Compression | Recall Loss | Best For |
|---|---|---|---|
| Flat (No Compression) | 1x | 0% | Small datasets (<1M) |
| Scalar Quantization (SQ8) | 4x | <1% | Balanced memory/accuracy |
| Product Quantization (PQ) | 8-32x | 2-5% | 100M+ vectors, RAM-constrained |
| Binary Quantization (BQ) | 32x | 5-10% | Extreme scale, OpenAI embeddings work well |
3. Filtering: Pre-filter vs. Post-filter π
What if you want to find "Python experts (vector)" but only in "New York (metadata)"?
- Post-filtering: Search 100 vectors, then delete those not in NY. (Bad: You might end up with 0 results if the top 100 were all in London).
- Pre-filtering: Only search in the NY bucket. (Modern Vector DBs use Metadata Filtering which indexes both vectors and keywords).
- ACORN Filter (Qdrant/Weaviate): A newer approach that interleaves filtering with graph traversal, achieving near-100% recall even with highly selective filters.
4. Algorithm Performance Comparison
| Algorithm | Speed | Memory Usage | Accuracy (Recall) |
|---|---|---|---|
| Flat (Brute Force) | Very Slow | High | 100% |
| IVF (Clustering) | Fast | Medium | 90-95% |
| HNSW (Graph) | Ultra-Fast | High (RAM) | 98-99% |
| DiskANN (Disk-based) | Fast (SSD) | Very Low (RAM) | 95-98% |
5. The Vector Database Ecosystem π
Understanding the algorithms is step one. Now let's look at the actual platforms you'll deploy in production.
| Database | Type | Best For | Unique Strength |
|---|---|---|---|
| Pinecone | Managed SaaS | Production RAG, fast startup | Zero-ops, native hybrid search |
| Qdrant | Open-source / Cloud | Complex filters, self-hosted | Payload filtering, ACORN, Rust performance |
| Weaviate | Open-source / Cloud | GraphQL queries, multi-modal | Built-in vectorization modules |
| Milvus / Zilliz | Open-source / Cloud | Billion-scale enterprise | GPU acceleration, DiskANN support |
| pgvector | PostgreSQL Extension | Existing Postgres users | No new infrastructure, SQL joins with vectors |
| ChromaDB | Open-source | Prototyping, local dev | Zero config, embedded mode |
| FAISS | Library (Meta) | Research, custom pipelines | Maximum flexibility, GPU support |
π Official Documentation
- Pinecone Documentation β Production-ready managed vector DB
- Qdrant Documentation β Open-source with powerful filtering
- Weaviate Documentation β GraphQL-based vector search
- Milvus Documentation β Billion-scale vector search
- pgvector GitHub β Vectors inside PostgreSQL
- ChromaDB Documentation β Simplest for prototyping
- FAISS GitHub β Meta's lower-level library
- ANN Benchmarks β Objective speed/recall comparisons
6. Platform API Code: Working Examples π»
Pinecone β Managed Cloud Vector DB
Qdrant β Open-Source with Rich Filtering
pgvector β Vectors Inside PostgreSQL
7. Choosing the Right Vector Database π―
Key Decision Factors:
- Team has zero ops experience β Pinecone (fully managed, no servers to maintain)
- Complex metadata filters β Qdrant (most powerful filter system)
- Already using PostgreSQL β pgvector (no new infra, ACID compliance)
- Billion-scale + GPU acceleration β Milvus / Zilliz
- Multi-modal (text + images) β Weaviate (built-in vectorizers for multiple modalities)
- Local development / prototyping β ChromaDB (zero config)
- Custom algorithms β FAISS (maximum flexibility, requires your own serving layer)
8. Multi-Tenancy: One Index for Many Customers π’
For SaaS applications, you need to isolate customer data within a shared index.
Interactive Challenge: Vector Compression (PQ)
A simple look at how quantization saves space by mapping to "clusters".
Quiz
Quiz
Question 1 of 4What is HNSW primarily used for?
Key Takeaways
β
HNSW is the most efficient way to search large-scale vector datasets.
β
Product Quantization is required if you have limited RAM and billion-scale data.
β
Metadata Filtering must be pre-filtered for correctness in production.
β
pgvector is the right choice if you already run PostgreSQL.
β
Pinecone is the fastest path to zero-ops production vector search.
β
Qdrant offers the most powerful filtering capabilities for complex queries.
β
Multi-tenancy isolation via metadata filtering is essential for SaaS applications.
What's Next?
Data is retrieved. Now let's orchestrate a team of agents to use it.
Next Chapter: Multi-Agent Orchestration: Graphs, Handoffs, and State.