Two Spark jobs write to the same table in table format Iceberg (format v2, a REST catalog). An hourly job appends that hour's events; a nightly job runs DELETE for users who asked to be erased and rewrites the affected files (copy-on-write). Both often start from the same snapshot S and commit minutes apart. Last week an hourly run's commit call timed out after its data files were written, the scheduler reran the whole job, and that hour's events now appear twice. Explain what happens when the second of two writers that started from S commits, which conflicts Iceberg resolves by itself and which end in failure, and how you would make a rerun after an unknown commit outcome safe.
The first commit moves the pointer off S, so the second's check-and-put fails; it refreshes, re-validates, retries (4 by default). Appends rebase; a serializable delete fails if another commit rewrote its files or added matching ones. After a timeout, look for its snapshot.
- Rerunning an append after a commit timeout without checking whether its snapshot landed
- Believing concurrent Iceberg writers silently overwrite each other's commits
- Expecting every conflict to resolve on retry, including a delete whose files another commit rewrote
- How would you record a batch id so a rerun can tell whether its snapshot already landed?
- Why does a partition overwrite or a MERGE by event id make a rerun of the hour safe?
Optimistic concurrency. A writer takes no locks. It reads the current metadata.json (snapshot S), writes data files, manifests and a new metadata.json, then asks the catalog to swap the pointer "from version S to mine" (check-and-put). If another writer got there first, the pointer is no longer at S, the swap fails, and nothing the second writer wrote becomes visible.
Retry. The loser refreshes the table and checks whether its changes still hold on top of the new snapshot, then tries again — up to commit.retry.num-retries times (4 by default):
- an append of new files read nothing from the table, so it simply rebases onto the new snapshot and commits;
- a copy-on-write delete replaces specific files, and Spark validates it at
write.delete.isolation-level(serializableby default): validation fails if another commit deleted or rewrote those files, or added data files that may contain rows matching the delete condition. The hourly append can bring new events of the erased users, so a nightlyDELETEthat commits second usually fails. Undersnapshotisolation only the first check remains. A failed delete is rerun from the current snapshot.
Unknown outcome. A commit call that times out does not say whether the pointer moved: the catalog may have applied the swap and the reply got lost (the Iceberg Java client singles this case out as CommitStateUnknownException). Blindly rerunning an append when the snapshot already landed is exactly what doubled the hour. Options:
- Write a batch id into the snapshot summary and check it before a rerun:
-- write side: .option("snapshot-property.batch-id", "2026-11-07T13")
SELECT snapshot_id, committed_at
FROM lake.events.snapshots
WHERE summary['batch-id'] = '2026-11-07T13';- Make the write idempotent:
MERGE INTO … ON t.event_id = s.event_id, or an overwrite of the hour's partition (dynamicINSERT OVERWRITE), so a rerun replaces data instead of appending it.