76. Drop Duplicate Events from a Retrying Producer
Difficulty: Medium · Topics: Streaming, Filtering & Selection
A mobile app sends events to events (event_id, user_id, ts). When a send times out, the app retries, so the same event can arrive twice, in the same batch or a later one. Two rows are the same event only when both event_id and ts match: the app reuses ids after a reinstall.
Write each event once. The stream runs for months, so Spark must not remember every event_id forever: use a 30-minute watermark on ts so old ids are forgotten. As a result, an event that arrives when the watermark has already passed its ts is dropped, duplicate or not.
Return event_id, user_id, ts in append mode.
The stream arrives in micro-batches: batch in the preview is the batch an event arrives in, not a column of the stream. The expected output lists every row your query writes, with batch set to the micro-batch after which it is written.
Row order does not matter; column names must match. Your code is graded on 3 test cases, including hidden edge cases.
Sample data
events
| batch | event_id | user_id | ts |
|---|---|---|---|
| 1 | e1 | u1 | 2024-05-01 10:00:00 |
| 1 | e2 | u2 | 2024-05-01 10:05:00 |
| 1 | e1 | u1 | 2024-05-01 10:00:00 |
| 2 | e3 | u1 | 2024-05-01 10:40:00 |
| 2 | e2 | u2 | 2024-05-01 10:05:00 |
| 3 | e4 | u3 | 2024-05-01 10:08:00 |
| 3 | e5 | u2 | 2024-05-01 10:45:00 |
| 3 | e6 | u1 | 2024-05-01 10:25:00 |
Expected output
| batch | event_id | user_id | ts |
|---|---|---|---|
| 1 | e1 | u1 | 2024-05-01 10:00:00 |
| 1 | e2 | u2 | 2024-05-01 10:05:00 |
| 2 | e3 | u1 | 2024-05-01 10:40:00 |
| 3 | e5 | u2 | 2024-05-01 10:45:00 |
| 3 | e6 | u1 | 2024-05-01 10:25:00 |
Hints
Hint 1
dropDuplicates([...]) on a stream keeps every key it has seen as state, so it can drop a copy that arrives in a later batch.Hint 2
The state is only cleaned up when the event-time column is part of the key: deduplicate on["event_id", "ts"] after withWatermark("ts", "30 minutes").Hint 3
Deduplicating onevent_id alone treats a reused id as a duplicate, and Spark can then never forget an id: its state grows forever.Learn the concepts
- Deduplicating a stream · 7 min read. Where duplicates come from, dropDuplicatesWithinWatermark, and deduplicating at the sink.
- Watermarks and late data · 8 min read. Event time vs processing time, how watermarks bound state, and what happens to late rows.
- dropDuplicates · 5 min read. Remove duplicate rows or keys, and why "keep the latest row" needs a window instead.
PySpark functions you'll practise
- dropDuplicates
- withWatermark
- writeStream
Related problems
- Click Counts in 5-Minute Windows with Late Data · Medium · Streaming, Aggregations, Filtering & Selection
- Hourly Revenue per Store, Updated Live · Medium · Streaming, Aggregations, Filtering & Selection
- Trending Searches in Sliding Windows · Hard · Streaming, Aggregations, Filtering & Selection
- Live Page View Counts · Easy · Streaming, Aggregations
- Enrich a Stream of Orders with Customer Data · Easy · Streaming, Joins, Filtering & Selection
Browse
Topics: Window Functions · Joins · Aggregations · Pivot, Unpivot & Rollup · Arrays · Null Handling · Conditional Logic · Dates · Filtering & Selection · Strings · Data Lake · Lakehouse · Streaming · Data Prep for AI · Spark Performance
Difficulty: Easy · Medium · Hard · Learn · All problems · dbt practice