Q&A
What are vector databases and embeddings?
Overview
Traditional databases search by exact match or keyword overlap; vector databases search by meaning. By converting content to embeddings and storing them in a vector DB, you can find the most semantically relevant documents for a query - even if they don't share keywords - by measuring geometric distance in embedding space.
graph LR
A["Text: 'How do I reset my password?'"] --> B["Embedding model"]
B --> C["[0.23, -0.87, 0.42, ...]<br/>768-dim vector"]
C --> D["Vector DB: ANN search"]
D --> E["Top-k nearest vectors"]
E --> F["Similar documents returned"]
style D fill:#1e1b4b,color:#fff
What Are Embeddings?
Embedding models (trained transformers) encode text into dense vectors such that:
- "dog" and "puppy" are close in vector space.
- "bank (financial)" and "bank (river)" are in different regions.
- A question and its answer are closer than a question and an unrelated sentence.
Creating Embeddings (Python)
from openai import OpenAI
client = OpenAI()
response = client.embeddings.create(
input="How does retrieval-augmented generation work?",
model="text-embedding-3-small"
)
vector = response.data[0].embedding # list of 1536 floats
Or with a local model:
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("BAAI/bge-large-en-v1.5")
vector = model.encode("How does RAG work?") # numpy array
Similarity Measures
| Metric | Formula | Use |
|---|---|---|
| Cosine similarity | `(A·B)/( | A |
| Dot product | A·B |
Fast; valid when vectors are normalized (= cosine then) |
| Euclidean distance | √Σ(aᵢ−bᵢ)² |
Magnitude matters; less used for text |
For text embeddings, cosine similarity (or dot product after L2 normalization) is standard.
How Vector Databases Work
Searching billions of vectors by brute-force exact nearest neighbour is O(N×d). Vector databases use Approximate Nearest Neighbour (ANN) indexing algorithms to make this fast:
Key ANN Algorithms
- HNSW (Hierarchical Navigable Small World) - graph-based; very fast query, high recall. Used by most modern vector DBs.
- IVF (Inverted File Index) - partition space into clusters; search only nearby clusters. Used by FAISS.
- LSH (Locality-Sensitive Hashing) - probabilistic, hashes similar vectors to the same buckets.
- ScaNN (Google's ANN) - state-of-the-art for exact/approx trade-off.
Popular Vector Databases
| Database | Type | Notes |
|---|---|---|
| Pinecone | Managed SaaS | Easy setup, scalable, popular for RAG |
| Weaviate | Open-source / cloud | Multi-modal, built-in ML |
| Qdrant | Open-source / cloud | Fast, rich filtering |
| Chroma | Open-source (local) | Easy to use, great for prototyping |
| pgvector | PostgreSQL extension | Keep everything in Postgres |
| FAISS | Library (Meta) | In-memory, GPU-accelerated, not a server |
| Milvus | Open-source | Production-grade, Kubernetes-native |
Metadata Filtering
Production search often combines vector similarity with structured filters:
# Retrieve semantically similar results but only from a specific category:
results = vectorstore.similarity_search(
query="password reset",
k=5,
filter={"category": "support", "language": "en"}
)
This hybrid approach (dense retrieval + metadata filter) is more precise than pure semantic search.
Embedding Dimensions and Trade-offs
Larger dimension → more expressive (captures more nuance) but slower to search and store. Common choices: 384 (fast/small), 768 (balanced), 1536–3072 (OpenAI large models). For production, benchmark your specific task to find the accuracy/cost sweet spot.
Why This Matters in Interviews
Vector databases are a core component in every RAG and AI search system. Cover: embeddings capture semantic meaning as dense vectors, cosine similarity measures closeness, ANN algorithms (especially HNSW) make search fast at scale, and the choice between managed (Pinecone) vs self-hosted (Qdrant, Chroma) vs SQL extension (pgvector). Explaining the index-query pipeline and metadata filtering shows production awareness.
Common Pitfalls
- Using different embedding models for indexing vs querying - vectors must be in the same space.
- Not normalizing vectors when using dot-product similarity (must normalize for it to equal cosine).
- Treating ANN as exact search - it's approximate (you can tune recall/speed trade-off).
- Forgetting to chunk documents before embedding - embedding a 100-page document as one vector loses granularity.
- No metadata filtering - returning semantically similar but irrelevant results.
References
- Sentence Transformers: https://www.sbert.net/
- Pinecone vector DB docs: https://docs.pinecone.io/
- FAISS (Meta): https://github.com/facebookresearch/faiss