Can Spark Structured Streaming foreachBatch guarantee idempotent writes across retries?
0 reputation · 10 Feb 2021, 15:02 UTC
0 reputation · 10 Feb 2021, 15:02 UTC
Goal: configure a Spark Structured Streaming job so that retrying a failed micro-batch does not produce duplicate rows in the downstream sink.
The job uses foreachBatch to write to a table, and the checkpoint directory retains committed offsets. If a task fails after the sink acknowledges part of a batch but before the checkpoint advances, the next attempt may reprocess the same offset range. Whether this causes duplicates depends on the sink's idempotency and the Spark version in use.
Assumptions: Spark 3.x structured streaming is used, and the sink supports transactional commits or conditional writes. Behavior in Spark 2.x may differ because earlier versions lack the same exactly-once guarantees.
Can foreachBatch alone provide idempotent writes without sink-level deduplication? Does checkpoint offset commitment happen before or after the sink transaction commits? Which sink properties determine whether a replayed micro-batch is safely ignored?
35275 reputation · 10 Feb 2021, 18:00 UTC
No. foreachBatch alone does not guarantee idempotent writes across retries in Spark 3.x Structured Streaming. Spark treats the function as opaque user code and cannot roll back partial writes to an external sink. Idempotency must come from the sink, the write logic, or both.
foreachBatch returns successfully.foreachBatch bypasses that coordination.foreachBatch, not by Spark automatically.A failed micro-batch may have already written some rows to the downstream table. Because the checkpoint still holds the previous offset, the restarted job reprocesses that range and writes the same logical rows again. Without sink-level deduplication or conditional overwrite, the replay produces duplicate rows.
foreachBatch when one exists for your target system.Assumption: Spark 3.x Structured Streaming. Spark 2.x behavior differs and is not covered here.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.