Design a Star Rating System (like Amazon)

Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2024-07-21

Problem Statement

Designing a Star Rating System (similar to Amazon’s product rating feature) that allows users to rate products, view average ratings, and browse ratings distribution. The system must support scalability, real-time updates, and fraud prevention since it will be used on a large-scale e-commerce platform.



🔶 Functional Requirements


1.Rating Submission
•A user can give a product a rating between 1–5 stars.
•A user can update or delete their rating at any time.
•Prevent duplicate ratings (only one rating per user per product).
2.Rating Aggregation
•Compute and update the average rating of a product in real-time.
•Provide distribution counts (e.g., 5★: 100, 4★: 50, etc.).
•Display total number of ratings.
3.Review Association (optional)
•A rating can optionally have a text review.
•Support upvotes/downvotes on reviews.
4.Browsing & Querying
•Allow sorting products by rating.
•Allow filtering by rating distribution (e.g., show products with avg rating ≥ 4).
5.Fraud Prevention
•Detect & prevent spam ratings (e.g., bots, multiple fake accounts).
•Support weighting ratings (verified buyers weigh more).
6.Scalability
•Must handle millions of products and billions of ratings.
•Ratings should be queryable with low latency.


🔶 Non-Functional Requirements


•Consistency: Updated ratings should reflect quickly (eventual consistency acceptable).
•Availability: The system should be highly available; rating submission should not block due to analytics failures.
•Performance: Aggregated ratings should be retrievable in under 100ms for product pages.
•Storage: Efficiently store billions of ratings (append-only log + aggregated store).
•Security: Ensure ratings cannot be tampered with.


🔶 Constraints


•One user can rate one product only once (updates allowed).
•Hot products (like iPhones, PS5) may receive millions of ratings within hours — system should scale.
•Ratings aggregation should not require scanning the entire dataset each time.


🔶 Example Use Cases


1.User rates a product 5 stars → product’s avg rating updates immediately.
2.A product has 1000 ratings → UI shows avg rating (4.3), rating breakdown (e.g., 5★ = 600, 4★ = 250, etc.).
3.A user updates rating from 3 to 4 stars → old rating replaced in aggregation.
4.Fraud detection flags multiple ratings from the same IP within minutes.


👉 Summary:

"Design a scalable, fault-tolerant Star Rating System that allows users to rate products (1–5 stars), computes aggregated ratings in real-time, provides rating distribution for product pages, handles billions of ratings efficiently, and includes fraud prevention mechanisms."

Solution

Comments