Exactly-once claims vs. end-to-end idempotency

Spark's internal guarantee and your sink's guarantee are two different promises, often conflated into one.

The rule

Spark's claim is real, but narrow: it covers Spark's own tracking. Spark will process each source offset exactly once according to its internal bookkeeping, even across restarts, as long as the is intact.

End-to-end is a separate, broader claim: that re-applying the same batch of output to the sink produces the same final state as applying it once. That depends entirely on how the sink is written to.

Why conflating the two costs you

A team that hears "Spark guarantees exactly-once" and stops there assumes the pipeline as a whole is exactly-once. But if the sink is a plain file append or an INSERT without a uniqueness constraint, replaying a micro-batch after a failure and restart writes the data again, producing duplicates in the sink even though Spark itself never reprocessed a source offset twice.

The two problems are genuinely different and need different fixes: duplicate sends from the source (the same logical event delivered twice upstream) are a problem, while reprocessing after a Spark-side failure and restart is an idempotency problem.

Two different problems

Duplicate upstream sendsReprocessing after restart
CauseSame event delivered twice by the sourceSpark restarts and replays a micro-batch
Fixed byDedup on a lifecycle-aware business keyIdempotent sink write (delete-then-rewrite or MERGE)
Spark's exactly-once offset tracking helps?No, this happens before Spark sees distinct events as duplicatesPartially, it tracks what Spark processed, but not what the sink durably received

A concrete worked example

A payments stream has two failure modes to solve independently. Duplicate sends: the same payment event arrives twice from upstream with identical (payment_id, lifecycle_stage). Dedup on exactly that composite key, with a watermark to bound the state needed to detect it, solves this.

Reprocessing after a failure: Spark restarts mid-batch and replays a micro-batch it had partially written to the sink. Tagging each row with its source file identifier and writing with a delete-then-rewrite pattern keyed on that identifier (or a MERGE keyed on the business key) solves this, since replaying the same batch produces the same final rows rather than appending a second copy.

Why these are the same mistake, twice

Both failure modes look identical from the outside: the sink ends up with duplicate rows. But the fix for one does nothing for the other. Deduping on event id will not catch a sink-side replay that wrote the exact same event id once (there is nothing to dedup against, it's one logical write happening twice at the storage layer). An idempotent sink write will not catch a true duplicate event sent twice by an upstream producer, since to the sink both are legitimately distinct Spark-side batches carrying the same business event.

Cost / gotcha

Fixing only one of these two problems leaves a pipeline that looks correct in testing and duplicates data in production under the failure mode you didn't cover.

Spark UI / verification evidence

Checkpoint directory offset logs show which source offsets Spark has committed, confirming the internal exactly-once claim. Verifying end-to-end idempotency requires testing at the sink: deliberately replay a committed micro-batch and confirm row counts and content in the sink are unchanged, which is not something the Spark UI shows directly.

Worked example

Deduplicate sends without discarding real updates

The sink holds one current row per order. It updates that row only when an incoming source version is newer.

DeliveryOrder / version / amountTarget amount
First event42 / v1 / 1010
Duplicate send42 / v1 / 1010
Legitimate update42 / v2 / 1515
Replay older event42 / v1 / 1015

What to notice: Order ID alone is too coarse for event deduplication: it would discard v2. Combine event/version identity with a sink rule that prevents older replays from overwriting newer state.

Practice / self-test

1. A team says "our pipeline is exactly-once because Spark guarantees it." What's missing from that claim?

Reveal answer

Spark's exactly-once guarantee covers its own offset tracking. It says nothing about what happens at the sink. Without an idempotent sink (delete-then-rewrite or MERGE), replaying a batch after a restart can still duplicate data there.

2. What's the difference between a duplicate upstream send and a Spark-side reprocess after failure?

Reveal answer

A duplicate upstream send is the same logical event delivered twice by the source before Spark ever sees it as two distinct batches; a reprocess is Spark itself replaying a micro-batch it already partially wrote, because it restarted mid-batch.

3. Why doesn't deduping on (payment_id, lifecycle_stage) protect against a sink-side replay?

Reveal answer

A sink-side replay re-applies the exact same already-deduped batch again. There's no duplicate for the dedup logic to catch, since from Spark's perspective it processed that batch's offset once; the duplication happens at the write to the sink, which dedup logic upstream of the sink never sees.

4. What makes a sink write idempotent against replay?

Reveal answer

A write pattern where re-applying the same batch produces the same final state as applying it once, such as delete-then-rewrite keyed on a stable identifier like a source file id, or MERGE keyed on a business key that overwrites rather than appends.

5. Why might deduping on payment_id alone (without lifecycle_stage) be wrong?

Reveal answer

A single payment can legitimately pass through multiple lifecycle stages (authorized, captured, refunded), each a distinct, valid event sharing the same payment_id. Deduping on payment_id alone would discard all but one of these legitimate stage transitions.