Reviews and ratings section of an e-commerce product page

Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2025-05-11

Problem Statement

Design a Reviews and Ratings System for an e-commerce platform (like Amazon, Flipkart, or Myntra) that allows customers to submit, view, and analyze product reviews and ratings.


The system should efficiently handle high read traffic (millions of product page views) and moderate write traffic (user reviews), while supporting real-time rating aggregation, spam detection, sorting, and filtering.


🎯 Objective


To build a scalable backend system that:


•Allows users to post reviews and ratings for products they purchased.
•Displays average ratings, total counts, and detailed breakdowns (1★–5★).
•Supports filtering and sorting (e.g., “Most recent”, “Top-rated”, “With images”, etc.).
•Handles millions of reviews efficiently using caching and pagination.
•Ensures data integrity, authenticity, and moderation of reviews.


✅ Functional Requirements


1. User Actions

Users can:


•Add a rating (1–5 stars) and review text.
•Upload images/videos of the product.
•Edit or delete their own reviews.
•Mark reviews as “Helpful” or “Report abuse”.


2. Product Page Display

Show:


•Average rating (e.g., 4.2★).
•Rating distribution (5★: 70%, 4★: 20%, etc.).
•Total number of reviews.
•Paginated list of reviews.
•Filters:
•Star rating (e.g., only 1★ reviews)
•With images/videos
•Verified purchases only
•Sorting:
•Most recent
•Highest/lowest rating
•M
•ost helpful


3. Backend Operations


•Store and index all reviews per product.
•Aggregate ratings in real time.
•Handle bulk reads efficiently.
•Support spam filtering (e.g., multiple reviews from same user/IP).
•Allow moderation (flag and review inappropriate content).


4. Analytics


•Compute metrics like:
•Average rating per product.
•Number of verified vs non-verified reviews.
•Rating trends over time.


⚙️ Non-Functional Requirements


Attribute with Goal


Scalability

Handle millions of reviews and billions of reads per day


Latency

Product page reviews should load in < 200ms


Availability

99.9% uptime


Consistency

Eventual consistency for rating aggregation


Reliability

Persist reviews safely (no data loss)


Security

Only verified users can review purchased products


Extensibility

Support multi-language reviews or video reviews later


🧩 Core Components


Component with Description


Review Service

Handles CRUD operations for user reviews


Rating Aggregation Service

Calculates and updates average ratings


User Validation Service

Confirms user purchased the product before posting


Content Moderation Service

Flags or filters abusive/spam content


Search & Filter Engine

Enables efficient filtering and sorting


Cache Layer

Stores frequently accessed rating data


Storage Layer

Persists reviews, ratings, and metadata


Notification Service

Sends notifications for replies or moderation actions


🧱 High-Level Architecture

1 2 +---------------------------+ 3 | Product Page Frontend | 4 +------------+--------------+ 5 | 6 v 7 +-----------+------------+ 8 | API Gateway | 9 +-----------+-------------+ 10 | 11 +----------------------------------------------------+ 12 | Application Layer | 13 +----------------------------------------------------+ 14 | Review Svc | Rating Agg Svc | Moderation | Cache Svc| 15 +----------------------------------------------------+ 16 | 17 +------------------+-------------------+ 18 | | 19+-------v-------+ +-------v-------+ 20| DB (SQL/NoSQL)| | Search Index | 21| (Reviews,Users)| | (Elasticsearch)| 22+-------+--------+ +----------------+ 23 | 24+-------v--------+ 25| Cache Layer | 26| (Redis/Memcached)| 27+------------------+

🧠 System Flow


1. Add Review


1.User posts review → API Gateway → Review Service.
2.Review Service:
•Validates user purchase.
•Stores review in DB.
•Publishes event to “Rating Update” queue.
3.Rating Aggregation Service consumes event → recalculates product’s average rating.
4.Updated rating is cached and reflected on product page.


2. Display Reviews


1.Product page requests /api/reviews?productId=123&sort=recent.
2.Cache Layer (Redis) returns aggregated rating and recent reviews (if cached).
3.If cache miss → Review Service queries DB → updates cache.
4.Frontend displays star distribution and review list.


3. Moderation Flow


1.Content Moderation Service (ML or keyword-based) scans new reviews.
2.Flags suspicious ones → stored in “Pending Review” state.
3.Admin dashboard allows manual approval or rejection.


🧱 Data Model (Simplified)


Review

1 2{ 3 "review_id": "R123", 4 "product_id": "P456", 5 "user_id": "U789", 6 "rating": 4, 7 "title": "Good quality", 8 "comment": "Product was as expected", 9 "images": ["img1.jpg", "img2.jpg"], 10 "helpful_count": 15, 11 "is_verified_purchase": true, 12 "status": "APPROVED", 13 "created_at": "2025-10-24T10:00:00Z" 14}


Product Rating Summary

1 2{ 3 "product_id": "P456", 4 "avg_rating": 4.3, 5 "total_reviews": 2100, 6 "rating_breakdown": { "5": 1400, "4": 400, "3": 200, "2": 70, "1": 30 }, 7 "last_updated": "2025-10-24T10:10:00Z" 8}


⚡ Scalability & Optimization Considerations

| Feature | Approach |

| ----------------------- | ----------------------------------------------------------- |

| **High read traffic** | Use Redis/Memcached for rating summaries and recent reviews |

| **Efficient filtering** | Use Elasticsearch / OpenSearch for indexed queries |

| **Bulk writes** | Write reviews asynchronously via Kafka queue |

| **Real-time updates** | Event-driven aggregation for rating recalculation |

| **Data consistency** | Eventual consistency for rating averages |

| **Spam detection** | Use ML or heuristics (IP, timing, content similarity) |

| **Pagination** | Cursor-based for performance |


🧰 Tech Stack (Example)


| Layer | Technology |

| -------------- | ----------------------------------------- |

| **Frontend** | React / Vue (Product Page) |

| **Backend** | Node.js / Java / Go |

| **Database** | MongoDB / Cassandra (Reviews) |

| **Cache** | Redis |

| **Search** | Elasticsearch |

| **Queue** | Kafka / RabbitMQ |

| **Auth** | OAuth2 / JWT |

| **Moderation** | AWS Comprehend / OpenAI API / Internal ML |

| **Deployment** | Docker + Kubernetes |

| **Monitoring** | Prometheus + Grafana |


🧠 Key Challenges


| Challenge | Approach |

| ------------------------ | ---------------------------------------------- |

| **Heavy read load** | Use caching and pre-computed aggregates |

| **Spam & fake reviews** | ML-based anomaly detection |

| **Large dataset** | Shard reviews by product_id |

| **Realtime aggregation** | Use event-driven updates instead of batch jobs |

| **Search & filtering** | Index reviews asynchronously |

| **Moderation delay** | Separate staging and approved tables |


✅ Goal:

Build a highly available, scalable, and low-latency Reviews & Ratings system that provides real-time insights to users while ensuring authenticity, moderation, and performance for an e-commerce platform.

Solution

Comments