0
0
LLDsystem_design~7 mins

Rating and review system in LLD - System Design Guide

Choose your learning style9 modes available
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.