Back to Tracker
Phase 2 — LLMs & Transformers

Embeddings & Vector Search

The backbone of every RAG system, semantic search, and recommendation engine. The most practically important topic for SDEs building AI products in 2026.

EmbeddingsCosine Similarity ANN SearchHNSW Pinecone / Qdrant / pgvectorHybrid Search
What Are Embeddings? CRITICAL

An embedding is a vector of floating-point numbers (typically 384–3072 dimensions) that represents the semantic meaning of a piece of text.

The key property: semantically similar text maps to vectors that are close together in the vector space.

embedding("king") - embedding("man") + embedding("woman") ≈ embedding("queen") embedding("dog") ≈ embedding("puppy") embedding("dog") ≠ embedding("database")
Intuition: imagine a 768-dimensional space where every concept has a location. Similar concepts cluster together. Embeddings are the coordinates.

What Embeddings Enable

  • 🔍Semantic search: find results by meaning, not keywords
  • 📚RAG retrieval: find the most relevant document chunks for a query
  • 🎯Recommendations: find items similar to what a user liked
  • 🔄Deduplication: find near-duplicate content at scale
  • 🏷️Classification: cluster by similarity without labeled data
Embedding Models — Which to Use
ModelDimensionsCostBest For
OpenAI text-embedding-3-small1536$0.02 / 1M tokensGood balance of cost and quality. Default choice for most applications.
OpenAI text-embedding-3-large3072$0.13 / 1M tokensHighest quality. Use when retrieval accuracy matters most.
Google text-embedding-004768Free (with Gemini API)Excellent free option. Required for Firestore native vector search.
sentence-transformers/all-MiniLM-L6-v2384Free (local)Fastest, smallest, runs on CPU. For prototyping or cost-sensitive setups.
BAAI/bge-large-en-v1.51024Free (local)Best open-source model. Competitive with OpenAI on MTEB benchmarks.
Interview answer for "which embedding model would you use?": "For a Firebase/GCP project I'd use text-embedding-004 — free and native to Firestore vector search. For a general production system, text-embedding-3-small for cost efficiency, or large if we have a precision budget. For local/offline: BAAI/bge-large."
Similarity Metrics — When to Use What CRITICAL
Cosine Similarity
cos(A,B) = dot(A,B) / (|A|×|B|)

Measures the angle between vectors. Range: −1 (opposite) to 1 (identical direction).

Ignores magnitude — only direction matters.

✓ Default for text embeddings. Most APIs return normalized vectors → cosine sim = dot product.
Dot Product
dot(A,B) = Σ aᵢ × bᵢ

For normalized vectors: dot product = cosine similarity. Faster to compute (no division).

Affected by magnitude — if vectors aren't normalized, higher-magnitude vectors dominate.

✓ Use when vectors are pre-normalized (most embedding APIs do this by default).
Euclidean Distance
d(A,B) = √(Σ(aᵢ-bᵢ)²)

Straight-line distance in vector space. Range: 0 (identical) to ∞.

Affected by vector magnitude — less reliable for text embeddings.

Less common for text. Better for image embeddings or when absolute distance matters.
# Python: cosine similarity (NumPy) import numpy as np def cosine_sim(a, b): a, b = np.array(a), np.array(b) return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b)) # For normalized vectors (most APIs normalize by default): def cosine_sim_normalized(a, b): return np.dot(a, b) # same result, faster
Why Brute Force Fails at Scale & How HNSW Solves It CRITICAL

The Problem: Brute-Force KNN

You have 1 million documents, each embedded as a 768-dim vector. A user asks a question. You need the top-5 most similar documents.

Brute force: compare query to every one of 1M vectors.
Cost: O(n × d) = O(1M × 768) = 768M multiply-adds per query.
At 1000 queries/second: 768 billion operations/second — impractical.

Approximate Nearest Neighbor (ANN)

Idea: trade a tiny bit of accuracy (miss ~2-5% of true nearest neighbors) for a massive speedup.

ANN algorithms return results that are "approximately" nearest neighbors — fast enough for production, accurate enough to be useful.

HNSW — How It Works

Hierarchical Navigable Small World graph:

  1. Build a multi-layer graph of vectors at insertion time
  2. Top layers: sparse graph with long-range connections (fast navigation, like a highway)
  3. Bottom layers: dense graph with local connections (precise search, like city streets)
  4. Query: start at the top layer, greedily navigate toward the query vector, zoom in at each lower layer
Brute force: O(n × d) HNSW query: O(log n × d) At n=1M: 20 steps instead of 1M
HNSW is the default index in Qdrant, Weaviate, Chroma, and FAISS. Pinecone uses a proprietary variant. Firestore uses Google's internal ANN algorithm.
Vector Database Decision Framework CRITICAL
DatabaseTypeBest ForKey Characteristic
Pinecone Managed SaaS Getting started fast, no infra to manage Fully managed, easiest to set up, closed source. Good free tier.
Qdrant Open source (Rust) Self-hosted production systems Very fast, excellent metadata filtering, Docker or cloud. Best OSS option.
Weaviate Open source Hybrid search (keyword + vector) needed BM25 + vector hybrid built-in. Good for production with mixed search needs.
Chroma Open source (Python) Prototyping, local development, learning Simplest API, in-memory or persistent, zero infra. Not for high-scale production.
pgvector PostgreSQL extension Already using PostgreSQL, don't want another service Add vector column to existing Postgres table. Limited scale vs dedicated vector DBs.
Firestore Vector Managed (Google) Firebase/GCP projects with Gemini embeddings Native KNN search in Firestore (launched late 2024). Zero extra infrastructure. Best for Firebase apps.

How to Answer in an Interview

"We already use Firebase"

→ Firestore native vector search. Zero extra infrastructure, uses Gemini embeddings natively.

"We already use PostgreSQL"

→ pgvector. Add vector column to existing tables. No new service to operate.

"We want managed, no ops"

→ Pinecone. Best developer experience, production-ready immediately.

"We want open source in production"

→ Qdrant. Best performance, excellent filtering, active community.

Hybrid Search: BM25 + Vector HIGH

Why Neither Alone Is Enough

Pure vector searchGreat for semantic understanding ("what does this mean?"). Misses exact product names, codes, IDs.
Pure keyword (BM25)Great for exact terms and proper nouns. Misses synonyms and conceptual matches.
Hybrid (both)Better than either alone. Handles semantic + exact match in one query.

Real-world Example

Query: "iPhone 15 Pro Max camera"

  • Keyword search: finds "iPhone 15 Pro Max" exactly ✓
  • Vector search: understands "camera" in photography context ✓
  • Hybrid: gets the product right AND understands the intent ✓✓

BM25 Formula (know conceptually)

BM25(q,d) = Σ IDF(qᵢ) × f(qᵢ,d)×(k+1) / (f(qᵢ,d) + k×(1-b+b×|d|/avgdl)) k=1.5, b=0.75 (standard defaults) f = term frequency in document IDF = log((N-n+0.5)/(n+0.5)) N = total docs, n = docs containing term
Don't memorize BPE formula for interviews. Know the concept: BM25 scores documents by term frequency (how often the query term appears) weighted by inverse document frequency (rare terms get higher weight). It's the gold standard keyword retrieval algorithm.
Weaviate has hybrid search built-in. For other vector DBs, run BM25 and vector search separately, then combine scores using Reciprocal Rank Fusion (RRF).