KatInterview

Rerun-Safe Monthly Churn Pipeline with Late Events

System DesignModel answer
MediumModel answerSystem Designetlidempotencyscd-type-2

Last updated

Design a daily pipeline that turns streaming subscription events into month-end churn and retention metrics, tolerating late data, safe reruns, 12-month backfills, and changing metric definitions.

Scenario

A subscription business emits a stream of events: start, upgrade, downgrade, cancel and reactivate. Each event has an event_id, subscription_id, customer_id, plan_id, mrr, an event timestamp and an ingestion timestamp. Events can show up as much as 3 days after they happened.

You own a daily batch job that must publish month-end metrics:

  • Logo churn
  • Gross revenue churn
  • Net revenue retention (NRR)

Requirements

  • Every run is idempotent: rerunning on the same inputs produces the same outputs and never double-applies an event.
  • You can backfill the last 12 months.
  • Plan attributes change over time, so the plan dimension is a Type 2 slowly changing dimension.
  • The pipeline has data quality checks that can block publication.
  • Results are reproducible, so a past number can be explained and regenerated.

What to cover

  1. DAG. Describe the tasks and their dependencies from raw events through curated snapshots to published metrics. State partitioning, clustering and keys for each table.
  2. Snapshot upsert. Write SQL (a MERGE or equivalent) that builds a monthly subscription snapshot from event-level changes. It must handle late cancels and reactivations correctly and must not double-apply anything on a rerun.
  3. Selective recompute. Explain how you detect which months a late event affects, recompute only those, and prove the recompute is correct using invariants.
  4. Metric versioning. Explain how historical reports stay interpretable when a metric definition changes (for example, via a semantic layer with versioned views).

State your assumptions about the schema before you start.

Hints

Hint 1

Avoid applying deltas to yesterday's output. If each snapshot row is a pure function of the deduplicated event history up to that month end, reruns are naturally safe.

Hint 2

A late event changes the state at its own month end and, because state carries forward, every later month too. Think about what range you must recompute and what high-water mark tells you where to start.

Hint 3

For metrics, define the cohort first: customers with MRR > 0 at the previous month end. Then logo churn, gross churn and NRR are all ratios over that same cohort, and invariants such as 'NRR = 1 - gross churn + expansion' become checkable.

Hint 4

For versioning, make the version part of the metric table key and expose both a pinned view and a 'current' alias, so old dashboards do not silently change.