cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Handling concurrent MERGE operations on the same Delta table

dataumer
New Contributor II

How do you handle situations where multiple Databricks jobs need to update the same Delta table at the same time?

Even when the jobs are updating different sets of records, concurrent modification errors can still occur.

Can row-level concurrency completely address this issue, or are there situations where we still need to implement retry logic or change the table design?

Scheduling all the jobs sequentially would significantly increase the overall processing time.

3 ACCEPTED SOLUTIONS

Accepted Solutions

arthurfr23
Contributor III

Row-level concurrency removes most of these errors, but it doesn't remove all of them. You still need retry logic. It only turns on when all of these are true:

  • Databricks Runtime 14.3 LTS or above
  • The target table is not partitioned (partitioned tables don't get it at all)
  • Deletion vectors are enabled on the table

If any of these is missing, conflicts are still detected per file. Two MERGEs that touch different rows in the same file will still conflict.

Even with row-level concurrency on, you can still get conflicts in these cases:

  • Two MERGE/UPDATE/DELETE jobs change the same row.
  • The MERGE condition uses complex types (structs, arrays, maps), non-deterministic expressions or subqueries. These fall back to normal file-level detection.
  • A metadata change runs at the same time, such as ALTER TABLE, a schema change or a protocol upgrade. This can make every concurrent write fail.
  • You run OPTIMIZE ... ZORDER BY at the same time as your writes.
  • The table has identity columns, which don't support concurrent transactions.

This is what I'd do:

ALTER TABLE target SET TBLPROPERTIES ('delta.enableDeletionVectors' = true);
-- prefer liquid clustering over partitioning for concurrent writers
ALTER TABLE target CLUSTER BY (business_key);
  1. Make each job's MERGE predicate explicitly filter the target to its own slice, for example AND t.region = 'EU'. That way each job only reads its own data.
  2. Keep a small retry with backoff anyway. Re-run the whole MERGE so it reads the latest snapshot:
import time, random
from delta.exceptions import ConcurrentAppendException, ConcurrentDeleteReadException

for attempt in range(5):
    try:
        run_merge()
        break
    except (ConcurrentAppendException, ConcurrentDeleteReadException):
        time.sleep(2 ** attempt + random.random())
else:
    raise
  1. Schedule OPTIMIZE with ZORDER and any schema changes outside the write window.

Docs: https://docs.databricks.com/aws/en/optimizations/isolation/row-level-concurrency

Can you share these details? - What's the exact exception (ConcurrentAppendException, ConcurrentDeleteReadException, etc.) and which runtime version are you on? - Is the target table partitioned, and are deletion vectors enabled? You can check with DESCRIBE DETAIL and SHOW TBLPROPERTIES.

Arthur Ferreira Reis

View solution in original post

balajij8
Esteemed Contributor III

Delta Lake uses optimistic concurrency control - the MERGE runs without locks. It reads a snapshot, rewrites the affected data files and only at commit time checks whether another transaction changed anything it touched. If two jobs rewrite the same data file, the commit check fails even when they update completely different rows because Delta's default conflict detection works at file granularity. A second failure mode is DELTA_CONCURRENT_APPEND - a MERGE whose scan overlaps files that another job appended after the snapshot began - so even disjoint jobs can collide when the merge condition can scan the whole table. These are correctness features and the losing transaction is rolled back to prevent data corruption.

You can run jobs in parallel safely though. Enable row level concurrency - it detects changes at row level and auto-resolves concurrent MERGE/UPDATE/DELETE jobs that touch different rows. It's active by default when the table is unpartitioned with deletion vectors enabled (or liquid clustered) on new runtimes. Note partitioned tables do not get row level concurrency. Use liquid clustering and keep an idempotent retry-with-backoff wrapper as a safety net to avoid conflicts.

-- enable deletion vectors
ALTER TABLE main.analytics.customers SET TBLPROPERTIES ('delta.enableDeletionVectors' = true)

-- liquid clustering
CREATE TABLE main.analytics.customers (
  customer_id BIGINT, status STRING, updated_at TIMESTAMP
) CLUSTER BY (customer_id)

 

View solution in original post

tajamulkhan
Esteemed Contributor
Hi

Delta Lake uses optimistic concurrency control, so concurrent MERGE operations can conflict even when they update different records, particularly when those records reside in the same underlying data files.

Row-level concurrency significantly reduces these conflicts, but it does not eliminate them completely.

1. Enable row-level concurrency

Row-level concurrency requires a supported Databricks Runtime and an unpartitioned Delta table with deletion vectors enabled.

ALTER TABLE catalog.schema.customers
SET TBLPROPERTIES (
    'delta.enableDeletionVectors' = 'true'
);

Liquid Clustered tables can also support row-level concurrency.

For example, if Job A updates customer 101 and Job B updates customer 102 within the same data file, row-level concurrency can allow both operations to succeed.

However, conflicts can still occur when both jobs modify the same row or when concurrent operations change table metadata.

I would recommend Databricks Runtime 14.3 LTS or above for this approach.

 
Databricks on Google Cloud

 

2. Implement bounded retry logic

Configure Lakeflow Jobs with a limited number of retries and a retry interval to handle temporary concurrency conflicts.

Ensure the MERGE operation is idempotent so retrying it does not introduce duplicates or overwrite newer records with older values.

3. Optimize the MERGE condition

For workloads operating on independent business regions, explicitly restrict the target scope.

MERGE INTO customers AS t
USING source_india AS s
ON t.customer_id = s.customer_id
AND t.region = 'India'

WHEN MATCHED THEN UPDATE SET *
WHEN NOT MATCHED THEN INSERT *;

This assumes the source contains only Indian customers and region is a stable part of the matching logic.

4. Consider a centralized MERGE pattern

If conflicts remain frequent, allow multiple jobs to write independently to staging tables and use a dedicated job to consolidate those changes into the target table.

My recommendation is to combine row-level concurrency, bounded retries and non-overlapping write scopes. There is no need to make the entire pipeline sequential just to avoid write conflicts.

Could you confirm whether the target table is partitioned and whether deletion vectors are enabled?

Official references:

 

View solution in original post

3 REPLIES 3

arthurfr23
Contributor III

Row-level concurrency removes most of these errors, but it doesn't remove all of them. You still need retry logic. It only turns on when all of these are true:

  • Databricks Runtime 14.3 LTS or above
  • The target table is not partitioned (partitioned tables don't get it at all)
  • Deletion vectors are enabled on the table

If any of these is missing, conflicts are still detected per file. Two MERGEs that touch different rows in the same file will still conflict.

Even with row-level concurrency on, you can still get conflicts in these cases:

  • Two MERGE/UPDATE/DELETE jobs change the same row.
  • The MERGE condition uses complex types (structs, arrays, maps), non-deterministic expressions or subqueries. These fall back to normal file-level detection.
  • A metadata change runs at the same time, such as ALTER TABLE, a schema change or a protocol upgrade. This can make every concurrent write fail.
  • You run OPTIMIZE ... ZORDER BY at the same time as your writes.
  • The table has identity columns, which don't support concurrent transactions.

This is what I'd do:

ALTER TABLE target SET TBLPROPERTIES ('delta.enableDeletionVectors' = true);
-- prefer liquid clustering over partitioning for concurrent writers
ALTER TABLE target CLUSTER BY (business_key);
  1. Make each job's MERGE predicate explicitly filter the target to its own slice, for example AND t.region = 'EU'. That way each job only reads its own data.
  2. Keep a small retry with backoff anyway. Re-run the whole MERGE so it reads the latest snapshot:
import time, random
from delta.exceptions import ConcurrentAppendException, ConcurrentDeleteReadException

for attempt in range(5):
    try:
        run_merge()
        break
    except (ConcurrentAppendException, ConcurrentDeleteReadException):
        time.sleep(2 ** attempt + random.random())
else:
    raise
  1. Schedule OPTIMIZE with ZORDER and any schema changes outside the write window.

Docs: https://docs.databricks.com/aws/en/optimizations/isolation/row-level-concurrency

Can you share these details? - What's the exact exception (ConcurrentAppendException, ConcurrentDeleteReadException, etc.) and which runtime version are you on? - Is the target table partitioned, and are deletion vectors enabled? You can check with DESCRIBE DETAIL and SHOW TBLPROPERTIES.

Arthur Ferreira Reis

balajij8
Esteemed Contributor III

Delta Lake uses optimistic concurrency control - the MERGE runs without locks. It reads a snapshot, rewrites the affected data files and only at commit time checks whether another transaction changed anything it touched. If two jobs rewrite the same data file, the commit check fails even when they update completely different rows because Delta's default conflict detection works at file granularity. A second failure mode is DELTA_CONCURRENT_APPEND - a MERGE whose scan overlaps files that another job appended after the snapshot began - so even disjoint jobs can collide when the merge condition can scan the whole table. These are correctness features and the losing transaction is rolled back to prevent data corruption.

You can run jobs in parallel safely though. Enable row level concurrency - it detects changes at row level and auto-resolves concurrent MERGE/UPDATE/DELETE jobs that touch different rows. It's active by default when the table is unpartitioned with deletion vectors enabled (or liquid clustered) on new runtimes. Note partitioned tables do not get row level concurrency. Use liquid clustering and keep an idempotent retry-with-backoff wrapper as a safety net to avoid conflicts.

-- enable deletion vectors
ALTER TABLE main.analytics.customers SET TBLPROPERTIES ('delta.enableDeletionVectors' = true)

-- liquid clustering
CREATE TABLE main.analytics.customers (
  customer_id BIGINT, status STRING, updated_at TIMESTAMP
) CLUSTER BY (customer_id)

 

tajamulkhan
Esteemed Contributor
Hi

Delta Lake uses optimistic concurrency control, so concurrent MERGE operations can conflict even when they update different records, particularly when those records reside in the same underlying data files.

Row-level concurrency significantly reduces these conflicts, but it does not eliminate them completely.

1. Enable row-level concurrency

Row-level concurrency requires a supported Databricks Runtime and an unpartitioned Delta table with deletion vectors enabled.

ALTER TABLE catalog.schema.customers
SET TBLPROPERTIES (
    'delta.enableDeletionVectors' = 'true'
);

Liquid Clustered tables can also support row-level concurrency.

For example, if Job A updates customer 101 and Job B updates customer 102 within the same data file, row-level concurrency can allow both operations to succeed.

However, conflicts can still occur when both jobs modify the same row or when concurrent operations change table metadata.

I would recommend Databricks Runtime 14.3 LTS or above for this approach.

 
Databricks on Google Cloud

 

2. Implement bounded retry logic

Configure Lakeflow Jobs with a limited number of retries and a retry interval to handle temporary concurrency conflicts.

Ensure the MERGE operation is idempotent so retrying it does not introduce duplicates or overwrite newer records with older values.

3. Optimize the MERGE condition

For workloads operating on independent business regions, explicitly restrict the target scope.

MERGE INTO customers AS t
USING source_india AS s
ON t.customer_id = s.customer_id
AND t.region = 'India'

WHEN MATCHED THEN UPDATE SET *
WHEN NOT MATCHED THEN INSERT *;

This assumes the source contains only Indian customers and region is a stable part of the matching logic.

4. Consider a centralized MERGE pattern

If conflicts remain frequent, allow multiple jobs to write independently to staging tables and use a dedicated job to consolidate those changes into the target table.

My recommendation is to combine row-level concurrency, bounded retries and non-overlapping write scopes. There is no need to make the entire pipeline sequential just to avoid write conflicts.

Could you confirm whether the target table is partitioned and whether deletion vectors are enabled?

Official references: