Reference path
The Comment System solution is read-only study material. Use it to compare your own design against a compact reference path before starting practice or after you finish a pass.
- Use an adjacency list storage model with a max depth limit of 3 -- simple writes, bounded query complexity with a fixed-level join.
- Cache the first page of comment threads per post in Redis; invalidate on any new comment for that post.
- Run spam scoring synchronously during comment creation; route mid-confidence scores to a human review queue.
- Use soft delete with placeholder text to preserve child reply visibility after a parent is removed.
- Push real-time updates via WebSocket with Redis Pub/Sub fan-out for posts with many concurrent viewers.
Review the problem detail before you compare trade-offs.
High-level design
The Comment API handles all CRUD operations and delegates upstream concerns to the Spam Filter (on creation) and Notification Service (on creation). The Comment Store is a relational database with an adjacency list schema. The Cache layer (Redis) holds serialized flat comment pages keyed by post ID and cursor token. A hot read path: the API looks up the cache by (post_id, cursor); on cache hit, it returns the serialized page directly; on miss, it queries the store for top-level comments plus the first reply level, serializes, caches with a 60-second TTL, and returns.
POST /posts/{postId}/comments
GET /posts/{postId}/comments?cursor=X&sort=top
PATCH /comments/{commentId}
DELETE /comments/{commentId}
POST /comments/{commentId}/reactions
POST /comments/{commentId}/reports
Thread tree storage models
The adjacency list is the recommended choice for a system with a depth limit of 3 and high write volume. Queries reconstruct the tree with a 3-way join or a single recursive CTE. Materialized path enables faster subtree reads (WHERE path LIKE '1/42/%') but requires rewriting all descendant rows on subtree moves. Nested set provides O(log n) range-query subtree access but O(n) insertion cost -- a poor fit for comment workloads where writes are frequent.
Real-time updates
New comment events are published to a Redis Pub/Sub channel per post. Multiple push server instances each hold a subset of WebSocket connections and subscribe to the channel; the broker fans out the event to all push servers, which each deliver it to their connected viewers. This decouples the Comment API from individual connection state and scales fan-out horizontally by adding push server instances.
Spam filtering pipeline
The spam filter computes a composite score from three signals: per-user comment rate in the last 60 seconds (rate score), link density in content (link score), and content similarity to known spam patterns (pattern score). Score below 20: auto-allow. Score 20-70: store as pending, route to human review queue sorted by score descending so highest-confidence spam is reviewed first. Score above 70: auto-block with 422 response. Human reviewers approve or reject pending comments; approved comments transition to visible status and the cache is invalidated.
Trade-offs to study
Adjacency list versus materialized path: adjacency list has O(1) writes and requires recursive reads bounded by depth limit; materialized path has O(1) reads for subtrees but O(n) rewrite cost on moves. For a fixed-depth comment tree with no move operation, adjacency list is simpler and sufficient.
Push versus poll for real-time: WebSocket push provides sub-second notification but requires persistent connections that consume server resources. Long-poll fallback is compatible with all clients and self-heals on connection drop, but adds latency proportional to poll interval. For most comment systems, WebSocket with a poll fallback is the right combination.
Automated versus human moderation: automated scoring handles high-confidence spam at scale with no human cost. Human review is expensive but required for mid-confidence cases where false positives affect legitimate users. Calibrating the threshold range minimizes both missed spam and false human review queue volume.
Do not model learner practice diagrams as Mermaid in this reader. Authored Mermaid explains reference content; learner high-level-design work remains a future canvas submission flow.