Bird
Raised Fist0
LLDsystem_design~7 mins

Rating and review system in LLD - System Design Guide

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Problem Statement
When users submit ratings and reviews, storing and retrieving them inefficiently can cause slow page loads and inconsistent data. Without proper design, the system may allow duplicate reviews, lose data integrity, or fail to scale as user numbers grow.
Solution
The system uses a structured approach to store each user's rating and review linked to the item. It enforces one review per user per item and supports updating or deleting reviews. Reviews are stored in a database with indexes for fast retrieval. The system aggregates ratings to show average scores efficiently.
Architecture
┌───────────────┐       ┌───────────────┐       ┌───────────────┐
│   User UI     │──────▶│ Review Service│──────▶│  Database     │
└───────────────┘       └───────────────┘       └───────────────┘
         │                      │                      ▲
         │                      │                      │
         │                      │                      │
         └──────────────────────┴──────────────────────┘
                          Aggregated Ratings Cache

This diagram shows users submitting reviews through the UI, which the Review Service processes and stores in the database. An aggregated ratings cache speeds up average rating retrieval.

Trade-offs
✓ Pros
Ensures data integrity by enforcing one review per user per item.
Supports updating and deleting reviews to keep data current.
Indexes enable fast retrieval of reviews and ratings.
Aggregated ratings cache improves performance for read-heavy operations.
✗ Cons
Maintaining cache consistency adds complexity.
Handling concurrent updates requires careful locking or transactions.
Storing large volumes of reviews can increase storage costs.
Use when your platform has thousands of users submitting reviews and you need fast read access to aggregated ratings and individual reviews.
Avoid if your system has very low review volume (under 100 reviews) where simple storage without caching is sufficient.
Real World Examples
Amazon
Amazon uses a rating and review system to allow customers to rate products and write reviews, helping other shoppers make informed decisions.
Airbnb
Airbnb collects ratings and reviews from guests and hosts to build trust and improve service quality.
Uber
Uber uses ratings and reviews to monitor driver and rider behavior and maintain service standards.
Code Example
The before code allows multiple reviews from the same user for the same item, causing duplicates. The after code uses a dictionary keyed by user and item to ensure only one review exists per user-item pair, allowing updates to overwrite previous reviews.
LLD
### Before: No enforcement of single review per user per item
class Review:
    def __init__(self, user_id, item_id, rating, comment):
        self.user_id = user_id
        self.item_id = item_id
        self.rating = rating
        self.comment = comment

reviews = []

def add_review(review):
    reviews.append(review)


### After: Enforce one review per user per item with update support
class Review:
    def __init__(self, user_id, item_id, rating, comment):
        self.user_id = user_id
        self.item_id = item_id
        self.rating = rating
        self.comment = comment

reviews = {}

# Key: (user_id, item_id)
def add_or_update_review(review):
    key = (review.user_id, review.item_id)
    reviews[key] = review

# Example usage
r1 = Review('user1', 'item1', 5, 'Great product!')
add_or_update_review(r1)
r2 = Review('user1', 'item1', 4, 'Changed my mind, still good.')
add_or_update_review(r2)

print(reviews[("user1", "item1")].rating)  # Outputs: 4
OutputSuccess
Alternatives
Event-driven review processing
Reviews are submitted as events and processed asynchronously to update storage and aggregates.
Use when: Choose when write latency can be relaxed and you want to decouple review submission from processing.
NoSQL document store
Stores reviews as documents without strict schema, allowing flexible review formats.
Use when: Choose when review data varies widely or schema changes frequently.
Summary
A rating and review system must prevent duplicate reviews from the same user on the same item to maintain data integrity.
Using indexes and caching aggregated ratings improves performance for read-heavy workloads.
Supporting review updates and deletes keeps the system flexible and user-friendly.

Practice

(1/5)
1. What is the primary purpose of a rating and review system in an online store?
easy
A. To process payment transactions
B. To collect user feedback and calculate average product ratings
C. To manage product inventory levels
D. To store user passwords securely

