Library

Volume 2 · RAG Architecture

Notebook
Sign in

Section 20

Embedding Models & Vector Databases

601 words · about 3 min

20.1 Embedding Model Comparison

Model Dimensions Max Tokens MTEB Avg Cost Best For
text-embedding-3-large (OpenAI) 3072 8191 ~64.6 $0.13/1M tokens High-quality enterprise default
text-embedding-3-small (OpenAI) 1536 8191 ~62.3 $0.02/1M tokens Cost-effective default
Cohere embed-v3 1024 512 ~64.5 $0.10/1M tokens Multi-language, RAG-optimized
voyage-3 1024 32K ~67.1 $0.06/1M tokens Long docs, code
BGE-large-en-v1.5 1024 512 ~63.9 Free (self-host) Privacy-sensitive, on-prem
E5-mistral-7b-instruct 4096 32768 ~66.6 Free (self-host) Long context, best open-source
Jina Embeddings v3 1024 8192 ~65.5 $0.02/1M tokens Flexible dimensions
Titan Embeddings v2 1024 — Good Managed pricing Cloud-native ecosystem integration
all-MiniLM-L6-v2 384 — Good Free (self-host) Fast, lightweight, edge use

Selection criteria: (1) quality — MTEB scores for your specific task type (retrieval vs. classification vs. clustering); (2) dimensionality — higher = better quality but more storage/compute; (3) context length — must accommodate your chunk sizes; (4) deployment — cloud API vs. self-hosted; (5) cost at volume; (6) multi-language support if relevant.

20.2 Vector Database Comparison

Database Type Filtering Scale Managed Key Feature
Pinecone Cloud-native Metadata filters Billions Yes Serverless, zero-ops scaling
Weaviate Open-source/Cloud GraphQL + filters 100M+ Both Hybrid search built-in
Qdrant Open-source/Cloud Payload filters 100M+ (billions) Both Rust-based, fastest self-hosted
Milvus/Zilliz Open-source/Cloud Attribute filters Billions Both GPU-accelerated
ChromaDB Open-source Metadata filters Millions Self-hosted Simple, prototyping-friendly
pgvector PostgreSQL extension Full SQL Millions N/A Reuse existing PostgreSQL stack
OpenSearch / Elasticsearch (k-NN) Managed/self-hosted Rich Billions Both Native hybrid (keyword+vector) search
Cloud-native Search services (e.g., Azure AI Search) Managed Rich Billions Yes Deep ecosystem integration, hybrid search

20.3 Similarity Search / ANN Algorithms

Algorithm Recall Speed Memory When to Use
Flat (Brute-force/Exact) 100% Slowest Baseline <100K vectors
IVF (Inverted File Index) 95-99% Fast Low 100K-10M vectors
HNSW (Hierarchical Navigable Small World) 95-99% Fastest High Default for production at scale
PQ (Product Quantization) 90-95% Fast Lowest Memory-constrained deployments
IVF-PQ 92-97% Fast Low Large-scale, cost-effective

Section 20 — Interview Q&A

Q: How would you select an embedding model and vector database for a RAG system storing 50M document embeddings at 1000 QPS? A: For the embedding model, I’d benchmark MTEB retrieval-task scores against my own domain’s queries (public benchmarks don’t always transfer), balancing quality against dimensionality — higher dimensions cost more to store and search but the incremental quality gain often isn’t worth it past ~1024-1536 dims for most enterprise retrieval tasks. For the vector database at this scale/throughput, pgvector typically can’t sustain 1000 QPS efficiently at 50M vectors; a purpose-built vector DB (Qdrant self-hosted, or a managed option like Pinecone) is needed. I’d document the trade-off as an ADR: a managed service meets performance needs quickly but costs more monthly and may raise data-residency concerns; a self-hosted option (e.g., Qdrant on Kubernetes) is cheaper at scale and keeps data in-house, but shifts operational burden (upgrades, backups, monitoring) onto the team — the right call depends on whether the org already has the ops capability.

Q: Compare HNSW, IVF, and PQ as ANN indexing strategies. A: HNSW builds a multi-layer graph structure that gives the best recall-speed trade-off and is the default choice for production at meaningful scale, at the cost of high memory usage since it keeps the full graph in memory. IVF clusters the vector space and only searches the nearest clusters (controlled by an nprobe parameter trading recall for speed) — lower memory than HNSW, good for 100K-10M vector ranges. PQ compresses vectors themselves (quantizing sub-vectors), cutting memory 4-8x at some recall cost — used when memory is the binding constraint, often combined with IVF (IVF-PQ) for large-scale, cost-sensitive deployments. Below ~100K vectors, brute-force flat search is often simplest and gives perfect recall since the O(n) cost is still fast enough.