Part 2 · Object storage · Chapter 4
Atomic commits on object stores
How a table format gets exactly one winner per version out of storage with no transactions: atomic rename, generation preconditions, a per-JVM lock, DynamoDB as an external arbiter, S3 conditional writes with write-id reconciliation, and catalogs that compare-and-swap a pointer.
After this chapter you should be able to:
- Name the commit primitive each design relies on (put-if-absent or compare-and-swap) and what provides it.
- Walk through S3SingleDriverLogStore, S3DynamoDBLogStore and S3LogStore, and say which interleavings each survives.
- Explain how S3LogStore resolves a lost acknowledgement, and when it gives up.
- Compare the Iceberg JDBC, Hive and Glue catalogs' compare-and-swap.
Chapter 3 ended with what S3 promises: one object at a time, atomically, with put-if-absent and compare-and-swap only since 2024. A table commit changes many objects. Both formats solve this the same way: make every commit end in one small decision that exactly one writer can win, and make everything else immutable files that nobody else can see until that decision names them. This chapter is about who makes that decision, on each kind of storage, and how the code makes it.
Links point to delta-io/delta at 25561a5 and apache/iceberg at 9d2bf51.
The primitive: compare-and-swap on a pointer
The general shape is a compare-and-swap (CAS) on a pointer: “if the table is still at version V, make it V+1; otherwise tell me I lost”. The two formats implement the pointer differently.
Delta has no pointer object. The current version is the highest N for which N.json exists, and writers “MUST never overwrite an existing log entry”.[PROTOCOL] Swapping from N-1 to N is creating N.json if it does not exist, so the primitive Delta needs from storage is put-if-absent, plus atomic visibility so nobody reads half a file. The Java LogStore interface says it in its contract: write with overwrite=false must throw FileAlreadyExistsException if the file exists, and if isPartialWriteVisible is false the file must appear all at once.[src]
Iceberg has a pointer. A commit writes a new metadata file and swaps the catalog’s pointer from the base metadata file to the new one; the swap must fail if the pointer moved.[spec] That is a true CAS, and it lives in the catalog, not the object store. Metadata files have unique names, so the store never has to arbitrate.
Either way, the loser learns it lost and does the format’s retry: Delta’s conflict check against the winning commits (chapter 10), Iceberg’s refresh and re-apply (chapter 17). The dangerous outcome is the third one: a writer told it won when it did not.
Q1. What plays the role of the "current version" pointer in a filesystem-based Delta table?
Delta LogStores
A LogStore is the class that turns “create N.json exactly once” into calls on one kind of storage. There is one per storage system.
Rename: HDFS and Azure
HDFSLogStore writes the commit to a hidden temp file .N.json.<uuid>.tmp, then calls FileContext.rename(temp, N.json, Rename.NONE). On HDFS the NameNode applies a rename as one metadata operation, and with Rename.NONE it fails if the destination exists. That one call gives both properties: the file appears atomically under its final name, and only one rename can win. The loser gets FileAlreadyExistsException and deletes its temp file.[src]
// HDFSLogStore.writeInternal, trimmed
if (!overwrite && fc.util().exists(path)) throw new FileAlreadyExistsException(path.toString());
Path tempPath = createTempPath(path); // .N.json.<uuid>.tmp
FSDataOutputStream stream = fc.create(tempPath, EnumSet.of(CreateFlag.CREATE), ...);
// ... write the actions, close ...
fc.rename(tempPath, path, overwrite ? Rename.OVERWRITE : Rename.NONE); // the decision
// finally: if the rename did not happen, fc.delete(tempPath)
The exists() check at the top is only a fast path. Two writers can both pass it; the rename is what decides. AzureLogStore does the same through HadoopFileSystemLogStore.writeWithRename, assuming that on Azure’s filesystem “Rename without overwrite is atomic”.[src][src]
Q1. Why does HDFSLogStore write a temp file and call FileContext.rename(temp, N.json, Rename.NONE)?
Q2. What does AzureLogStore assume about Azure's Hadoop filesystem?
Generation preconditions: GCS
Google Cloud Storage has preconditions on object generations, so GCSLogStore needs no rename. It calls fs.create(path, overwrite=false); the gcs-connector turns that into a create with a precondition, and if another writer created the object between create and close, the upload fails with an error whose cause chain contains "412 Precondition Failed". The store maps that to FileAlreadyExistsException. It also runs the write on a fresh thread, because an interrupted thread could otherwise upload an incomplete file.[src]
Q1. How does GCSLogStore notice that another writer created N.json while its own write was in flight?
S3 before conditional writes
Until 2024, S3 offered none of these: no rename, no precondition. S3SingleDriverLogStore does the only thing left, an existence check and a PUT, under a lock:[src]
// BaseS3LogStore.writeWithPathLock, trimmed
withPathLock(resolvedPath, () -> { // static PathLock: one per JVM
if (fs.exists(resolvedPath) && !overwrite) throw new FileAlreadyExistsException(...);
stream = fs.create(resolvedPath, overwrite); // plain PUT on close()
...
});
The PathLock is a static field, shared by every LogStore in the class loader, so all writers in one Spark driver are serialized: check-then-PUT is safe there. Across drivers there are two locks. Both exists() calls return false, both PUTs succeed, and S3 keeps the one with the later timestamp (chapter 3). The name is the warning: single driver.
Q1. Two notebooks in the same cluster (one Spark driver) commit to one table through S3SingleDriverLogStore. Is it safe?
S3 with DynamoDB
S3DynamoDBLogStore (Delta Lake 1.2.0, April 2022) gets put-if-absent from DynamoDB instead.[release] Its table has tablePath as the hash key and fileName as the range key; each item records the commit’s temp path and whether the commit is complete.[src] BaseExternalLogStore.write commits version N in four steps:[src]
- Step 0. Fail if
N.jsonalready exists. - Step 1. Make sure
N-1is complete: if its DynamoDB entry sayscomplete=false, finish that commit first (below). This keeps the log free of gaps. - Step 2, prepare. Write the actions to a temp file
T(N)=_delta_log/.tmp/N.json.<uuid>, thenputItemthe entryE(N, T(N), complete=false)with the condition thatfileNamedoes not exist yet. AConditionalCheckFailedExceptionbecomesFileAlreadyExistsException.[src][src] - Step 3, commit, and step 4, acknowledge. Copy
T(N)toN.json, then overwrite the entry withcomplete=trueand an expiry time. Errors here are logged and ignored: the version is already won.
The conditional putItem is the decision. Everything after it is deterministic, because the entry names the exact temp file. If the writer dies after step 2, the next reader finishes the commit: listFrom on a _delta_log path looks up the latest entry, and if it is incomplete, fixDeltaLog copies T(N) to N.json (unless it is already there) and marks the entry complete.[src][src] The writer of N+1 does the same in step 1.
The expiry is not arbitrary. A completed entry gets an expireTime one day out (configurable as ddb.ttl), for DynamoDB’s TTL to delete it, rather than being deleted at once. The source comment walks through the interleaving that a zero delay would allow: the entry disappears, a second writer’s exists(N.json) returns a stale false, and its copy overwrites the first commit. The comment also notes that this “isn’t possible on S3 (which provides strong consistency)”; the day is there for stores that do not.[src] Chapter 10 has a stepper for this store.
Q1. A writer's putItem of E(5, complete=false) succeeds, then the process dies before copying T(5) to 5.json. What happens to version 5?
Q2. Why does a completed DynamoDB entry expire a day later instead of being deleted at once?
S3 with conditional writes
S3LogStore is the S3 LogStore that uses S3’s own put-if-absent. Its javadoc lists what it needs: “Hadoop S3A 3.4.2 or later, conditional creation, user metadata and abortable, immediately visible output streams”, and it warns that “Mixing this store with unconditional writers in independent drivers is unsafe.”[src] The write itself is short:[src]
// S3ConditionalWrite.write, trimmed
String writeId = UUID.randomUUID().toString();
FSDataOutputStream stream = fs.createFile(path)
.overwrite(false)
.must(FS_OPTION_CREATE_CONDITIONAL_OVERWRITE, true) // If-None-Match: *
.must(WRITE_ID_OPTION, writeId) // x-amz-meta-delta-log-store-write-id
.build();
// write the actions; on any failure before close(), abort() so nothing is published
try { stream.close(); } // the conditional PUT completes here
catch (IOException failure) { abort(stream, failure); reconcile(fs, path, writeId, failure); }
A 412 (or 409) is a conflict: the store throws FileAlreadyExistsException, and the transaction retries at the next version as on any other storage. The new problem is a response that never arrives. The PUT may have committed, and the client only sees a timeout. Retrying the transaction at N+1 would commit it twice; treating it as a conflict would drop a commit that is in the table.
The write id resolves that. reconcile checks that N.json is a file, reads its delta-log-store-write-id metadata, and decides:[src]
- The id is this write’s: the commit landed. Return normally.
- The id is someone else’s, and the failure itself was a conditional failure (
RemoteFileChangedException, a HadoopFileAlreadyExistsException, or a service error with status 409 or 412): a conflict. - Anything else (another id after a timeout, a failed probe, an interrupt):
S3UnknownWriteOutcomeException, “the object may have been committed. Do not blindly retry the transaction.”
Unknown is a real outcome here, as it is for the Iceberg REST catalog’s 5xx responses (chapter 21). A store that never says “unknown” is guessing.
- write(5.json, actions, overwrite=false). DelegatingLogStore picks S3LogStore for s3/s3a/s3n paths when the filesystem advertises conditional create and custom headers; otherwise it falls back to S3SingleDriverLogStore and logs a warning. DelegatingLogStore ↗
- PUT 5.json If-None-Match: * x-amz-meta-delta-log-store-write-id: <uuid>. createFile(path).overwrite(false) with the conditional-overwrite option and a fresh write id as user metadata. Nothing is visible until close(). S3ConditionalWrite.write ↗
- 412 Precondition Failed. Another writer committed version 5 first. The store throws FileAlreadyExistsException, which OptimisticTransaction turns into a conflict check and a retry at version 6. isConditionalConflict ↗
- 200 OK … lost on the network. The other bad case: the PUT may have committed, but the client saw a timeout. Retrying blindly could commit the same transaction twice at a later version.
- HEAD 5.json; read x-amz-meta-delta-log-store-write-id. reconcile(): if the object on 5.json carries this write's id, the commit happened. If it carries another id and the failure was a 409/412, it is a conflict. reconcile ↗
- success, FileAlreadyExistsException, or S3UnknownWriteOutcomeException. Anything inconclusive (another owner after a timeout, a failed probe, an interrupt) is "Unknown outcome … Do not blindly retry the transaction." unknown ↗
Q1. Why does S3LogStore attach a random write id as S3 user metadata to every commit file?
Q2. A's conditional PUT times out. On HEAD, 5.json carries B's write id. What does S3LogStore throw?
Q3. One cluster uses S3LogStore and another, on an older Hadoop, falls back to S3SingleDriverLogStore. Why is that unsafe?
Which store you get
DelegatingLogStore picks a store per path scheme, in this order: a store configured for the scheme (spark.delta.logStore.<scheme>.impl); for s3, s3a and s3n, S3LogStore if the filesystem advertises conditional create and custom headers; the scheme’s default; and HDFSLogStore for everything else.[src] The scheme defaults are S3SingleDriverLogStore for S3, AzureLogStore for abfs, abfss, adl, wasb and wasbs, and GCSLogStore for gs.[src] When S3 falls back, it logs that conditional writes are unavailable and the single-driver store “cannot coordinate concurrent drivers”. S3DynamoDBLogStore is never chosen automatically; you configure it.
Q1. No LogStore is configured. A Delta table lives at s3a://bucket/t and the cluster's S3A does not support conditional create. Which LogStore does Delta use?
Iceberg catalogs as arbiters
Every Iceberg catalog implements the same doCommit(base, metadata): write the new metadata file, then swap the pointer if it still points at base’s file, else throw CommitFailedException so the caller refreshes and retries (chapter 17). They differ in who does the compare.
JDBC. One SQL statement is the CAS:[src]
UPDATE iceberg_tables
SET metadata_location = ?, previous_metadata_location = ?
WHERE catalog_name = ? AND table_namespace = ? AND table_name = ?
AND metadata_location = ? -- the base the writer started from
JdbcTableOperations first loads the row and checks that it still points at the base, then runs the update. One updated row is a commit; zero rows is CommitFailedException.[src] The check before the update is only a fast path: two writers can both pass it, and the WHERE clause is what lets one of them through.
Hive Metastore. By default HiveTableOperations takes an exclusive HMS lock on the table, reloads it, checks that metadata_location equals the base, and calls alter_table with the new location. The lock is heartbeated; if the heartbeat fails during the commit, another writer may have taken the lock, so the commit ends in CommitStateUnknownException.[src] With engine.hive.lock-enabled=false as a table property (or the matching Hive configuration setting), it skips the lock and passes the expected metadata_location to alter_table as expected_parameter_key and expected_parameter_value, so the metastore does the compare itself.[src][src]
AWS Glue. GlueTableOperations checks the location, then calls UpdateTable. Without a lock manager it sends the Glue table’s versionId, and Glue rejects the update with ConcurrentModificationException if the entry changed; Iceberg maps that to CommitFailedException. Unexpected failures go through a commit-status check that can end in CommitStateUnknownException.[src][src]
File-system tables. The oldest scheme writes v<N>.metadata.json with a rename, relying on an atomic rename that fails if the target exists. The spec marks it deprecated and “unsafe” in object stores and local file systems.[spec] HadoopTableOperations can wrap the rename in a LockManager, which helps only if every writer uses the same lock.[src]
Q1. What makes an Iceberg commit through the JDBC catalog atomic?
Q2. Which are true of HiveTableOperations.doCommit at the pin? (select all that apply)
Q3. Without a lock manager, how does GlueTableOperations keep two writers from both swapping the pointer?
Q4. Iceberg file-system tables commit by renaming a metadata file to v<N>.metadata.json. What does the spec say about them at the pin?
Catalogs as commit authorities
The newest designs move the decision out of storage entirely. A catalog-managed Delta table (chapter 14) is committed by asking the catalog to ratify version v; the catalog “must ratify version v at most once”, and only after v-1. Publishing v.json to _delta_log afterwards copies a decision that was already made, which is why a published file proves the version was ratified.[PROTOCOL] The Iceberg REST catalog (chapter 21) does the same for Iceberg: the client sends requirements and updates, and the server performs the swap with whatever its backing store offers.
Moving the decision to a catalog changes more than mutual exclusion. The catalog sees every commit, so it can enforce access, vend credentials and keep the table’s latest state without a LIST. The storage contract shrinks back to what S3 has always offered: write immutable objects atomically.
Q1. For a catalog-managed Delta table, what decides which commit is version v?
Try it
Two writers commit version 5 through the arbiter you pick. Each button press sends that writer’s next request. Steps link to the code they follow.
- S3SingleDriverLogStore, two JVMs. Press A twice (lock, exists), B twice, then A, B, A, B. Both writers are told they committed. The verdict shows whose commit is actually in
5.json. - Tick “both writers in one JVM” and do the same. B waits for A’s lock, then fails its
exists()check. - S3LogStore. Interleave the conditional PUTs: whoever finishes first wins and the other gets
412. Then tick “A’s PUT response is lost”: run A alone and it reconciles to success from its write id; let B win first and A ends withS3UnknownWriteOutcomeException. - S3DynamoDBLogStore. Interleave freely: only one
putItemsucceeds. Tick “A dies after claiming the version”, run A until it dies, run B (it loses version 5 to the dead writer), then press “A reader lists _delta_log/” and watch it finish A’s commit. - Iceberg JDBC. Run A and B to the validate step, then both UPDATEs. Both pass the check; one UPDATE matches zero rows.
- Iceberg Hive. B blocks on the HMS lock, then fails its location check.
Writer A · jvm-1
Requests · newest first
Try interleaving: one request from A, one from B, and so on. Then reset and run A to the end before B.
State
| S3 key | body |
|---|---|
00000000000000000004.json | commit 4 |
Comparison
| Design | Decision made by | Primitive | Survives two independent writers | Can end “unknown” |
|---|---|---|---|---|
Delta HDFSLogStore, AzureLogStore | Filesystem rename | put-if-absent (rename, no overwrite) | Yes | No |
Delta GCSLogStore | GCS precondition | put-if-absent | Yes | No |
Delta S3SingleDriverLogStore | A lock in one JVM | none across JVMs | No | No, it just loses commits |
Delta S3DynamoDBLogStore | DynamoDB conditional putItem | put-if-absent, outside S3 | Yes | Recovered by the next reader |
Delta S3LogStore | S3 If-None-Match: * | put-if-absent | Yes, if every writer uses it | Yes, S3UnknownWriteOutcomeException |
| Iceberg JDBC | Database UPDATE … WHERE | CAS on a row | Yes | SQL errors are rethrown, not classified |
| Iceberg Hive | HMS lock, or alter_table with an expected value | lock, or CAS | Yes | Yes, a lost lock heartbeat |
| Iceberg Glue | Glue versionId | CAS on the catalog entry | Yes | Yes, after unexpected failures |
| Catalog-managed Delta, Iceberg REST | The catalog service | ratify / CAS on the server | Yes | Yes, on 5xx |
Summary
- A commit is one decision that exactly one writer can win. Delta needs put-if-absent on
N.json; Iceberg needs compare-and-swap on the catalog’s pointer. - HDFS and Azure decide with an atomic rename; GCS with a generation precondition. S3 had neither until 2024.
S3SingleDriverLogStoreis safe only within one driver.S3DynamoDBLogStoremoves the decision to a DynamoDB conditional put and lets any reader finish a commit whose writer died.S3LogStoreusesIf-None-Match: *and a write id in S3 metadata to tell a lost response from a lost race, and reports “unknown” when it cannot tell. It is chosen automatically when S3A supports conditional create.- Iceberg catalogs compare in the catalog: a SQL
WHERE, an HMS lock or expected parameter, a Glue version id. Catalog-managed tables and REST catalogs make the catalog the authority for both formats.
Concepts in this chapter
Explained here
Primary sources
Review this chapter
This chapter has 16 quiz questions.