- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Saturday
Suppose a job task successfully writes records to a Delta table but fails while performing a subsequent operation.
When we repair the job, the failed task executes again and potentially inserts the same records.
How do you normally handle such situations in production pipelines?
Is using MERGE instead of append sufficient, or would you recommend maintaining a separate audit table to track completed operations?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Saturday
The best approach here is to make your pipeline idempotent rather than relying entirely on the job repair mechanism.
When a Databricks task fails after successfully writing data to a Delta table, repairing the task does not roll back the previously committed transaction.
Use MERGE instead of append
MERGE INTO target_orders AS t USING source_orders AS s ON t.order_id = s.order_id WHEN MATCHED AND s.updated_at > t.updated_at THEN UPDATE SET * WHEN NOT MATCHED THEN INSERT *;
For example, if a task inserts 1,000 orders and fails during downstream processing, rerunning it will not insert those same orders again, assuming unique, non-null business keys in both tables.
2. Separate independent operations into tasks
Task 1: Extract data.
Task 2: Write to Delta.
Task 3: Downstream processing.
This allows you to repair the failed task without unnecessarily repeating successful operations.
3. Maintain a batch audit table
Track batch IDs, processing status and record counts for troubleshooting. However, an audit table alone does not guarantee exactly-once processing.
My recommendation is to combine idempotent writes with task-level recovery.
Are you using batch processing or Structured Streaming with foreachBatch? The implementation would differ slightly.
Official references:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Sunday
It happens because a job task isn't a transaction boundary - it's just a container of code. When a task writes to a Delta table and then fails on a later operation (API call, notify step), the write has already committed as a Delta version and the task is marked failed. You need to make each write idempotent by design, so a rerun converges to the same result instead of duplicating. The right pattern is a MERGE keyed on a key, so re-inserting the same batch updates in place instead of appending duplicates. For sources like Kafka/APIs, dedupe within the batch or use dropDuplicates before the write.
You can keep three mechanisms - MERGE-upsert instead of append for anything keyed, Push stateful/incremental logic into streaming tables or Lakeflow pipelines, which gives exactly-once processing by combining Structured Streaming checkpoints with Delta's transactional commits (retried batch fully succeeds or fully rolls back, for foreachBatch sinks - pass batch_id as an idempotency key so a retried batch cannot double write). As a recovery path, Delta time travel RESTORE to roll a table back to the version before a bad run, fix the pipeline and rerun. If the writes are keyed MERGE-upserts, repair runs become safe - the rerun simply matches existing keys and completes run, duplicates are almost impossible and you may not need any audit table.
MERGE INTO main.sales.transactions AS t
USING (
SELECT * FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY order_id ORDER BY _ingest_ts DESC) rn
FROM main.staging.daily_orders
) WHERE rn = 1 -- dedupe within the batch
) AS s
ON t.order_id = s.order_id
WHEN MATCHED THEN UPDATE SET * -- rerun
WHEN NOT MATCHED THEN INSERT *ROW_NUMBER dedupe guards duplicate records within a single batch - the ON t.order_id match guards duplicates across runs and together, a repair run can execute the task twice without duplicating a row.