Case Study 26 β Trending Topics (Hashtags)
Design a service like Twitter/X Trending: show the top hashtags in a region over a sliding time window (e.g., last 15 minutes).
1. Problem
When users post tweets with hashtags, the platform must count mentions in real time and surface the top K trending tags per region (country, city, or global).
Users expect trends to update every few minutes and reflect sudden spikes (breaking news, viral memes).
2. Requirements
Functional (MVP)
- Ingest tweet events containing one or more hashtags
- Aggregate counts per hashtag per region per time window
- Return top K trending hashtags for a region (e.g., K = 10)
- Support multiple window sizes (15 min, 1 hour, 24 hours)
- Filter spam / banned hashtags
Out of scope (initially)
- Personalized trends per user
- Sentiment analysis or topic clustering (e.g., grouping
#covidand#coronavirus) - Trend explanations ("Why is this trending?")
Non-functional
- Near real-time updates (trends refresh every 1β5 minutes)
- High write throughput (millions of tweets/day)
- Low latency reads for the trends page (< 200 ms)
- Approximate counts are acceptable; exact counts not required
- Graceful degradation during traffic spikes
3. Back-of-the-envelope estimates
These numbers are rough order-of-magnitude math β not a capacity plan. In interviews, the goal is to show you know what to count and which resource breaks first. A useful shortcut: 1 QPS sustained β 86,400 requests/day. Trending spikes during breaking news can hit 5β10Γ average for minutes in one region.
Why we estimate
Trending hashtags sit between firehose ingestion (millions of tweets) and cheap reads (top-10 list per region). Estimates tell us:
- Whether we can count in memory or need streaming aggregation
- Why we precompute top-K instead of sorting all hashtags on every read
- How many unique tags per window fit in RAM per region
Assumptions
| Assumption | Value | Why it matters |
|---|---|---|
| Tweets per day | 500M | Ingestion volume |
| Hashtags per tweet (avg) | 1.5 | Counter increments per tweet |
| Regions (countries + cities) | 50 | Partition trending by geo |
| Top K displayed | 10 | Output size per region |
| Window sizes | 15 min, 1 hr, 24 hr | Multiple counter maps |
| Trend page refreshes | 10K QPS peak | Read path |
Step A β Traffic (QPS) with labeled arithmetic
Hashtag counter increments (writes to aggregation layer):
Hashtag events/day = 500M tweets Γ 1.5 hashtags/tweet
= 750M counter increments/day
Write QPS (avg) = 750M Γ· 86,400
β 8,700 increments/second
Peak write QPS (3Γ viral event) β 8,700 Γ 3
β 26,000 increments/second
Each tweet may bump 1β3 hashtag counters in 1β2 regions (user geo + global).
Trending page reads:
Assume 200M trend page views/day across all regions
Read QPS (avg) = 200M Γ· 86,400 β 2,300 reads/second
Peak (5Γ) β 11,500 reads/second
Design for β 10K QPS peak (given assumption)
Reads must be < 200 ms β serve precomputed top-10 from cache.
Per-region write share:
Global + 50 regions β not every tweet hits every region
Effective counters per tweet β 1.2 region updates avg
Regional write QPS (peak) β 26,000 Γ (1.2/51) β 600/s per hot region
US region might get 30% alone β ~8,000/s during viral US news
Step B β Storage
In-memory counters (15-minute sliding window, per region):
Unique hashtags in 15-min window (per region) β 100K active tags
Bytes per entry β 28 B (tag string ~20 B + count 8 B)
Memory per region = 100K Γ 28 B β 2.8 MB
50 regions β 140 MB for 15-min window
Three window sizes (15m, 1h, 24h):
24h window may hold ~2M unique tags/region β ~56 MB/region β ~2.8 GB total β still RAM-friendly
Historical trend snapshots (optional):
Snapshots every 5 min Γ 50 regions Γ 10 tags Γ 100 B
Per day β 288 Γ 50 Γ 1 KB β 14 MB/day β store in Postgres or S3 for βtrends over timeβ
Tweet stream (Kafka β not long-term storage for counters):
750M events/day Γ 200 B event β 150 GB/day through Kafka
Retention 24β48 hr β ~150β300 GB Kafka disk
Step C β Bandwidth and other resources
Trend API response (read path):
Top-10 payload β 10 tags Γ 50 B (tag + count + rank) β 500 B
Peak read QPS β 10,000/s
Egress = 10,000 Γ 500 B β 5 MB/s β trivial if precomputed
Ingestion from tweet service:
Tweet events peak β 26,000 hashtag updates/s (same as counter writes)
Event size β 200 B (tweet_id, hashtags[], region, timestamp)
Kafka ingest β 26,000 Γ 200 B β 5.2 MB/s β modest
Top-K maintenance (avoid full sort):
Don't sort 100K tags on every read
Use min-heap of size K per region β update O(log K) per increment
Or batch recompute top-10 every 30β60 seconds in background worker
Step D β Read:write ratio table
| Operation | Type | Avg QPS | Peak QPS | Notes |
|---|---|---|---|---|
| Ingest tweet β increment counters | Write | ~8,700 | ~26,000 | Stream processing |
| Fetch top-K trends (region) | Read | ~2,300 | ~10,000 | Precomputed cache |
| Ban/spam filter check | Read | ~8,700 | ~26,000 | Bloom filter or blocklist |
| Publish snapshot (worker) | Write | ~17/s | ~17/s | 50 regions / 3 windows every 60 s |
| Admin override / block tag | Write | ~0.01 | ~1 | Rare |
Ratio: counter writes ~3Γ trend reads on average β but reads must be instant; writes can lag 30β60 s.
What the numbers tell us
- ~26K counter increments/s peak β Kafka + stream processors (Flink/Storm/custom), not synchronous DB increments
- ~140 MB RAM for 15-min counters across 50 regions β in-memory aggregation works
- Precompute top-10 every 30β60 s β reads hit Redis/ CDN cache, never sort 100K tags live
- Multiple windows (15m/1h/24h) β separate counter maps or time-bucketed keys with TTL
- Spam/banned hashtags β filter at ingest; one viral bot tag shouldnβt pollute trends
- Regional partitioning β US breaking news doesnβt require recomputing Icelandβs trends
Common mistake for this problem
Running SELECT hashtag, COUNT(*) GROUP BY hashtag ORDER BY count on a SQL database for every trend refresh. At 750M events/day, thatβs impossible. Another mistake: exact counts β approximate streaming counts (HyperLogLog or lossy counters) are fine for display. Finally, global sort of all hashtags on every tweet β use incremental top-K heaps per region.
4. High-Level Design (HLD)
Components
| Component | Role |
|---|---|
| Kafka | Durable stream of tweet-created events |
| Counter Workers | Parse hashtags, increment per-region counters |
| Redis | Fast sliding-window counters (sorted sets or time-bucketed keys) |
| Window Aggregator | Periodically roll up buckets, compute top-K, write snapshot |
| Trends Store | Persisted top-K snapshots per region/window |
| Trends Cache | Serve precomputed lists (CDN-friendly) |
| Trends API | GET /trends?region=US&window=15m |
Flows
Ingest
- Tweet service publishes
{ tweetId, region, hashtags[], timestamp } - Counter worker normalizes hashtags (lowercase, strip
#) - Increment counters in Redis using time buckets (e.g., 1-minute buckets for 15-min window)
- Optionally filter banned/spam tags before counting
Compute top-K (every 1β5 min)
- Scheduler triggers aggregator per region
- Sum last N minute-buckets β score per hashtag
- Run top-K selection (min-heap of size K or approximate Heavy Hitters)
- Write snapshot to Trends Store + invalidate Trends Cache
Read
- Client requests trends for region
- API returns cached snapshot (stale up to refresh interval is OK)
- On cache miss, read from Trends Store
Trade-offs
- Exact counts vs approximate β Count-Min Sketch or Heavy Hitters save memory; exact Redis counters are simpler for MVP
- Push vs pull compute β Precompute top-K on a schedule (pull) vs update on every tweet (push); precompute scales better for reads
- Global vs regional β Separate counter namespaces per region avoid cross-shard joins
5. Low-Level Design (LLD)
APIs
GET /api/v1/trends?region=US&window=15m&limit=10
β {
"region": "US",
"window": "15m",
"updatedAt": "2026-07-20T12:05:00Z",
"trends": [
{ "hashtag": "worldcup", "score": 18420, "rank": 1 },
{ "hashtag": "ai", "score": 9210, "rank": 2 }
]
}
POST /internal/v1/events/tweet-created (internal only)
Body: {
"tweetId": "t_123",
"region": "US",
"hashtags": ["WorldCup", "USA"],
"createdAt": "2026-07-20T12:04:31Z"
}
β 202 Accepted
GET /api/v1/trends/:hashtag?region=US&window=15m
β { "hashtag": "worldcup", "score": 18420, "rank": 1 }
Schema
-- Precomputed snapshots (source for reads)
trend_snapshots (
id BIGSERIAL PRIMARY KEY,
region VARCHAR(8) NOT NULL,
window_name VARCHAR(16) NOT NULL, -- '15m', '1h', '24h'
computed_at TIMESTAMPTZ NOT NULL,
payload JSONB NOT NULL -- ordered list of {hashtag, score, rank}
)
CREATE INDEX idx_snapshots_region_window_time
ON trend_snapshots (region, window_name, computed_at DESC);
-- Optional: historical archive for analytics
trend_history (
region VARCHAR(8),
window_name VARCHAR(16),
hashtag VARCHAR(256),
score BIGINT,
bucket_start TIMESTAMPTZ,
PRIMARY KEY (region, window_name, hashtag, bucket_start)
);
banned_hashtags (
hashtag VARCHAR(256) PRIMARY KEY,
reason TEXT,
banned_at TIMESTAMPTZ NOT NULL
)
Redis key patterns:
cnt:{region}:{minute_epoch}:{hashtag} β INCR (expires after window + buffer)
trends:{region}:15m β cached JSON snapshot (TTL 60s)
Modules
TrendsController
TrendsReadService
TweetEventConsumer
HashtagNormalizer
SlidingWindowCounter
TopKAggregator
TrendSnapshotRepository
BannedHashtagFilter
Algorithm β sliding window with minute buckets
Divide the 15-minute window into 15 one-minute buckets. Each tweet increments the bucket for its event minute.
function incrementHashtag(region, hashtag, eventTime):
tag = normalize(hashtag) // lowercase, unicode normalize
if bannedTags.contains(tag): return
minute = floorToMinute(eventTime)
key = "cnt:" + region + ":" + minute + ":" + tag
redis.incr(key)
redis.expire(key, 20 minutes) // window + safety margin
function scoreInWindow(region, hashtag, now, windowMinutes=15):
total = 0
for m in last N minute keys:
total += redis.get("cnt:" + region + ":" + m + ":" + hashtag) ?? 0
return total
Algorithm β top-K with min-heap
For each region, scan active hashtags in the window (from Redis key scan or a secondary "active set") and keep only top K.
function computeTopK(region, windowMinutes, K):
candidates = getActiveHashtags(region, windowMinutes) // from Redis SCAN or HyperLogLog set
heap = MinHeap(size=K) // stores (score, hashtag)
for tag in candidates:
score = scoreInWindow(region, tag, now(), windowMinutes)
if score == 0: continue
if heap.size < K:
heap.push((score, tag))
else if score > heap.peek().score:
heap.pop()
heap.push((score, tag))
return heap.sortedDescending()
Optimization: Use Space-Saving or Count-Min Sketch + min-heap when candidate set is huge (millions of tags) β tracks heavy hitters in O(1) memory per stream.
Algorithm β velocity / spike detection (optional)
Trending is not just high count β it's acceleration:
velocity(tag) = count(last 5 min) / max(count(previous 5 min), 1)
finalScore = count Γ log(1 + velocity)
This surfaces sudden spikes even if absolute count is lower than evergreens.
Concurrency & correctness
- Counters are eventually consistent β acceptable for trends
- Idempotent consumer: dedupe by
tweetIdin Kafka (at-least-once β safe INCR if processed once) - Snapshot writes are monotonic: only publish if
computed_atis newer - Banned hashtag list cached locally with short TTL
6. Scale evolution
| Stage | Change |
|---|---|
| MVP | Single Redis + Postgres snapshots; one aggregator cron |
| Write spike | Partition Kafka by region; scale counter workers horizontally |
| Huge hashtag cardinality | Count-Min Sketch per region; Space-Saving for top-K |
| Global scale | Shard Redis by hash(region); regional aggregators |
| Sub-minute freshness | Stream processing (Flink) with tumbling windows instead of cron |
| Personalization | Separate ranking layer using user interests (out of MVP scope) |
7. Recap
- Trends = high write, low read freshness β precompute top-K on a schedule
- Use time buckets for sliding windows instead of storing every event
- Top-K via min-heap; approximate algorithms when cardinality explodes
- Reads serve snapshots from cache β don't compute rankings on the hot path
Practice: redraw the HLD from memory, then write pseudocode for incrementHashtag and computeTopK without looking.