In two sentences
Most pipelines receive the same record more than once: a retry re-sends an event, or an order is sent again every time its status changes. Deduplicating means keeping exactly one row per key, and choosing which one by a rule that gives the same answer every run.
See it
Order webhooks, received at least once:
| order_id | status | updated_at | received_at |
|---|---|---|---|
| 7 | pending | 10:00 | 10:00:02 |
| 7 | paid | 10:05 | 10:05:01 |
| 7 | paid | 10:05 | 10:09:30 ← retry |
| 8 | pending | 10:02 | 10:02:01 |
What we want, one row per order, the latest version:
| order_id | status | updated_at |
|---|---|---|
| 7 | paid | 10:05 |
| 8 | pending | 10:02 |
SELECT DISTINCT * doesn't do it: the retry differs in received_at, so all four rows are "distinct".
In SQL
SQL
SELECT order_id, status, updated_at
FROM (
SELECT *,
row_number() OVER (
PARTITION BY order_id
ORDER BY updated_at DESC, received_at DESC -- tie-breaker: same version, keep the last received
) AS rn
FROM order_events
)
WHERE rn = 1;Two different kinds of duplicates, two keys:
- Exact re-sends of one event: partition by the event's own id (
event_id). - Versions of one entity: partition by the entity's id (
order_id) and order by its version or update time.
When to use it
| situation | approach |
|---|---|
| the same event re-sent by a retry | dedup on the event's own id (event_id) |
| an entity sent again on every change, you need its current state | latest row per entity key, by version or updated_at |
| you need the entity's history, not just its current state | keep every version (an SCD type 2 table); only drop exact re-sends |
| a streaming job | dedup inside a time window (a watermark): keeping every key forever costs unbounded state |
Best practices
- Dedup once, early, in a known place. Do it in the first cleaned layer after landing (silver), not in every report downstream.
- Keep the raw duplicates where they landed. The landing table stays append-only, so you can audit a retry storm or replay with a different rule.
- Write the key and the ordering rule down in the table's description. "Unique on
order_id, latest byupdated_at, thenreceived_at" is a contract the next engineer can check. - Watch the duplicate rate. A sudden jump means something upstream is retrying in a loop.
Common mistakes
- No tie-breaker. Two rows with the same
updated_atand no second sort column:row_numberpicks one at random, and a re-run can pick the other. Results that change on re-run are a bug. max(updated_at)then join back. Two rows tied on the max both match, and the duplicates come back.GROUP BY order_idwithmax()on every column.max(status)andmax(updated_at)can come from different rows, building a row that never existed.- Ordering by arrival instead of by the event. Messages arrive out of order. The newest version is the one with
the highest
updated_at(or version number), not the last one received. - Deduplicating each batch only. A retry can land in tomorrow's batch. Dedup against what's already in the target too (see Idempotent loads and MERGE).
What an interviewer asks next
- "Why do duplicates happen at all?" At-least-once delivery: if the acknowledgement is lost, the sender retries. Exactly-once end to end is rare; dedup on read is the norm.
- "What if two versions have the same timestamp?" Use a sequence number from the source if there is one; otherwise a documented tie-breaker.
Go further
Confluent's page on delivery semantics explains at-most-once, at-least-once and exactly-once in one screen.
Practise it
Deduplicate an At-Least-Once Event Stream, then Flatten Nested Order Webhooks and Allocate Discounts to Lines.