KatInterview

Building Fast Daily Metrics Tables for a Photo App Dashboard

System DesignModel answer
HardModel answerSystem Designdata-modelingetlincremental-loads

Last updated

Design the derived tables and incremental pipeline behind a photo app's KPI dashboard, then extend it with cohort retention, weekly rollups and a change of reporting timezone.

Scenario

You own the analytics layer for a very large photo-sharing product. Leadership looks at a dashboard that shows one row per calendar day. It must load quickly, even though the raw data keeps growing without bound.

All raw timestamps are stored in UTC. The raw tables are:

  • users(user_id PK, username, created_at)
  • photos(photo_id PK, user_id, created_at)
  • follows(follower_user_id, followed_user_id, created_at): a row means the follow was created at created_at and stays active from then on.
  • user_events(event_id PK, user_id, event_type, occurred_at)

Days are initially defined in the America/Los_Angeles timezone.

Metrics (one value per report date, in the reporting timezone)

  • new_users: accounts created on that date, and its change versus 7 days earlier
  • DAU: distinct users with at least one user_events row that day, and its change versus 7 days earlier. Treat the exact definition of "active" as something you would confirm with stakeholders.
  • new_photos, its change versus 7 days earlier, and new_photos_per_DAU
  • total_followers: cumulative number of follow relationships at the end of the date
  • Share of users, as of end of date, with 0, 1-2, and 3+ inbound followers

What to cover

  1. Table design. Define the derived tables: grain, keys, columns, what is precomputed and what is computed at read time, partitioning or clustering, incremental loading, idempotency, late-arriving data, and the correctness-versus-cost trade-off.
  2. Daily refresh. Describe the ETL steps that populate these tables each day from the raw data (SQL or pseudocode is fine).
  3. Week-1 retention. For each signup cohort date D, what fraction of the cohort is active a week later, using the same definition of active? Explain the table changes and how to compute it cheaply.
  4. Weekly rollups. The dashboard must also support weekly reporting. Explain what changes in the grain and the computation, how "vs 7 days ago" becomes a weekly comparison, which metrics are additive and which are not, and how to treat a week that is not finished.
  5. New reporting timezone. A New York team wants America/New_York days. Explain what changes in storage, day boundaries, ETL windows, backfills, and support for several timezones, and how to avoid double counting or gaps at day edges.

Prioritize correctness, read performance, and maintainability.

Hints

Hint 1

Start by pinning down definitions: what counts as active, whether unfollows exist, what the denominator of the follower-bucket percentages is, and whether retention means exactly day 7 or any day in days 1 to 7.

Hint 2

Store additive counts in a small per-day table and compute ratios and week-over-week deltas in a view, so a late correction never leaves stale derived numbers. Put a per-user-per-day activity table underneath it to support DAU, weekly actives and retention without touching raw events.

Hint 3

Convert each local day to a half-open UTC interval (23 or 25 hours on DST days), overwrite whole partitions so reruns are idempotent, and re-process a trailing lookback window for late data. For timezone changes, make the timezone a key of every derived table rather than a hard-coded assumption.