SD-13 · Back to the write-up
Design a Top Shared Articles
Partition share events by article, count them in time buckets on the owning shard, and merge each shard’s top K into one cached list.
Follow a request
Build the lists
Read · CDN hit
Read · CDN miss
A viral article
A counter shard crashes
Clients
Edge and ingest
Stream processing
Results
ClientsShare + read
CDNLists for 5 s
Share APICombine 1 s
Share filterDedup + bots
Top-K APIStateless
Share log64 partitions
ArchiveEvents + checkpoints
Top-K mergerUnion of 16 lists
Counter shards16, by article
Result cacheTop 100 per window
event_id, article_id and the channel.1 / 7The numbers, at a glance
1 viral article ≈ 30,000/sEvery other article ≈ 70,000/s
100,000/s1 counter
30,000/s16 shards
≈ 750/s+ combine
3 GB live state50 GB of events a day
A share, in order
From a tap on “share” to the article moving up the 5-minute list, within about 6 seconds.
Client
Share API
Share filter
Share log
Counter shard
Top-K merger
Result cache
1 POST /v1/shares
2 seen? pair? bot score
3 count it
4 1-second sum per article
5 ack (2 of 3 copies)
6 202 Accepted
7 records for its partitions
8 shard top 100, every 2 s
9 SET top:5m
RequestResponseAsync, does not block
How to count
The decision this question is really about.
INSERT share row
GROUP BY article, last 1 h
Seconds per query
+ Simple and exact.− Scans millions of rows for every list.
All events → 1 node
Count-min sketch + heap
Ranks near the cut-off may be wrong
+ Tiny, fixed memory.− Over-counts; one node caps the rate.
Combine 1 s at ingest
16 shards by articleTime buckets
Merge 16 × top 100
+ Exact, fresh, scales out.− Needs combining for hot articles.
How it grows
Add a box only when a number forces you to. Highlighted boxes are new at each stage.
Clients
App serverTop-K API
CounterResult cache
Clients
Share APITop-K API
Share logArchive
Counter shardsTop-K mergerResult cache
Clients
Share API + combineCDN
Share logTop-K API
Counter shardsTop-K mergerResult cache
Clients
Share APIShare filterCDN
Share logTop-K API
Counter shardsTop-K mergerResult cache
Was this walkthrough useful?Your vote tells us what to improve next.