Skip to content
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

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
1user → ingest · POST /v1/sharesA reader shares an article. The app sends event_id, article_id and the channel.1 / 7

The numbers, at a glance

In a big news spike……one article can take 3 of every 10 shares
1 viral article ≈ 30,000/sEvery other article ≈ 70,000/s
Messages to the countersAt a 100,000 shares/s spike
Live state is small3 GB of buckets and totals for 10 million articles
So exact counts are affordable; no sketch needed.

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.

SQL + ORDER BY
Too slow
INSERT share row
GROUP BY article, last 1 h
Seconds per query
+ Simple and exact.− Scans millions of rows for every list.
Sketch on one node
Approximate
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.
Partition + merge
Our pick
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.

Stage 1 · ≈ 1,000 shares/sMake it work
Clients
App serverTop-K API
CounterResult cache
One counter process with time buckets in memory.
Stage 2 · ≈ 30,000 shares/sPartition the counting
Clients
Share APITop-K API
Share logArchive
Counter shardsTop-K mergerResult cache
A log by article, 16 counter shards, a merger and checkpoints.
Stage 3 · ≈ 100,000 shares/sSurvive the spike
Clients
Share API + combineCDN
Share logTop-K API
Counter shardsTop-K mergerResult cache
Combine counts per article for 1 s; cache lists at the edge.
Stage 4 · 100,000 shares/s + botsCount real shares
Clients
Share APIShare filterCDN
Share logTop-K API
Counter shardsTop-K mergerResult cache
Drop repeats and bot shares before counting.
Was this walkthrough useful?Your vote tells us what to improve next.