KatInterview

Real-Time Analytics Dashboard: Range Queries and Event-Time Completeness

System DesignModel answer
MediumModel answerSystem Designsystem-designstream-processingwatermarks

Last updated

Design a backend that turns a live event stream into dashboard metrics, serves arbitrary time-range queries from rollups, and uses watermarks to decide when event-time windows are final.

You are asked to design the backend of an analytics dashboard. Its charts are computed from a never-ending stream of events (for example page views, API calls, or purchases), and users can pick any time range to inspect.

Walk through your design as you would in a conversation with an interviewer. Two areas deserve particular depth:

  1. Range queries. How does the system answer "show me metric X between time A and time B" quickly, whether the range is the last 15 minutes or the last 90 days?
  2. Watermarks. How does the stream processor decide that a time window is complete when events can arrive late or out of order? What happens to events that show up after that decision?

The scope is deliberately underspecified. The metrics shown, event volume, freshness target, allowed lateness, retention, and number of dashboard viewers are all unknown. Ask clarifying questions or state explicit assumptions before committing to an architecture.

A strong answer should cover:

  • Your assumptions about metrics, freshness, lateness, and query shapes
  • An end-to-end pipeline: ingestion, windowed aggregation, storage, and a query service
  • Which metrics can be combined exactly across buckets (counts, sums) and which need special structures (percentiles, distinct counts)
  • What a watermark asserts, how it is generated, and the freshness-versus-completeness trade-off
  • Failure handling and observability: replay, idempotent writes, backfills, and monitoring of pipeline lag

Hints

Hint 1

Pin down the query shapes first. Short recent ranges want fine granularity, long ranges tolerate coarse granularity. That suggests storing the same metric at several resolutions.

Hint 2

Ask which metrics are mergeable. Sums and counts add across buckets, but a percentile of percentiles is wrong, and distinct counts cannot be summed. Think about mergeable sketches.

Hint 3

A watermark is a claim of the form 'no more events with event time earlier than T are expected'. Consider how it is derived (max observed event time minus a delay, per partition, then the minimum across partitions) and what you do with events that violate it.

Hint 4

Separate the speed path from the correction path: emit provisional results early, finalize on watermark, and reconcile later arrivals via updates or a batch backfill.