KatInterview

Real-Time Event Tracking with Rolling Window Aggregations

System DesignModel answer
HardModel answerSystem Designstreamingtime-seriesaggregation

Last updated

Design a high-throughput pipeline that ingests behavioral events, deduplicates them, and serves accurate activity counts over 1-minute, 1-hour, and 24-hour sliding windows.

Background

Consumer platforms rely on near-real-time behavioral signals — things like content impressions, search clicks, follow actions, and page visits — to power fraud detection, personalization, and live engagement dashboards. You are asked to design a system that continuously captures these signals from client applications and answers low-latency queries about what a user did over the last minute, last hour, or last 24 hours.

Functional Requirements

  • Event ingestion: Accept events containing a user ID, an action type, a target resource ID, free-form metadata, and a timestamp generated on the client.
  • Deduplication: Guarantee that retried or replayed events are counted only once, using a per-event unique identifier supplied by the client.
  • Aggregation API: Expose an endpoint that returns either a count breakdown by action type or a list of raw recent events for a specific user, for any of three windows: past 1 minute, past 1 hour, past 24 hours.
  • Late arrivals: Correctly handle events that arrive out of order or up to 10 minutes after their client-side timestamp.

Non-Functional Requirements

  • Throughput: 500 million daily active users producing roughly 50 billion events per day — approximately 600,000 events per second on average, spiking to 1.5 million per second.
  • Freshness: A newly ingested event must be visible in query results within 5 seconds.
  • Read latency: P99 under 50 ms for the 1-minute window, under 200 ms for the 1-hour and 24-hour windows.
  • Durability and availability: No data loss once an event is acknowledged; 99.99% ingestion availability.

What to Cover

  1. API contracts — the ingestion endpoint and the query interface, including how you model windows and pagination.
  2. Pipeline architecture — the components between the client and the storage layer (message bus, stream processor, caches, persistent stores).
  3. Aggregation strategy — how pre-aggregation avoids full scans at read time, and how you handle window boundaries accurately.
  4. Deduplication and late-arrival mechanics — the state management and watermarking approach you would use.
  5. Scalability and trade-offs — partitioning choices, hot-key mitigation, and retention policies.

Hints

Hint 1

Start by sizing the problem numerically. 50B events/day works out to ~600K/s average and ~1.5M/s peak. Pin down event size (Protobuf/Avro at ~400 bytes) and figure out peak ingress bytes and dedup state size before drawing any components. Numbers should drive your architecture choices.

Hint 2

Avoid scanning raw events at query time. Instead, pre-aggregate in the stream processor into fixed-size time buckets (e.g., 5-second or 1-minute buckets) and store them in a tiered way: Redis for the hot short-term window, a wide-column store for longer retention. The query layer just sums whichever buckets fall inside the requested window.

Hint 3

Deduplication at 1.5M/s with a 10-minute window means you need to track roughly 900M event IDs concurrently. Consider where that state lives (local Flink RocksDB state vs. external Redis) and what the trade-offs are. Think about how late arrivals interact with already-emitted bucket counts and what side-output or reconciliation strategy you would use for events that fall outside the lateness bound…