shuffleet
Home
Practice
Problems
Lessons
Plans
Compete
Challenges
Interviews
Leaderboard
Progress
Skills
Analytics
Submissions
Account
Settings

Deduplication: the latest record per key

Junior · 3 min read

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_idstatusupdated_atreceived_at
7pending10:0010:00:02
7paid10:0510:05:01
7paid10:0510:09:30 ← retry
8pending10:0210:02:01

What we want, one row per order, the latest version:

order_idstatusupdated_at
7paid10:05
8pending10:02

SELECT DISTINCT * doesn't do it: the retry differs in received_at, so all four rows are "distinct".

One bucket per order_id, newest version first: only row number 1 leaves each bucket

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

Sources send duplicates, landing keeps them, the cleaned layer removes them once, reports trust the key

situationapproach
the same event re-sent by a retrydedup on the event's own id (event_id)
an entity sent again on every change, you need its current statelatest row per entity key, by version or updated_at
you need the entity's history, not just its current statekeep every version (an SCD type 2 table); only drop exact re-sends
a streaming jobdedup 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 by updated_at, then received_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_at and no second sort column: row_number picks 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_id with max() on every column. max(status) and max(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.