Top K / Trending Topics System 00:00:01 Yesterday's biggest singer is not necessarily today's trend A music community wants fast-changing topics, not simply the largest lifetime totals. Build an illustrative ranking pipeline with an explicit time window, repeat handling and a definition of which observations carry weight. The post service sends 500 events, about 50 KB. One request each would cost more than the events. Keyed by account. All copies confirm, so a lost machine loses nothing. Accepted means stored, not yet counted. All events of one account reach the same filter, so a repeat within the hour is found locally. Locally combine eligible increments over the example's ten-second interval before forwarding partial counts. One counter owns this place and topic. It adds the sum to the right minute. Acceptance into a durable log precedes ranking. Bound the time window of deduplication and state what a published ranking means when late observations arrive. A fast list can still measure the wrong definition of trending. 00:01:02 A candidate must exist before it can be ranked A ranking job compares current attention with an ordinary baseline. A large sketch of counters does not automatically provide the names of the highest topics. Maintain a way to enumerate candidate identities as well as estimate their counts. A shorter timer is fresher and costs more work. Fifteen seconds fits the budget of 60. Only topics that passed the gate. A sketch cannot list its contents, entries can. Compare a candidate's current demand with its reference behavior over the defined baseline. Fewer than 1,000 different people is too small, whatever the growth. A heap holds the best so far, smallest on top. A better score replaces it. The ranker joins the lists. Because each topic has one counter, the true top 50 is inside. Apply a minimum-evidence rule before interpreting novelty. Build bounded local candidates under the chosen scoring rule, merge them and check which candidates an approximation could omit. 00:02:02 Two counters disagree about red A Count-Min Sketch hashes each positive update into one cell per row. Red has occurred three times. Blue collides with red in one row. Does that collision make the smaller estimate too low? All counters are zero. There will never be more than eight. One hash for each row. Two counters go up. The same two counters again. In row 1 blue shares red's counter. In row 2 it has its own. The counters say 4 and 3. The smaller is right: 3. Read blue's own chosen cell in each row and take the smaller count for its estimate. Under nonnegative updates, collisions add counts and the minimum across rows can reduce the inflation. Here the counters read four and three, so the estimate for red is three. An exact answer in one example is not an exactness guarantee. 00:02:53 Three named slots must remember a flood of topics A separate Space-Saving summary retains three named candidates with estimated counts. When every slot is occupied and an unknown topic arrives, the algorithm replaces a minimum counter. The new name inherits uncertainty. After nine mentions each line is exact. red again: one more mark. blue has no line. The smallest is green with one. Replace a minimum-count slot with the new identity and raise that estimated count by one. Every topic seen more than N divided by 3 times is on the board, and no mark is too low. For the standard unit-update algorithm with three counters, a topic occurring more than one third of all updates must be represented. Some represented candidates can still be below that threshold. Validate expensive actions with stronger evidence. 00:03:47 Midnight does not reset a sliding five-minute window Keep one count per minute and the sum of the active five minutes. When nine-oh-five begins, evict the nine-oh-zero cell before adding new mentions. Now let a delayed observation refer to nine-oh-two. Each minute has its own cell. Their sum is kept as one number. 8 more. The window 9:00 to 9:04 is full. At 9:05 the cell 9:00 leaves: 30 minus 4. 9 arrive in 9:05. Two mentions of 9:02 arrive late. Their cell is still here, so they count there. One for 9:00 goes to history only. If its bucket is still active under the lateness policy, amend that bucket and the total. An event outside the retained window can enter historical reconciliation instead. Candidate membership, estimated frequency and time membership are three separate sources of error.