Skip to content

Trending Topics / Top-K — Detailed#

flowchart TB
  subgraph Source
    EVT[Events: clicks, hashtags, queries]
    KAFKA[[Kafka]]
  end

  subgraph Stream[Stream layer]
    DECAY[Time decay applier]
    CMS[Count-Min Sketch<br/>per window]
    SS[Space-Saving / Heavy Keepers]
    SPIKE[Spike detector<br/>z-score / EWMA]
    WIN[Sliding windows 1m / 5m / 1h]
  end

  subgraph Storage
    TOPK[(Top-K materialized lists)]
    HIST[(Historical baselines)]
    DASH[(Dashboards)]
  end

  subgraph Serve
    API[Top-K API]
    PER[Personalization overlay]
    SAFE[Safety filter - banned trends]
  end

  EVT --> KAFKA --> Stream
  Stream --> Storage
  Storage --> Serve

    classDef client fill:#dbeafe,stroke:#1e40af,stroke-width:1px,color:#0f172a;
    classDef edge fill:#cffafe,stroke:#0e7490,stroke-width:1px,color:#0f172a;
    classDef service fill:#fef3c7,stroke:#92400e,stroke-width:1px,color:#0f172a;
    classDef datastore fill:#fee2e2,stroke:#991b1b,stroke-width:1px,color:#0f172a;
    classDef cache fill:#fed7aa,stroke:#9a3412,stroke-width:1px,color:#0f172a;
    classDef queue fill:#ede9fe,stroke:#5b21b6,stroke-width:1px,color:#0f172a;
    classDef compute fill:#d1fae5,stroke:#065f46,stroke-width:1px,color:#0f172a;
    classDef storage fill:#e5e7eb,stroke:#374151,stroke-width:1px,color:#0f172a;
    classDef external fill:#fce7f3,stroke:#9d174d,stroke-width:1px,color:#0f172a;
    classDef obs fill:#f3e8ff,stroke:#6b21a8,stroke-width:1px,color:#0f172a;
    class EVT,DECAY,CMS,SS,SPIKE,WIN,API,PER,SAFE service;
    class TOPK,HIST,DASH datastore;
    class KAFKA queue;

Algorithms#

  • Count-Min Sketch sized for ε, δ guarantees.
  • Space-Saving / Heavy Keepers: maintains top-K with bounded memory.
  • Decay: exponential decay on counts so old activity fades.
  • Spike detection: compare to historical baseline (z-score, EWMA control chart).

Glossary & fundamentals#

Concepts referenced in this design. Each row links to its canonical page; the tag column shows whether it is a high-level (HLD) or low-level (LLD) concept.

Tag Concept What it is Page
HLD Pub/Sub & message brokers topics, consumer groups, delivery semantics pub-sub-pattern
HLD Probabilistic data structures Bloom, HLL, Count-Min, MinHash, t-digest probabilistic-data-structures