How to Stop Duplicate Records When Data Pipelines Retry
Design retry-safe ingestion with stable keys, deterministic batch handling and idempotent upserts instead of relying on exactly-once assumptions.
What is actually going wrong?
Retries are essential for resilient pipelines, but a retry can replay records after a timeout where the source write succeeded and the orchestrator never received confirmation.
Symptoms teams commonly notice
- The same business event appears multiple times
- Counts increase after a failed-and-retried pipeline run
- A timeout is followed by a successful retry and doubled totals
- Different source extracts contain overlapping records
Why this problem happens
Use evidence to narrow the failure domain before applying a fix.
Explore the system flow
Select a stage to see what should happen and what evidence to inspect.
Source event
Stable event ID / business key
How to solve it reliably
A production solution should address the root cause, make failures observable and support safe recovery.
- 01Identify a stable business key or event ID and document its uniqueness scope.
- 02Persist a batch ID and source offset so each attempt can be traced and replayed safely.
- 03Land raw data append-only, then deduplicate deterministically in a curated layer.
- 04Use an upsert operation such as Delta MERGE with a deterministic source record per key.
- 05Reconcile inserted, updated, rejected and duplicate counts; retain rejected records for investigation.
-- Delta Lake pattern: deduplicate the incoming batch first
CREATE OR REPLACE TEMP VIEW source_dedup AS
SELECT * FROM (
SELECT *, ROW_NUMBER() OVER (
PARTITION BY order_id, line_id
ORDER BY source_updated_at DESC, ingest_timestamp DESC
) AS rn
FROM incoming_orders
) s WHERE rn = 1;
MERGE INTO curated_orders AS t
USING source_dedup AS s
ON t.order_id = s.order_id AND t.line_id = s.line_id
WHEN MATCHED AND s.source_updated_at > t.source_updated_at
THEN UPDATE SET *
WHEN NOT MATCHED THEN INSERT *;Illustrative starting point. Validate identifiers, data types, security and platform-specific syntax in your environment before production use.
Find the likely failure area
Choose the symptom closest to what you observe. This guided diagnostic suggests where to investigate first; it does not replace environment-specific testing.
Duplicates only after retries
Inspect partial commits and replay behaviour for the failed run ID.
Turn the fix into an operating practice
Follow the stages in order. Each step creates evidence for the next one.
Choose the idempotency key
Use source event ID or a composite business key with documented semantics.
Keep the problem from returning
- Choose the idempotency key: Use source event ID or a composite business key with documented semantics.
- Separate raw from curated: Keep immutable input and process it into a trusted target.
- Make writes repeatable: Use deterministic deduplication and transactional upserts.
- Test replay scenarios: Run the same batch twice and prove the curated result is unchanged.
Where we can help
Dealing with this challenge in your environment?
Share the symptoms, constraints and outcome you need. We can help scope a practical technical approach.