Design a Reviews and Ratings System
Understanding the Problem
⭐ What is a reviews and ratings system? It lets users give an item a score, usually 1 to 5 stars, with optional text and photos, and shows everyone the item's average score, how many ratings it has and the individual reviews. It's a shared component behind product pages, restaurant and dish listings, app stores and photo-rating sites. The write side is small; the read side is huge, because the average is shown everywhere the item appears.
You'll see two common versions. One is reviews of food items on a delivery app: rate a dish, write a review, see each dish's average and its restaurant's average. The other is an image rating platform: users rate images, and the site shows top-rated images, but only among images with at least a minimum number of ratings. They're the same system: a stream of (user, item, score) events, aggregates per item, and a ranking on top of the aggregates. We'll use dish reviews as the main example and cover the image ranking in a deep dive.
Functional Requirements
Core Requirements
- Users should be able to rate an item from 1 to 5 stars, optionally with text and photos. One rating per user per item, which they can edit or delete.
- Users should be able to see an item's average rating, rating count and star histogram wherever the item is shown.
- Users should be able to read an item's reviews, sorted by most recent or most helpful.
- Users should be able to browse the top-rated items in a category, counting only items with at least a minimum number of ratings.
Below the line (out of scope)
- Full-text search inside reviews.
- Personalized recommendations based on ratings.
- Seller or restaurant replies to reviews.
Non-Functional Requirements
Ask your interviewer for scale. For ours: 100 million items (dishes across restaurants, or images), 10 million new ratings per day, and listing pages that show an item's average are viewed 5 billion times per day, because every search result and menu shows a star rating.
Core Requirements
- Reading an item's aggregate rating should be fast, under 10 ms at the service, at very high read volume.
- Aggregates can be eventually consistent: a new rating should be reflected within a minute or so. The user's own review should appear to them immediately.
- Aggregates must be correct over time, including after edits, deletes and moderation removals. No drift.
- Each user gets one rating per item, enforced reliably.
- Rankings should resist manipulation: an item with three 5-star ratings shouldn't outrank one with 2,000 ratings averaging 4.8.
Below the line (out of scope)
- Strong consistency between a rating and its aggregate.
- Multi-region writes.
Let's do the arithmetic. 10M ratings per day is about 115 writes per second, maybe 1,000 at peak. That's small. 5B aggregate reads per day is about 58,000 per second on average and a few hundred thousand at peak. Storing 100M aggregates at around 50 bytes each (sum, count, five histogram buckets) is about 5 GB, which fits comfortably in a cache cluster.
The ratio is the whole story: reads outnumber writes by roughly 500 to 1. That tells you to precompute the aggregate on write (or shortly after) and serve it from a cache, never compute an average at read time.