KatInterview

Exactly-Once Click Event Cleanup in a Streaming Lake Pipeline

System DesignModel answer
HardModel answerSystem Designsparkstructured-streamingdeduplication

Last updated

Design a Spark Structured Streaming job that removes retry-induced duplicate click events at 10M/s with a 24-hour window, handling late data, hot keys, idempotent writes, and backfills.

Scenario

An ad-tech platform collects click events from many trackers and pushes them into a message bus at roughly 10 million events per second. Every event carries:

  • event_id (a UUID)
  • timestamp (set by the tracker)
  • user_id
  • payload

Trackers retry on network failure, so the same event_id can show up more than once, each time with a different arrival time. You must build a Spark Structured Streaming job that removes these duplicates and writes the result to a table in an S3-based data lake.

Constraints

  • Ingest volume: 10M events/second.
  • The business requires duplicates to be eliminated across a 24-hour window.
  • The pipeline must support backfilling historical data without creating duplicates of rows already in production.

What to cover

  1. A deduplication strategy keyed on event_id that stays performant at this scale. Include a rough sizing of the state involved.
  2. How you treat late events that arrive after the deduplication window or watermark has already moved past them.
  3. How you deal with data skew, for example when certain event_id prefixes (or specific IDs) are far more frequent than others.
  4. How you make the sink idempotent, so that a retried or restarted Spark job does not insert duplicate rows in the destination.

Explain your trade-offs and name the pitfalls you would watch for.

Hints

Hint 1

Do the capacity math first: how many distinct event_ids exist in 24 hours, and roughly how many bytes of state does each one cost? Ask whether that fits in streaming state at all.

Hint 2

Consider two tiers: a cheap streaming dedup over a short watermark that catches almost all retries, plus a transactional insert-only check against the lake table for the long tail.

Hint 3

Think about what a random salt on the dedup key does to copies of the same event, and what truly causes skew when keys are UUIDs.

Hint 4

On a restart Spark replays the same batch with the same offsets. Ask what property the write needs so that a replay changes nothing, and how a backfill can reuse that same property.