Sub topic 13
Concurrent Writes and Conflict Handling
This chapter assumes you know how the Delta transaction log records each commit as a new table version, and that you can write UPDATE, DELETE and MERGE statements. Here you will learn how Delta lets several writers change one table at once, when it rejects a write, and how to reduce conflicts.
Optimistic concurrency
Delta Lake does not lock a table while a job writes to it. It uses optimistic concurrency control. Each writer assumes no one else is changing the same data, and it checks that assumption only when it commits.
A write goes through these steps:
- The writer reads the latest table version and notes it as the starting point.
- The writer does its work, such as reading files and writing new data files.
- At commit time, the writer checks whether other commits landed after its starting version.
- If those commits do not conflict with what this writer read or changed, Delta commits the write as the next version.
- If they do conflict, the write fails with an exception.
Readers are never blocked. A query always sees one consistent version, even while writers commit new versions.
What counts as a conflict
Delta compares the files each transaction read and changed. Two writers conflict when they touch the same files in incompatible ways. Appends that only add new files rarely conflict with each other. Operations that rewrite existing files are the usual cause.
| Situation | Typical result |
|---|---|
| Two jobs append to the same table | Both commit |
| One job appends while another updates old rows | Usually both commit |
| Two jobs update or delete rows in the same files | One fails with a conflict |
| A job reads files that another job deleted or rewrote | The reader-writer fails |
| Two `MERGE` statements match rows in the same files | One fails with a conflict |
The exact rules also depend on the table's isolation level. The default, WriteSerializable, allows more combinations than Serializable. Conflicts often surface as ConcurrentAppendException, ConcurrentDeleteReadException, ConcurrentDeleteDeleteException or MetadataChangedException.
Row-level concurrency
On recent Databricks Runtime versions, tables that use liquid clustering or deletion vectors can detect conflicts at the row level instead of the file level. Two jobs that change different rows in the same file can then both commit. Partitioned tables get a similar benefit when concurrent jobs target different partitions. Check the Databricks documentation for the runtime and table features your workspace supports.
Reducing conflicts
Make each job's target explicit, so Delta can see that jobs do not overlap. For partitioned tables, put the partition column in the condition:
MERGE INTO main.sales.orders AS t
USING main.sales.orders_updates AS s
ON t.order_id = s.order_id AND t.order_date = '2026-12-03'
WHEN MATCHED THEN UPDATE SET *
WHEN NOT MATCHED THEN INSERT *;Other practical steps:
- Run only one job at a time against the same set of rows.
- Keep transactions short, so the window for other commits is small.
- Avoid running
OPTIMIZEover the same data a heavyMERGEis changing.
Retrying a failed write
A conflict exception means the write did not commit and the table is unchanged. It is safe to retry. A retry reads the new table version and tries again.
import time
from delta.exceptions import ConcurrentModificationException
def merge_with_retry(run_merge, attempts=3):
for attempt in range(1, attempts + 1):
try:
run_merge()
return
except ConcurrentModificationException:
if attempt == attempts:
raise
time.sleep(2 ** attempt)Pass in a function that runs your MERGE. Only retry operations that are safe to repeat. A MERGE keyed on order_id is, because running it again gives the same result.
Exercise
Two jobs run UPDATE on main.sales.orders at the same time. Job 1 changes rows where order_date = '2026-12-01'. Job 2 changes rows where order_date = '2026-12-02'. The table is not partitioned and has no deletion vectors. Why might one job fail, and what change makes both succeed?
Show answer
Without partitioning or row-level tracking, rows from both dates can sit in the same data files. Both jobs rewrite those files, so the second commit conflicts with the first. Partitioning the table by order_date, or using liquid clustering with deletion vectors, and including the date in each job's condition lets Delta see that the jobs touch different data. Both can then commit.