Design a Top K / Trending Topics System
hard 45 minCount billions of mentions a day and show the ten topics that are rising right now, in each country and city.
See it run
Follow a mention into the counters, watch a small table hold a huge stream, and see how the list is made.
Operations
Take in a batch
500 mentions arrive. They are stored first, then filtered and added up.
Step 1 of 10
Send a batch
Batch
b-91, 500 events
The post service sends 500 events, about 50 KB. One request each would cost more than the events.
Step 1: Send a batch
def ingest(caller, batch): limiter.check(caller) # 429 if over good = [e for e in batch if well_formed(e)] log.append(good, key="account") # wait for copies return 202, len(good)def filter_event(e): if (e.account, e.topic, e.hour) in seen: return seen.add((e.account, e.topic, e.hour)) w = trust(e.account) # 0 under 0.3 for place in (e.city, e.country, "world"): sums[place, e.topic, e.minute] += w # sent every 10 sdef on_sum(place, topic, minute, n): counter.add((place, topic), minute, n)- Delay
- p99 under 100 ms
- Size
- 46,300 events/s
- Peak (3x)
- 139,000/s
- Event
- 100 B
Accepted means stored. Counted comes later.
In the real world
The same trending topics doing real jobs. Pick one and step through it.
A final ends. Forty thousand mentions a second are the same word.
Step 1 of 4
If split by topic
- Counter 1
- 40,000/s
- Counter 2
- 3,000/s
The counter of that word would receive 40,000 events a second while others are idle.
Step 1: If split by topic
Common mistakes
The bugs interviewers watch for, with the fix beside each.
Merging top lists from a random split
criticalA topic that is just below the line on every worker may be first overall, and nobody sends it.
Wronglog.append(event) # any partitiontop = merge(w.top(10) for w in workers)Rightsums.send(key=(place, topic)) # one ownertop = merge(c.top(10) for c in counters)Merging sketches with the larger counter
criticalEach counter is a sum. Taking the larger one forgets the other worker's mentions.
Wrongcell = max(a.cell, b.cell)Rightcell = a.cell + b.cellListing topics from a sketch or a HyperLogLog
majorBoth forget names. They can answer a question about x, not tell you which x exist.
Wrongfor topic in sketch.topics(): ...Rightfor topic in exact_entries: ...Tumbling hours on a live list
majorAt one minute past, the list is made from one minute of data.
Wrongif now.minute == 0: counts.clear()Righttotal += newesttotal -= oldestUpdating a sorted set in place
minorTopics that left the list stay in it, and readers see half of an update.
WrongZADD trends:US:1h ...RightSET trends:US:1h <whole list, version>
Interview tips
Define it first
- Rising, not large.
- People, not posts.
- Which window and place.
Prove the merge
- One topic, one counter.
- Fewer than K beat it anywhere.
- Count the world as a place.
Size from the error
- eN/w for the width.
- Five rows for 99.3%.
- Exact is smaller for small places.
Expect opponents
- Once per account and hour.
- Hold, do not delete.
- Replay after a wrong rule.