How to build a simple recommendation system?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
A recommendation system suggests items a user is likely to care about, such as movies, books, or products. You do not need a massive production stack to learn the core idea; a small user-item matrix and a similarity measure are enough to build a useful first version.
Pick a Simple Strategy First
There are many recommender approaches, but for a beginner-friendly implementation, item-based collaborative filtering is a good starting point. The basic idea is:
- Collect user ratings
- Represent them as a matrix
- Measure similarity between items
- Recommend items similar to what the user already liked
This avoids training a heavy model and makes the logic easy to inspect.
Prepare the Ratings Data
Suppose you have a table with user_id, item, and rating.
This produces a user-item matrix where each row is a user and each column is an item.
Compute Item Similarity
Once you have the matrix, compute similarity between item columns. Cosine similarity is common because it compares rating patterns rather than raw magnitude.
If Book A and Book C have similar rating patterns across users, their similarity score will be high.
Generate Recommendations for One User
Now recommend items for a target user by looking at what they rated highly and pulling similar items they have not rated yet.
This is a deliberately small implementation, but it demonstrates the core loop used by larger systems: infer preference from similar behavior.
Improve the Baseline
A simple recommender can be useful, but real data is messy. Once the baseline works, typical improvements include:
- Ignore very unpopular items with too little data
- Normalize ratings so generous and strict users are more comparable
- Add content features such as genre, category, or tags
- Separate training data from evaluation data
If you later outgrow pure collaborative filtering, you can move to matrix factorization or hybrid recommenders. The important part is to start with something you can validate.
Evaluating Whether It Works
Do not judge a recommendation system only by whether the code runs. You need an evaluation plan. A common approach is to hide one known user interaction and see whether the system recommends that item back.
For explicit ratings, you can also compute ranking metrics or prediction error. Even a simple offline check is far better than guessing.
In production, you would also measure click-through rate, conversion rate, or watch time depending on the product.
Common Pitfalls
The most common mistake is treating missing ratings as genuine dislike. In most datasets, a missing value means the user never interacted with the item, not that they rejected it.
Another mistake is overfitting to tiny data. If only two users rated an item, a high similarity score may be misleading. Add minimum-support rules before trusting the output.
A third mistake is skipping evaluation. Recommendation systems can produce plausible-looking results that are actually weak. Always test with held-out data or online metrics.
Summary
- A simple recommendation system can be built from a user-item ratings table and cosine similarity.
- Item-based collaborative filtering is easy to understand and implement.
- Start with a pivoted ratings matrix, compute similarities, then rank unseen items.
- Missing ratings should be treated carefully because they are not always negative feedback.
- Validate the system with held-out data before trusting the recommendations.

