Sub topic 13
Concurrent writes and conflict handling
This chapter assumes you know how the Delta transaction log records each commit as a numbered version. It also assumes you have run UPDATE, DELETE and MERGE on a Delta table. Here you will learn what happens when two jobs change the same table at once, which combinations conflict, and how to write jobs that survive a conflict.
How Delta handles two writers
Delta Lake uses optimistic concurrency control. A writer does not lock the table. It reads the current version, does its work, and tries to commit the next version number in the transaction log. If no other writer has committed in the meantime, the commit succeeds.
If another writer committed first, Delta checks whether the two sets of changes are compatible. If they are, your commit is quietly re-based onto the newest version. If not, your write fails with a conflict exception and none of its changes are applied. Because commits are atomic, a failed writer leaves the table unchanged.
Optimistic control works well for data pipelines because most concurrent writes touch different data. Appends from several jobs are the most common case, and they almost never conflict.
Isolation levels
The isolation level decides how strict the conflict check is. Delta offers two.
| Isolation level | What it guarantees | Default |
|---|---|---|
| `WriteSerializable` | Writes behave as if they ran one at a time. Reads of the table may see a state that no serial order of writes produced. | Yes |
| `Serializable` | Both reads and writes behave as if all operations ran one at a time. | No |
WriteSerializable allows more concurrency, so it conflicts less. In practice, the difference shows up when an UPDATE, DELETE or MERGE runs next to an INSERT. Under Serializable, the insert can conflict with the other operation. Under WriteSerializable, it does not.
You can set the level per table:
ALTER TABLE main.sales.orders
SET TBLPROPERTIES ('delta.isolationLevel' = 'Serializable');Choose Serializable only when your logic reads the table and then writes based on what it saw, and a missed concurrent insert would produce wrong results. Otherwise keep the default.
Which operations conflict
The table below summarizes the usual behavior with the default isolation level and no row-level concurrency.
| Operation A | Operation B | Result |
|---|---|---|
| `INSERT` or append | `INSERT` or append | No conflict |
| `INSERT` | `UPDATE`, `DELETE`, `MERGE` | No conflict under `WriteSerializable`, can conflict under `Serializable` |
| `UPDATE`, `DELETE`, `MERGE` | Same kinds of operation | Conflicts if both touch the same data files |
| `OPTIMIZE` | `UPDATE`, `DELETE`, `MERGE` | Can conflict if both touch the same files |
| Any write | `ALTER TABLE` that changes schema or properties | Can fail with a metadata conflict |
The key idea is file overlap. Delta stores data in immutable files. An UPDATE rewrites every file that contains a matching row. If two operations rewrite the same file, only one can win, so the other fails.
The conflict exceptions
When a conflict occurs, Delta raises a specific exception. The name tells you what collided.
| Exception | Typical cause |
|---|---|
| `ConcurrentAppendException` | Another writer added files in the area your operation read, such as a partition your `UPDATE` or `MERGE` scanned. |
| `ConcurrentDeleteReadException` | Another writer deleted or rewrote a file your operation read. |
| `ConcurrentDeleteDeleteException` | Two writers tried to delete or rewrite the same file. |
| `MetadataChangedException` | Another writer changed the table schema or properties while you were writing. |
| `ProtocolChangedException` | The table's protocol version was upgraded during your write. |
| `ConcurrentTransactionException` | Two streaming queries with the same checkpoint or application ID wrote to the table. |
The first three are the ones you will see most often. They all mean the same thing: your operation and another one wanted the same files.
Reducing conflicts with disjoint conditions
The most effective fix is to make each job's scope narrow and non-overlapping, and to say so in the statement. Suppose two jobs each merge one day of data into a table, and the table is laid out by event_date. If the merge condition does not mention the date, Delta must assume the merge may touch any file, so it conflicts with a concurrent merge for another day.
Add the date to the condition so the scopes are provably disjoint:
MERGE INTO main.sales.events AS t
USING main.sales.events_updates AS s
ON t.event_id = s.event_id
AND t.event_date = DATE'2026-12-03'
WHEN MATCHED THEN UPDATE SET *
WHEN NOT MATCHED THEN INSERT *;A second job that merges 2026-12-04 with the same pattern now touches different files, so both can commit. This only helps when the condition lines up with how the data is laid out, which is why partitioning or liquid clustering (covered earlier) affects concurrency as well as read speed.
Also make sure the source rows for each job fall inside the date you wrote in the condition. A source row outside that date would not match the condition and would be inserted as new, which creates duplicates.
Row-level concurrency
On recent Databricks Runtime versions (14.2 and above), row-level concurrency detects conflicts at the row level instead of the file level. It applies to tables that use deletion vectors, and to tables that use liquid clustering. Two UPDATE, DELETE or MERGE operations that change different rows in the same file can then both succeed, even when you cannot express the split in a condition.
Deletion vectors are enabled with a table property:
ALTER TABLE main.sales.orders
SET TBLPROPERTIES ('delta.enableDeletionVectors' = 'true');Row-level concurrency reduces conflicts but does not remove them. If two operations change the same row, one still fails. Operations that rewrite whole files, such as some OPTIMIZE runs, can also still conflict.
Retrying after a conflict
Some conflicts are expected on a busy table, so jobs that write often should retry. A retry works because the failed attempt left no changes. Re-running the whole statement reads the new table version and tries again.
import time
from delta.exceptions import DeltaConcurrentModificationException
from delta.tables import DeltaTable
def merge_with_retry(updates_df, max_attempts=4):
for attempt in range(1, max_attempts + 1):
try:
target = DeltaTable.forName(spark, "main.sales.orders")
(target.alias("t")
.merge(updates_df.alias("s"), "t.order_id = s.order_id")
.whenMatchedUpdateAll()
.whenNotMatchedInsertAll()
.execute())
return
except DeltaConcurrentModificationException:
if attempt == max_attempts:
raise
time.sleep(2 ** attempt)DeltaConcurrentModificationException is the base class for the exceptions in the table above, so one except clause covers them all. The wait grows with each attempt, which gives the competing job time to finish.
Retrying is safe here because MERGE is idempotent: running it twice with the same source gives the same result. A retry of a non-idempotent statement can apply a change twice. For example, UPDATE ... SET amount = amount + 10 that failed before commit is safe to retry, but only because the failed attempt wrote nothing. Do not add your own retry on top of a commit that succeeded but whose acknowledgement you lost, unless the statement is idempotent.
Design habits that prevent conflicts
- Keep appends and updates apart where you can. Land raw data with appends, and apply changes in a single scheduled
MERGEjob. - Run one writer per slice of data. Give each job its own date, region or tenant, and put that key in the condition.
- Schedule
OPTIMIZEoutside the busiest write windows, or run it on a slice that no other job is changing. - Avoid changing the schema or table properties while jobs are writing. A metadata change fails every writer that started before it.
- Give each streaming query its own checkpoint location so it has its own transaction identity.
- Make writes idempotent, so a retry never harms the data.
A conflict exception means your write did not happen. If a job swallows the error and carries on, downstream tables will silently miss that data. Let the exception propagate, or retry and then fail loudly.Exercise
Two scheduled jobs both run MERGE INTO main.sales.events at the same time, one for 2026-12-03 and one for 2026-12-04. The table is clustered by event_date, but both merge conditions only say t.event_id = s.event_id. One job fails with ConcurrentAppendException about once a day. What change to the statements is most likely to stop this, and why?
Show answer
Add the date to each merge condition, for example AND t.event_date = DATE'2026-12-03' in the first job and AND t.event_date = DATE'2026-12-04' in the second. Without it, Delta must assume each merge could touch any file, so the jobs overlap and one sees the other's added files. With the date, the two jobs provably touch different files, so both can commit. Wrapping each merge in a retry helps as well, but narrowing the scope removes the cause.
Exercise
A table uses the default isolation level. One job runs INSERT INTO the table while another runs DELETE on old rows. Do they conflict? What would change if the table were set to Serializable?
Show answer
Under the default WriteSerializable level they do not conflict, because an insert and a delete can be placed in a valid serial order. Under Serializable, the delete's read of the table can be invalidated by the concurrent insert, so the delete can fail with a conflict exception such as ConcurrentAppendException.