Design a real-time trending-topics system
A trending-topics system finds the top-K most-mentioned items over a sliding time window from a high-volume social stream. The challenge is computing top-K at scale without storing exact counts for every term.
Streaming & Pipelines, 10 components, about 35 min
1. Scope
In scope
- • Ingest a high-volume event stream
- • Compute top-K over a sliding window
- • Per-region + global trends
- • Near-real-time updates
Out of scope
- • Spam/bot filtering ML
- • Personalized trends
- • Historical analytics UI
2. Functional requirements
Must have
- • Top-K trending items, updated continuously
- • Sliding time window (e.g., last hour)
- • Regional + global views
- • Handle bursty volume
Nice to have
- • Trend velocity (rising fast)
- • Categories
- • Exact reconciliation
3. Non-functional requirements
4. Back-of-envelope estimation
Assumptions
- • 500K posts/sec peak
- • Sliding 1-hour window
- • Top-50 per region
- • Approximate counting
Derived numbers
5. API design
Current top-K for a region
6. High-level design
What each box in the reference diagram is responsible for:
7. Data model
8. Deep dives
Approximate counting with Count-Min Sketch
Tracking exact counts for every hashtag is memory-prohibitive at 500K events/sec. A Count-Min Sketch hashes each term into a few small counter arrays; lookups take the min across rows. It uses fixed memory regardless of cardinality, never undercounts, and overcounts only slightly — perfect for "what's hot" where exactness doesn't matter.
Sliding window
Trends are about recency. A streaming engine (Flink) maintains a sliding window (e.g., last hour, advancing each minute) so old mentions age out. This requires windowed state, checkpointed for fault tolerance.
Regional then global
Compute trends per region in parallel (smaller state, local relevance), then a reducer merges regional top-K lists into a global ranking. This shards the work and supports localized trends.
Lambda-style reconciliation
The fast streaming path is approximate. An hourly batch (Spark) computes exact counts from the data lake and reconciles the live results, correcting any drift — the classic speed-layer + batch-layer pattern.
9. Bottlenecks
- Cardinality of terms → Count-Min Sketch bounds memory
- Hot keys/celebrities → sketch handles skew gracefully
- Window state size → checkpoint + shard by region
10. Trade-offs
Exact or approximate counts
Choose: Count-Min Sketch per time bucket plus a heap of top-K candidates.
Instead of: Exact hash map of every term: memory grows with distinct terms, which spike during events.
Trending only needs the heavy hitters; sketch error is small relative to them.
What "trending" means
Choose: Velocity against a baseline: current rate divided by the expected rate for this term and hour.
Instead of: Raw volume: always shows the same evergreen terms.
Trending is about change, not popularity.
Window
Choose: Sliding window built from 1-minute buckets (sum the last 60).
Instead of: Exact sliding window over raw events: expensive.
Bucketed windows are cheap and accurate enough.
11. Failure modes
Coordinated manipulation (bot networks)
Fake trends.
Mitigation: Count unique authors with account-quality weights rather than raw posts.
Stream processor restarts
Counts reset.
Mitigation: Checkpointed state (for example Flink snapshots) and replay from Kafka offsets.
Harmful term trends
Platform amplifies it.
Mitigation: Policy filters and human review before promotion to the trends module.
12. What interviewers probe
Answer each one yourself before opening it.
How big is the Count-Min Sketch?Show a strong answer
Width e/ε, depth ln(1/δ). For ε = 0.0001 and δ = 0.001: ~27K × 7 counters ≈ 760 KB per bucket.
How do you do trends per region?Show a strong answer
Partition the stream by region; each region keeps its own sketches and top-K.
How do you merge results across partitions?Show a strong answer
Sketches with identical hash functions add element-wise; merge candidate heaps and re-query the merged sketch.
How quickly must a trend appear?Show a strong answer
Within about a minute: bucket size and pipeline latency set the floor.
Sign in to track this guide in your curriculum progress.