KatInterview

Data Model for Course Reviews, Aggregates, and Moderation

System DesignModel answer
HardModel answerSystem Designdatabase-designschema-designsystem-design

Last updated

Design an online course review system covering completion checks, transactional rating aggregates, edit histories, and keyset-paginated read paths.

Design the relational database architecture and query strategy for a course review and rating system within an online learning platform.

After completing a course, a learner can post a numerical score (1 to 5 stars) along with a text review. Potential students depend on overall course scores and review feeds when selecting courses to buy.

Focus on table structure, primary and foreign keys, integrity constraints, data mutations, and index design for high-volume read operations.

Requirements & Functional Expectations

  1. Completion Check: Only learners with a verified, completed course enrollment can write a review.
  2. Review Multiplicity Rules: A learner can submit at most one active review per completed enrollment. If a learner re-enrolls and completes the course again in the future, they can submit a new review for that specific completion instance.
  3. Lifecycle & Governance: Learners can edit or mark their reviews as deleted. Platform moderators can hide policy-violating reviews. Historical edit snapshots must be audit-logged.
  4. Read Access Paths:
    • Course overview pages require real-time aggregated metrics (average rating, total counts, star distribution) without triggering expensive table scans.
    • Review lists must support paginated browsing filtered to visible reviews, ordered by recency (newest first) or helpfulness (most upvotes first).
  5. Instructor Engagement: Instructors can publish at most one response per review written for their course.

Core Architecture Tasks

  1. Entities & Integrity Constraints: Define tables, unique keys, candidate keys, foreign key constraints, and custom checks (e.g., handling retakes, preventing duplicate reviews or orphaned responses).
  2. Read Path Optimizations & Indexing Strategy: Detail how aggregate ratings and sorted, paginated review feeds are served under heavy read throughput.
  3. Consistency Under State Mutations: Detail how review edits, deletions, upvotes, and moderation state changes keep aggregate counters and index structures accurate.

Hints

Hint 1

Key the review on the unique enrollment instance identifier rather than a composite (learner_id, course_id) key to seamlessly allow retake reviews.

Hint 2

Maintain real-time aggregated metrics in a dedicated aggregate table updated inside the write transaction to keep rating lookups O(1).

Hint 3

Implement keyset (cursor) pagination using composite indexes to prevent deep-OFFSET latency spikes on high-volume feeds.