Solution

  1. Step 1: Understand the system's goal

    A rating and review system is designed to gather user opinions and ratings about products.
  2. Step 2: Identify the main function

    It calculates average ratings to help other users make decisions quickly.
  3. Final Answer:

    To collect user feedback and calculate average product ratings -> Option B
  4. Quick Check:

    Rating system = Collect feedback + average rating [OK]
Hint: Focus on feedback and rating calculation [OK]
Common Mistakes:
  • Confusing rating system with payment or inventory systems
  • Thinking it manages user credentials
  • Assuming it handles shipping or delivery
2. Which data structure is best suited to store individual reviews for quick lookup by product ID?
easy
A. Hash map with product ID as key and list of reviews as value
B. Array of reviews without indexing
C. Linked list of all reviews
D. Stack of reviews

Solution

  1. Step 1: Consider lookup efficiency

    Quick lookup by product ID requires a data structure with fast key-based access.
  2. Step 2: Choose appropriate structure

    A hash map (dictionary) allows O(1) average time to find reviews by product ID.
  3. Final Answer:

    Hash map with product ID as key and list of reviews as value -> Option A
  4. Quick Check:

    Fast lookup = Hash map [OK]
Hint: Use hash maps for fast key-based lookup [OK]
Common Mistakes:
  • Using arrays without indexing causes slow searches
  • Linked lists have O(n) lookup time
  • Stacks do not support direct lookup by key
3. Given the following pseudocode for updating average rating after a new review:
current_avg = 4.0
num_reviews = 5
new_rating = 5
new_avg = (current_avg * num_reviews + new_rating) / (num_reviews + 1)

What is the value of new_avg?
medium
A. 4.17
B. 4.16
C. 4.0
D. 4.5

Solution

  1. Step 1: Calculate total rating sum before new review

    Total sum = current_avg * num_reviews = 4.0 * 5 = 20
  2. Step 2: Add new rating and compute new average

    New sum = 20 + 5 = 25
    New average = 25 / (5 + 1) = 25 / 6 ≈ 4.1667
  3. Final Answer:

    4.17 -> Option A
  4. Quick Check:

    Average update formula ≈ 4.17 [OK]
Hint: Multiply avg by count, add new, divide by count+1 [OK]
Common Mistakes:
  • Forgetting to add new rating to total sum
  • Dividing by old count instead of count+1
  • Rounding too early causing wrong average
4. A rating system stores average rating and count per product. After deleting a review, the average becomes incorrect. What is the likely cause?
medium
A. Recalculating average using sum of all reviews
B. Using integer division instead of float division
C. Not updating the count of reviews after deletion
D. Storing reviews in a hash map

Solution

  1. Step 1: Understand average calculation

    Average = sum of ratings / count of reviews. Both must be accurate.
  2. Step 2: Identify deletion impact

    If count is not decreased after deleting a review, average calculation divides by wrong count.
  3. Final Answer:

    Not updating the count of reviews after deletion -> Option C
  4. Quick Check:

    Count mismatch causes wrong average [OK]
Hint: Always update count when reviews change [OK]
Common Mistakes:
  • Ignoring count update after deletion
  • Assuming recalculation always fixes average
  • Confusing data structure choice with calculation error
5. You want to design a scalable rating and review system for millions of products and users. Which approach best balances fast average rating queries and frequent review updates?
hard
A. Store all reviews and compute average on each query
B. Use a single database table without indexes
C. Cache only the latest review per product
D. Maintain precomputed average and count, update incrementally on review changes

Solution

  1. Step 1: Consider query and update load

    Millions of products and users mean many queries and updates.
  2. Step 2: Choose efficient strategy

    Precomputing average and count and updating them incrementally avoids scanning all reviews each time.
  3. Step 3: Evaluate other options

    Computing average on each query is slow; no indexes cause slow lookups; caching only latest review misses full rating info.
  4. Final Answer:

    Maintain precomputed average and count, update incrementally on review changes -> Option D
  5. Quick Check:

    Precompute + incremental update = scalable [OK]
Hint: Precompute averages, update on changes for scale [OK]
Common Mistakes:
  • Recomputing averages on every query
  • Ignoring indexing and caching strategies
  • Caching incomplete data causing stale info