KatInterview

Design a Memory-Bounded Real-Time URL Analytics Engine

System DesignModel answer
MediumModel answerSystem Designsystem-designstreamingdata-structures

Last updated

Architect a memory-efficient system to compute exact all-time and probabilistic 24-hour rolling Top-K URLs from high-velocity web request logs.

Context

A global content delivery network handles hundreds of thousands of HTTP requests per second. To provide actionable telemetry and dynamic firewall rules, you need to build a stream processing platform that continuously tracks and surfaces the most heavily accessed URLs.

Requirements

Functional

  • Global Top-K: Query GetGlobalTopK(k) returning the top $k$ most accessed URLs since system inception ($k \le 1{,}000$).
  • Rolling Top-K: Query GetRollingTopK(k, window=24h) returning the top $k$ most accessed URLs within the trailing 24 hours ($k \le 1{,}000$).
  • Event Ingestion: Process an unbounded stream of log entries formatted as (url: String, timestamp: EpochMs, user_agent: String, client_ip: String).

Non-Functional & Operational

  • Peak Throughput: $100{,}000$ events/second ($\approx 8.6$ billion events/day).
  • High Cardinality: Up to 500 million unique URL strings globally.
  • Resource Limits: Worker nodes are constrained to 4 GB of physical RAM each. The complete URL state space cannot fit in system memory.
  • Latency & Freshness: Read queries must return in under $100\text{ ms}$. Rolling window output must reflect events processed within the last $1\text{--}5$ minutes.
  • Out-of-Order Events: Gracefully process events arriving up to 15 minutes out of sequence.
  • Accuracy Constraints: Provide guaranteed bounded error approximations where probabilistic structures are used, and define exact tracking bounds where required.

Key Areas to Cover

  1. System Topology & Data Pipeline: Map the high-level architecture from ingestion (e.g., Kafka) to stream aggregation (e.g., Flink) and serving storage.
  2. Memory-Constrained Counting Architecture: Compare exact disk-backed state (LSM-trees / RocksDB) against bounded-memory sketches (Space-Saving, Count-Min Sketch). Define key partition strategies that protect nodes from memory exhaustion.
  3. Sliding Window Processing: Detail how the 24-hour sliding window is discretized into discrete micro-buckets, how state is expired, and how bucket summaries are merged.
  4. Fault Tolerance & Skew: Explain how to address hot key distributions (e.g., viral URLs) and maintain consistency using watermarks and checkpoints.

Hints

Hint 1

Estimate the size of the full global state space (500M unique URLs) to prove why pure in-memory hash maps fail on a single node.

Hint 2

Partitioning incoming traffic by URL hash guarantees that all instances of a specific URL land on the exact same stream processing worker.

Hint 3

To handle a 24-hour sliding window without high memory churn, discretize time into 5-minute fixed buckets and store mergeable sketches like Space-Saving rather than raw events.