Concurrency control in Hudi: the lock provider that protects nothing
Two writers, one Hudi table, three configurations, measured. The default loses an entire commit silently. Turning on optimistic concurrency control with the lock provider that needs no infrastructure loses it just as silently. Only one of the three tells the losing writer that it lost.
- What “concurrent” means here
- The default loses a commit, silently
- Optimistic concurrency, and the provider that does nothing
- A lock that actually spans writers
- The three runs side by side
- What OCC actually does
- Choosing a lock provider
- Table services are writers too
- Non-blocking concurrency control
- Key takeaways
- References
- Trademarks
TL;DR
- Two concurrent writers against a Hudi table with the default
SINGLE_WRITERboth reported success, both commits landed on the timeline, and the table contained only one writer’s rows. The other 200 updates were gone, with no error anywhere.- Turning on
optimistic_concurrency_controlwithInProcessLockProviderchanged nothing: both writers committed, one writer’s data vanished, identical outcome to having no concurrency control at all.- That is not a bug.
InProcessLockProvideris a JVM-local mutex. Twospark-submits are two JVMs, so neither sees the other’s lock. It is also the provider people reach for, because it needs no infrastructure.- With
ZookeeperBasedLockProvider, the same test produced one commit and oneConcurrentModificationException. The losing writer knows it lost and can retry.- OCC is not a lock held for the duration of a write. Writers work freely and take a short lock at commit time to check whether what they touched moved underneath them.
- Hudi has three modes:
SINGLE_WRITER,OPTIMISTIC_CONCURRENCY_CONTROLandNON_BLOCKING_CONCURRENCY_CONTROL.
The Merge-on-Read post notes in passing that two concurrent jobs “can both succeed and one can lose its changes”. The tests below force that situation.
Three configurations, two genuinely concurrent spark-submit processes each time,
both writing the same 200 record keys into the same table. Only the concurrency configuration differs between runs.
What “concurrent” means here
Hudi coordinates writers through its timeline. A write creates an instant, moves
it through requested and inflight, and completes it. Two writers overlapping
in time are only a problem when they touch the same file groups, and with upserts on the same keys they always do.
The test is therefore the worst realistic case: same table, same keys, same partition, overlapping in time. Each writer is a separate JVM, because that is what two Spark applications are.
df = [(i, f"w{writer_id}", writer_id) for i in range(1, 201)]
df.write.format("hudi").options(**opts).mode("append").save(PATH)
The default loses a commit, silently
hoodie.write.concurrency.mode defaults to SINGLE_WRITER. Hudi assumes there is one writer and does not enforce it: nothing stops a second writer, and nothing tells you one arrived.
RESULT writer=1 mode=single OUTCOME=committed
RESULT writer=2 mode=single OUTCOME=committed
Both succeeded. Both are on the timeline, four milliseconds apart:
20260928043109150_20260928043355742.commit
20260928043109154_20260928043355529.commit
And the table:
TOTAL_ROWS 200
BY_WRITER [('w2', 200)]
COMMITS ['109154']
Writer 1’s commit exists on the timeline and none of its data is visible. Hudi acknowledged 200 updates to the application that made them and then discarded them. There was no exception, warning or failed job to alert on, so the only way to find out is to notice that the rows are wrong.
Every layer above Hudi reports success.
Optimistic concurrency, and the provider that does nothing
The documented fix is optimistic concurrency control:
"hoodie.write.concurrency.mode": "optimistic_concurrency_control",
"hoodie.write.lock.provider": "org.apache.hudi.client.transaction.lock.InProcessLockProvider",
"hoodie.cleaner.policy.failed.writes": "LAZY",
InProcessLockProvider is the one that appears in every quick-start, because it
is the only one that needs no infrastructure. Running the identical test:
RESULT writer=1 mode=occ_inproc OUTCOME=committed
RESULT writer=2 mode=occ_inproc OUTCOME=committed
TOTAL_ROWS 200
BY_WRITER [('w1', 200)]
It fails the same way. Both writers committed, 200 rows from one of them are gone, and nothing raised an error. Which writer loses depends on which commits last.
The class name gives the reason: the lock is held in process.
It is a mutex inside one JVM. Two spark-submit invocations are two JVMs, so
writer 1’s lock is invisible to writer 2 and vice versa. Each acquires “the” lock
instantly, sees no conflict, and commits.
Per-JVM locks do not coordinate writers in different applications.
The configuration looks correct, with the mode set, a provider named and OCC shown as on in the job logs, yet it protects nothing between applications. It is useful only for concurrent writers inside a single application.
A lock that actually spans writers
Swapping the provider for one backed by shared infrastructure, with everything else identical:
"hoodie.write.concurrency.mode": "optimistic_concurrency_control",
"hoodie.write.lock.provider": "org.apache.hudi.client.transaction.lock.ZookeeperBasedLockProvider",
"hoodie.write.lock.zookeeper.url": "zookeeper",
"hoodie.write.lock.zookeeper.port": "2181",
"hoodie.write.lock.zookeeper.base_path": "/hudi_locks",
"hoodie.write.lock.zookeeper.lock_key": "cc_demo",
"hoodie.cleaner.policy.failed.writes": "LAZY",
RESULT writer=2 mode=occ_zk OUTCOME=committed
RESULT writer=1 mode=occ_zk OUTCOME=failed(ConcurrentModificationException)
TOTAL_ROWS 200
BY_WRITER [('w2', 200)]
One writer committed. The other failed with a ConcurrentModificationException caused by HoodieWriteConflictException. The table holds a consistent result, and the
writer that lost knows it lost, so it can retry.
ZooKeeper shows the znode for the lock:
ls /hudi_locks -> [cc_demo]
The table contents have the same shape as in the broken runs: one writer’s 200 rows. What differs is that the losing writer was told. A table that looks fine is therefore not evidence that the configuration works.
The three runs side by side
| Mode | Lock provider | Writer 1 | Writer 2 | Table | Silent loss |
|---|---|---|---|---|---|
SINGLE_WRITER |
none | committed | committed | only w2 | yes |
optimistic_concurrency_control |
InProcessLockProvider |
committed | committed | only w1 | yes |
optimistic_concurrency_control |
ZookeeperBasedLockProvider |
failed | committed | only w2 | no |
Two of the three configurations lose data without telling anyone, including the OCC setup with the in-process provider.
What OCC actually does
The word “optimistic” describes when the lock is taken.
Writers do not hold a lock while they work. Each writes its files freely, and only at commit time takes a short lock to check whether the file groups it touched were modified by a commit that landed while it was working. If they were, it aborts.
The lock is held only for the conflict check and the commit.
Two consequences follow:
- Cheap when collisions are rare, expensive when they are common. Two writers touching disjoint partitions rarely conflict and rarely retry. Two writers upserting the same keys, as in this test, conflict every time, and the loser’s work is thrown away. If your writers always collide, OCC converts a correctness problem into a throughput problem; partitioning the work between writers is the better answer.
- The retry is yours. Hudi raises the exception; it does not re-run the write. A pipeline enabling OCC needs a retry path, and that retry must be safe to run twice.
hoodie.cleaner.policy.failed.writes set to LAZY belongs with OCC for a related
reason: with multiple writers, the eager policy would let one writer clean up
files belonging to another writer’s in-flight commit. Lazy cleaning defers that to
the cleaner, which can tell an abandoned write from a running one.
A conflict means overlapping file groups between the concurrent commits under the default conflict-resolution strategy. Writers in different partitions usually do not conflict; writers upserting the same keys always do.
Choosing a lock provider
The bundle ships several, and the choice is about where the lock is visible:
| Provider | Lock lives in | Use when |
|---|---|---|
InProcessLockProvider |
one JVM’s memory | concurrent writers inside one application; tests |
FileSystemBasedLockProvider |
a file on the table’s filesystem | simple setups on a filesystem with atomic create-if-absent semantics |
StorageBasedLockProvider |
object storage | object stores offering the conditional writes this needs |
ZookeeperBasedLockProvider |
ZooKeeper | you already run ZooKeeper |
HiveMetastoreBasedLockProvider |
the Hive Metastore | you already run one, which most Hudi deployments do |
DynamoDBBasedLockProvider |
DynamoDB | AWS, no ZooKeeper or metastore to lean on |
InProcessLockProvider protects less than people assume. The metastore and ZooKeeper providers often need no new infrastructure, because the platform already runs one of them, and moving from the broken configuration to a working one is then a class name and a few connection properties.
A caution on the filesystem provider: it depends on the underlying store giving atomic create-if-absent, which not every object store does with the semantics a lock needs. Prefer a provider whose backing store was designed for coordination.
If you can, avoid multiple writers. One writer per table, with table services in the same job, removes the problem and is the right default for most pipelines.
Table services are writers too
Compaction, clustering and cleaning modify the table, so they participate in all of the above. An asynchronous compaction running while an ingest job writes is the same two-writer situation tested above.
The common arrangement is one writer per table, with table services running inline or from a single dedicated job. It is less flexible and avoids conflicts. Where genuinely concurrent ingestion is required, enable OCC with a real lock provider and accept the retries.
Merge-on-Read does not remove conflicts under optimistic concurrency. Repeating the ZooKeeper test on a Merge-on-Read table gave the same result: both writers upserted the same file group, one committed, and the other failed with HoodieWriteConflictException. Letting writers append to one file group at the same time is what non-blocking concurrency control is for.
The metadata table is itself a Hudi table and is written as part of your commits, so it is subject to the same coordination. Configure concurrency control correctly before enabling anything that increases write contention.
Non-blocking concurrency control
Hudi 1.x adds a third mode. The enum in 1.1.1 confirms it:
SINGLE_WRITER
OPTIMISTIC_CONCURRENCY_CONTROL
NON_BLOCKING_CONCURRENCY_CONTROL
NBCC is designed to let concurrent writers proceed without taking a lock at all, resolving ordering at read and compaction time instead. The completion-time ordering of the 1.x timeline supports this, and it is the same mechanism behind the incremental-read behaviour covered separately. It carries requirements on table type and index that OCC does not.
The enum is verified here; NBCC’s behaviour is not. The measured scenarios use OCC or a single writer, so test NBCC against your own table type and index before relying on it.
Key takeaways
Two of the three configurations lost 200 rows without raising anything, and one of them was optimistic_concurrency_control with InProcessLockProvider.
In both failures there was no exception, failed job or alert, and both writers were told they had committed. The only symptom was wrong rows in the table, which nothing was checking.
Test your own setup: write the same keys from two separate processes and confirm that one of them fails. If both succeed, you do not have concurrency control, whatever the configuration says.
References
- Hudi concurrency control for the modes, lock providers and their configuration
- Hudi configurations for
hoodie.write.lock.*and the cleaner policies that accompany OCC WriteConcurrencyMode.javafor the three modesInProcessLockProvider.javafor what its scope actually is- Hudi Merge-on-Read for the table type and compaction
- Incremental processing in Hudi for the completion-time timeline ordering NBCC builds on
Trademarks
Apache Hudi, Apache Spark, Apache ZooKeeper, Apache Hive, Apache Iceberg, Apache and the Apache feather logo are either registered trademarks or trademarks of The Apache Software Foundation in the United States and other countries. Amazon DynamoDB is a trademark of Amazon.com, Inc. or its affiliates.
Found this useful?
These posts and tools are free. If one saved you an afternoon, you can buy me a coffee.
Comments
Questions, corrections and reviews are welcome. Sign in with GitHub to post; comments are stored in this site's public GitHub Discussions.