#🧱 Building Block: Vector Database (Milvus / Pinecone / pgvector)
#Overview
A Vector Database is a specialized data store designed to index, store, and query high-dimensional vectors (embeddings). It powers similarity search for recommendation engines, semantic search, image retrieval, and RAG (Retrieval-Augmented Generation) for LLMs.
#Key Concepts
- Embeddings: Dense numerical vectors (128–2048 dimensions) produced by ML models that capture semantic meaning of items, users, text, or images.
- ANN (Approximate Nearest Neighbor): Algorithms that find the K most similar vectors without brute-force comparison. Trades exactness for massive speed gains.
- HNSW (Hierarchical Navigable Small World): Graph-based ANN index with best recall/speed trade-off. Builds a multi-layer navigable graph.
- IVF (Inverted File Index): Partitions vector space into clusters; searches only relevant clusters. Uses less memory than HNSW.
- Product Quantization (PQ): Compresses vectors to reduce memory usage at the cost of some accuracy.
- Distance Metrics: Cosine similarity (most common for embeddings), Euclidean (L2), Inner Product (dot product).
#Popular Implementations
| Database | Type | Strengths |
|---|---|---|
| Milvus | Standalone, open-source | Best for large-scale (billions of vectors), GPU support |
| Pinecone | Managed SaaS | Easiest to operate, good for startups |
| pgvector | PostgreSQL extension | Good for < 10M vectors, no extra infra |
| Weaviate | Standalone, open-source | Built-in ML model integration |
| Qdrant | Standalone, open-source | Rust-based, excellent filtering support |
#When to Use
- Recommendation Engines: User/item embeddings → find similar items via ANN search.
- Semantic Search: Convert text queries to embeddings → find similar documents.
- Image/Video Retrieval: Find visually similar content.
- RAG for LLMs: Retrieve relevant context from a knowledge base for grounded generation.
#When NOT to Use
- Exact keyword matching (use Elasticsearch).
- Structured data queries with joins (use PostgreSQL).
- Small datasets < 100K vectors (brute-force cosine sim is fast enough).
#Key Performance Numbers
HNSW on 100M vectors (256-dim):
Query latency: < 5ms (top-200 neighbors)
Memory: ~40GB (vectors + graph overhead)
Recall@200: 95%+
Build time: ~2 hours#Trade-offs
- Pros: Sub-10ms similarity search over billions of vectors, essential for modern ML-powered applications.
- Cons: Memory-intensive (HNSW), requires embedding model management, index rebuild on model version changes.
#Used In This Repo
- Recommendation Engine (#23) — User and item embedding storage + ANN candidate generation