SD-22 · Back to the write-up
Design a Top K Rankings
Count every event in a log partitioned by item, rank each list once a minute, and serve charts from a cache and the edge.
Follow a request
Count an event
Count and flush
Build the lists
Read · CDN hit
Read · CDN miss
A worker crashes
Clients
Edge + ingest
Counting
Ranking + data
Purchase serviceSends events
ReaderApp or web
CDNPages, 30 s
API serversStateless
Dedupe filterSeen today?
Event log16 partitions
Counter workersOne per partition
Count storeRollups by item
Top-K builderEvery minute
Rankings cacheVersioned lists
event_id.1 / 5The numbers, at a glance
1 miss ≈ 700/s at peak19 edge hits
200,000/sPer event
1,000/sCombined
260 MB of listson a 6 GB cache node
An event, in order
Ingest returns at step 4. Steps 5 to 9 happen off the request path, within a minute.
Purchase service
API server
Dedupe filter
Event log
Counter worker
Top-K builder
Rankings cache
1 POST /v1/events
2 seen (user, item) today?
3 no
4 append by item_id
5 202
6 event stream
7 changed keys, via store
8 write v8813
9 flip :current
RequestResponseAsync, does not block
How to count and rank
The decision this question is really about.
event
UPDATE n = n + 1
ORDER BY per view
+ Exact and live.− A big sort per view, a hot row per viral app.
raw log
batch job / hour
81,000 lists
+ Exact and simple.− Charts an hour old.
log by item_id
count in memoryflush / 10 s
rank / minutecache + CDN
+ Exact, a minute fresh, cheap reads.− A log, workers and a builder to run.
How it grows
Add a box only when a number forces you to. Highlighted boxes are new at each stage.
Clients
API server
SQL counts
Clients
Load balancer
API × N
Count DBTop-K builderRankings cache
Clients
Load balancer
API × NEvent log
Counter workers × 16
Count storeTop-K builderRankings cache
ClientsCDN
Load balancer
API × NDedupe + fraud filterEvent log
Counter workers × 16
Count storeTop-K builderRankings cache
Was this walkthrough useful?Your vote tells us what to improve next.