AlgoViz

Design a Top K / Trending Topics System

hard 45 min

Count 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.

Take in a batch

500 mentions arrive. They are stored first, then filtered and added up.

Step 1 of 10

Send a batch

Our systemPost serviceIngest APIKafkaby accountFiltersums 10 sCounterby topicS3checkpoints

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.

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

Kafkaby accountFilter AFilter BCounter 1red, orangeCounter 2blue, greenRanker
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

The bugs interviewers watch for, with the fix beside each.

  • Merging top lists from a random split

    critical

    A topic that is just below the line on every worker may be first overall, and nobody sends it.

    Wrong
    log.append(event)            # any partitiontop = merge(w.top(10) for w in workers)
    Right
    sums.send(key=(place, topic))   # one ownertop = merge(c.top(10) for c in counters)
  • Merging sketches with the larger counter

    critical

    Each counter is a sum. Taking the larger one forgets the other worker's mentions.

    Wrong
    cell = max(a.cell, b.cell)
    Right
    cell = a.cell + b.cell
  • Listing topics from a sketch or a HyperLogLog

    major

    Both forget names. They can answer a question about x, not tell you which x exist.

    Wrong
    for topic in sketch.topics(): ...
    Right
    for topic in exact_entries: ...
  • Tumbling hours on a live list

    major

    At one minute past, the list is made from one minute of data.

    Wrong
    if now.minute == 0: counts.clear()
    Right
    total += newesttotal -= oldest
  • Updating a sorted set in place

    minor

    Topics that left the list stay in it, and readers see half of an update.

    Wrong
    ZADD trends:US:1h ...
    Right
    SET trends:US:1h <whole list, version>
  • 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.