<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to avoid duplicate records when repairing failed Databricks Jobs? in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/how-to-avoid-duplicate-records-when-repairing-failed-databricks/m-p/177929#M57421</link>
    <description>&lt;P&gt;&lt;SPAN&gt;The best approach here is to make your pipeline idempotent rather than relying entirely on the job repair mechanism.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;When a Databricks task fails after successfully writing data to a Delta table, repairing the task does not roll back the previously committed transaction.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;Use MERGE instead of append&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;PRE&gt;MERGE INTO target_orders AS t
USING source_orders AS s
ON t.order_id = s.order_id

WHEN MATCHED AND s.updated_at &amp;gt; t.updated_at
THEN UPDATE SET *

WHEN NOT MATCHED
THEN INSERT *;&lt;/PRE&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;2. Separate independent operations into tasks&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;P&gt;&lt;SPAN&gt;Task 1: Extract data.&lt;/SPAN&gt;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/LI&gt;&lt;LI&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;P&gt;&lt;SPAN&gt;Task 2: Write to Delta.&lt;/SPAN&gt;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/LI&gt;&lt;LI&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;P&gt;&lt;SPAN&gt;Task 3: Downstream processing.&lt;/SPAN&gt;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;SPAN&gt;This allows you to repair the failed task without unnecessarily repeating successful operations.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;3. Maintain a batch audit table&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Track batch IDs, processing status and record counts for troubleshooting. However, an audit table alone does not guarantee exactly-once processing.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;My recommendation is to combine idempotent writes with task-level recovery.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Are you using batch processing or Structured Streaming with &lt;/SPAN&gt;&lt;SPAN&gt;foreachBatch&lt;/SPAN&gt;&lt;SPAN&gt;? The implementation would differ slightly.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;Official references:&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;P&gt;&lt;SPAN&gt;&lt;A href="https://learn.microsoft.com/azure/databricks/jobs/repair-job-failures?utm_source=chatgpt.com" target="_blank" rel="noopener"&gt;Repair failed jobs&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/LI&gt;&lt;LI&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;P&gt;&lt;SPAN&gt;&lt;A href="https://learn.microsoft.com/en-us/azure/databricks/delta/merge?utm_source=chatgpt.com" target="_blank" rel="noopener"&gt;Delta MERGE operations&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/LI&gt;&lt;/UL&gt;</description>
    <pubDate>Sat, 28 Nov 2026 11:51:30 GMT</pubDate>
    <dc:creator>tajamulkhan</dc:creator>
    <dc:date>2026-11-28T11:51:30Z</dc:date>
    <item>
      <title>How to avoid duplicate records when repairing failed Databricks Jobs?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-avoid-duplicate-records-when-repairing-failed-databricks/m-p/177907#M57416</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Suppose a job task successfully writes records to a Delta table but fails while performing a subsequent operation.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;When we repair the job, the failed task executes again and potentially inserts the same records.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;How do you normally handle such situations in production pipelines?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Is using MERGE instead of append sufficient, or would you recommend maintaining a separate audit table to track completed operations?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 28 Nov 2026 10:13:48 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-avoid-duplicate-records-when-repairing-failed-databricks/m-p/177907#M57416</guid>
      <dc:creator>alondatamvp</dc:creator>
      <dc:date>2026-11-28T10:13:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to avoid duplicate records when repairing failed Databricks Jobs?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-avoid-duplicate-records-when-repairing-failed-databricks/m-p/177929#M57421</link>
      <description>&lt;P&gt;&lt;SPAN&gt;The best approach here is to make your pipeline idempotent rather than relying entirely on the job repair mechanism.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;When a Databricks task fails after successfully writing data to a Delta table, repairing the task does not roll back the previously committed transaction.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;Use MERGE instead of append&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;PRE&gt;MERGE INTO target_orders AS t
USING source_orders AS s
ON t.order_id = s.order_id

WHEN MATCHED AND s.updated_at &amp;gt; t.updated_at
THEN UPDATE SET *

WHEN NOT MATCHED
THEN INSERT *;&lt;/PRE&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;2. Separate independent operations into tasks&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;P&gt;&lt;SPAN&gt;Task 1: Extract data.&lt;/SPAN&gt;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/LI&gt;&lt;LI&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;P&gt;&lt;SPAN&gt;Task 2: Write to Delta.&lt;/SPAN&gt;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/LI&gt;&lt;LI&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;P&gt;&lt;SPAN&gt;Task 3: Downstream processing.&lt;/SPAN&gt;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;SPAN&gt;This allows you to repair the failed task without unnecessarily repeating successful operations.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;3. Maintain a batch audit table&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Track batch IDs, processing status and record counts for troubleshooting. However, an audit table alone does not guarantee exactly-once processing.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;My recommendation is to combine idempotent writes with task-level recovery.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Are you using batch processing or Structured Streaming with &lt;/SPAN&gt;&lt;SPAN&gt;foreachBatch&lt;/SPAN&gt;&lt;SPAN&gt;? The implementation would differ slightly.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;Official references:&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;P&gt;&lt;SPAN&gt;&lt;A href="https://learn.microsoft.com/azure/databricks/jobs/repair-job-failures?utm_source=chatgpt.com" target="_blank" rel="noopener"&gt;Repair failed jobs&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/LI&gt;&lt;LI&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;P&gt;&lt;SPAN&gt;&lt;A href="https://learn.microsoft.com/en-us/azure/databricks/delta/merge?utm_source=chatgpt.com" target="_blank" rel="noopener"&gt;Delta MERGE operations&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/LI&gt;&lt;/UL&gt;</description>
      <pubDate>Sat, 28 Nov 2026 11:51:30 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-avoid-duplicate-records-when-repairing-failed-databricks/m-p/177929#M57421</guid>
      <dc:creator>tajamulkhan</dc:creator>
      <dc:date>2026-11-28T11:51:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to avoid duplicate records when repairing failed Databricks Jobs?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-avoid-duplicate-records-when-repairing-failed-databricks/m-p/178027#M57464</link>
      <description>&lt;P&gt;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 &lt;STRONG&gt;MERGE &lt;/STRONG&gt;keyed on a key, so re-inserting the same batch updates in place instead of appending duplicates.&amp;nbsp;For sources like Kafka/APIs, dedupe within the batch or use dropDuplicates before the write.&lt;/P&gt;&lt;P&gt;You can keep three mechanisms - &lt;STRONG&gt;MERGE&lt;/STRONG&gt;-&lt;STRONG&gt;upsert&lt;/STRONG&gt; instead of append for anything keyed, &lt;STRONG&gt;Push&lt;/STRONG&gt; 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,&amp;nbsp;for foreachBatch sinks - pass batch_id as an idempotency key so a retried batch cannot double write). As a &lt;STRONG&gt;recovery path&lt;/STRONG&gt;, Delta time travel&amp;nbsp;&lt;STRONG&gt;RESTORE &lt;/STRONG&gt;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.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;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 *&lt;/LI-CODE&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;P&gt;&lt;STRONG&gt;ROW_NUMBER&lt;/STRONG&gt; dedupe guards &lt;STRONG&gt;duplicate&lt;/STRONG&gt; 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.&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Sun, 29 Nov 2026 13:51:52 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-avoid-duplicate-records-when-repairing-failed-databricks/m-p/178027#M57464</guid>
      <dc:creator>balajij8</dc:creator>
      <dc:date>2026-11-29T13:51:52Z</dc:date>
    </item>
  </channel>
</rss>

