How Recommendation Engines Work

A mathematical study on recommendations: collaborative filtering algorithms, cosine distance, matrix embeddings, and model latency optimizations.
01 // The Problem
Websites that show the same top-selling items to all users fail to convert. Customers expect personalized experiences based on their browsing history. Attempting to calculate these matches on the fly using standard SQL tables is too slow, causing database latency spikes and poor page performance.
02 // The Context
Personalization is a dimensionality reduction challenge. We collect user interactions (clicks, purchases) and product tags, compile them into a sparse matrix, and compress it into dense vectors (embeddings). The similarity between a user vector and a product vector indicates their matching score.
03 // The Solution
We deploy an offline-online hybrid recommendation pipeline. We train a collaborative filtering model using Alternating Least Squares (ALS) to generate user and product embeddings. We store these vectors in a specialized vector database and query nearest-neighbors in under 12ms to serve live, personalized product feeds.
04 // System Architecture
05 // The Implementation
We train the algorithm in Python. We vectorize user purchase grids, compute latent factors, and save them. We load these product vectors into pgvector. When a user requests a page, we fetch their user vector, calculate cosine similarity against all products, and return the top 5 matches.
06 // Key Engineering Lessons
- Scale product vectors to avoid recommending highly popular items to everyone.
- Implement real-time inventory checks. Never recommend out-of-stock items, regardless of their similarity score.
- Use approximate nearest neighbors (ANN) search algorithms to maintain low latency as your product catalog scales.
07 // Technical Code Implementation
import numpy as np
def cosine_similarity(u, v):
# Calculate angular distance between vectors
dot_product = np.dot(u, v)
norm_u = np.linalg.norm(u)
norm_v = np.linalg.norm(v)
return dot_product / (norm_u * norm_v)08 // Developer Q&A
A: The cold start problem occurs when a new user or product has no historical interaction data. We resolve this by recommending popular items or matching early category selections.
A: Collaborative filtering matches users with similar purchase histories, while content filtering recommends items that share attributes (like tags or text similarity).
Build this architecture
Need similar AI integrations, API streaming pipelines, or database architectures configured for your business operations?
START AN ENGINEERING ROADMAP