We use analytics cookies.We use cookies to analyze site traffic and improve your experience.
Loading...
System Design Masterclass · Database Types Storage
Free preview: the first 2 parts of this 7-part lesson.
Already bought the course? Sign in to read the whole lesson.
this lesson is part of
System Design Masterclass
Unlock this lesson and the 751 other paid lessons
$25 once, by PayPal
See the System Design courseread the rest of this lesson in
System Design Masterclass
Unlock this lesson and the 751 other paid lessons
$25 once, by PayPal
See the System Design coursein Database Types Storage
This lesson is part of the full System Design Masterclass. Unlock it along with 769 other lessons for a one-time payment.
One-time payment by card or PayPal
Get Lifetime AccessOne-time payment: UPI, card or netbanking
Get Lifetime Access18 Foundation lessons are free, no payment needed. Try them now
It is easy and good
“It is really nice and good Please join the course”
Every recommendation engine, every semantic search system, every content-based filter, every duplicate detector, and every pipeline has the same operation at its core: given this item, find the most similar items.
Spotify finds songs similar to the one you're playing. Pinterest finds pins visually similar to the one you liked. Stripe detects transactions that look similar to known fraud patterns. Your email client finds messages similar to the one you flagged as spam.
All of these are similarity search problems. The item is represented as a vector (an ), and "similar" means "close in vector space." The database's job is to find the K closest vectors to your query vector, the K-nearest neighbors.
It sounds simple. It's not. The challenge is doing this fast at scale. With 1 billion vectors in 1,536 dimensions, brute-force comparison takes seconds per query. Users expect results in milliseconds. Bridging that gap, from seconds to milliseconds, is what separates a toy demo from a production system.
The choice of distance metric fundamentally changes what "similar" means.
Measures the angle between two vectors. Ignores magnitude, only cares about direction.
cosine_sim(A, B) = (A . B) / (||A|| * ||B||)
Range: -1 (opposite) to 1 (identical direction)
Use when: Comparing text embeddings. A long document and a short document about the same topic should be similar, even though the long document's vector might have larger magnitudes. treats [0.1, 0.2, 0.3] and [0.2, 0.4, 0.6] as identical, same direction, different scale.
Straight-line distance between two points in vector space.
L2(A, B) = sqrt(sum((A_i - B_i)^2))
Range: 0 (identical) to infinity
Use when: The magnitude of the vector carries meaning. For image where brighter/darker images produce different magnitudes, L2 distance captures those differences. Also used for spatial data and clustering.
dot(A, B) = sum(A_i * B_i)
Range: -infinity to infinity
Use when: Vectors are normalized (unit length). In that case, dot product equals cosine similarity but is faster to compute (no division by magnitudes). Many embedding models produce normalized vectors specifically so dot product can be used.
Sum of absolute differences. Less common for embeddings, but used in specific domains.
| Metric | When to Use | Most Common For |
|---|---|---|
| Cosine similarity | Direction matters, magnitude doesn't | Text embeddings |
| Euclidean (L2) | Both direction and magnitude matter | Image embeddings, spatial data |
| Dot product | Vectors are pre-normalized | General purpose (fast) |
| Manhattan (L1) | Sparse vectors, specific domains | Sparse features |
The practical choice: If you're using OpenAI, Cohere, or Sentence Transformer embeddings, cosine similarity (or dot product on normalized vectors) is almost always correct. The model was trained to produce vectors where cosine similarity reflects semantic similarity.