Apache Hudi table maintenance: compaction, cleaning, and why a read-optimized query goes stale
A Hudi Merge-on-Read table defers work: updates land in log files and reads merge them. Compaction pays that debt, cleaning removes what it leaves behind, and until compaction runs a read-optimized query returns stale values. Here is each step measured file by file.
- Why does a Hudi table need maintenance at all?
- What Hudi keeps
- Which operation fixes which symptom
- What a Merge-on-Read table defers
- What to schedule, and how often
- Symptoms and causes
- Key takeaways
- References
- Trademarks
TL;DR
- In Hudi Merge-on-Read, a read-optimized query returns stale values until compaction runs. The same table summed to 20,000 read-optimized and 40,000 on a snapshot read, from identical row counts.
- Hudi compaction writes new base files and leaves the old base and log files in place: base file count went 4 → 8 while log files stayed at 16. Cleaning is the separate step that removes them.
- A Spark batch writer turns inline compaction on when you leave it unset, so check what your tables actually do before relying on either default.
- Hudi cleans by default. Maintenance that nobody runs is still the most common lakehouse failure, so know which jobs your writer runs for you.
- For the Iceberg equivalents, see Apache Iceberg table maintenance.
Why does a Hudi table need maintenance at all?
A Hudi write does not modify existing files in place. It adds new ones and records the change on the timeline, so snapshot isolation and incremental queries work. The consequence is the same as in any lakehouse table: the files a table no longer needs do not go anywhere on their own.
Two costs accumulate. In a Merge-on-Read table, updates are written as log files next to the base file, so a reader has to merge more and more of them. And every file slice a compaction replaces stays on disk until cleaning removes it.
Maintenance repairs both, and they are different operations: doing the first does not do the second.
Everything below was run and measured: Hudi 1.2.0 on Spark 3.5.9, writing to a local warehouse.
What Hudi keeps
Timeline (.hoodie/)
|
Instants: deltacommit, commit, compaction, clean
|
File Group
|
File Slice
|
Base file (parquet) + Log files
Records are grouped into file groups. Each slice holds a base file and, for Merge-on-Read tables, log files carrying updates that have not yet been merged in. The timeline records every operation as an instant, which is how a reader tells a finished write from one still in progress.
Which operation fixes which symptom
| Symptom | Operation |
|---|---|
| Log files accumulating, reads merge more and more | compaction |
| Read-optimized query returns old values | compaction |
| Many small base files | clustering |
| Storage grows after compaction | cleaning |
| A failed write left residue | rollback of the failed commit |
| Timeline growing very long | timeline archival |
What a Merge-on-Read table defers
A Merge-on-Read table takes the opposite trade. Updates are written as log files next to the base file rather than rewriting it, so writes are cheap and reads must do the merging.
After an initial insert of 20,000 rows across 4 partitions, then four rounds of
upserts touching 5,000 rows each, with hoodie.compact.inline=false on every
write:
after initial insert base=4 log=0 instants=4
after 4 upsert rounds base=4 log=16 instants=16
Sixteen log files: four partitions times four upsert rounds. The base files were never rewritten. The timeline records what happened:
deltacommit 5
deltacommit.inflight 5
deltacommit.requested 5
The setting matters. A Spark batch writer turns inline compaction on when you leave it unset, and the same five writes then end with a compaction of their own, so there is nothing stale to show.
deltacommit is the Merge-on-Read write instant. Each one exists in three
states, requested then inflight then completed, which is how a reader tells a
finished write from one still in progress.
The read-optimized query is stale, and here is the proof
Two query types read the same table. A snapshot query merges base files with their log files; a read-optimized query reads only the base files.
| Query type | rows | sum(ts) |
|---|---|---|
| snapshot | 20,000 | 40,000 |
| read_optimized | 20,000 | 20,000 |
The row counts are identical, because the upserts updated existing records
rather than adding new ones. The values are not. Every record began with
ts = 1; 5,000 of them were updated to ts = 5, giving the correct sum of
15000×1 + 5000×5 = 40000. The read-optimized query returned 20,000, which is
20000×1: it saw none of the updates.
A row count will never reveal this. Anyone validating a read-optimized pipeline by comparing counts will find them matching and conclude the data is current.
Compaction merges the logs in
CALL run_compaction(op => 'run', table => 't3')
timestamp operation_size state
20260923050058927 4 COMPLETED
And the reads afterwards:
| base files | log files | snapshot sum(ts) |
read_optimized sum(ts) |
|
|---|---|---|---|---|
| Before | 4 | 16 | 40,000 | 20,000 |
| After | 8 | 16 | 40,000 | 40,000 |
Two things to take from that table. The read-optimized query now agrees, which is what compaction is for. And the file counts moved the way Iceberg’s did: base files went from 4 to 8 and the log files did not go anywhere. Compaction wrote new base slices; the old base files and the log files that fed them remain until cleaning removes them.
Compaction also writes a commit instant rather than a deltacommit, so the
timeline distinguishes the merge from the writes that made it necessary.
Cleaning, and what I could not demonstrate
Cleaning is Hudi’s equivalent of snapshot expiry: it removes file slice versions older than the retention policy.
CALL run_clean(table => 't3', retain_commits => 1)
This returned an empty result (no rows, and no files deleted) with
hoodie.cleaner.commits.retained set to 1 and cleaning triggered manually. The
base and log file counts were unchanged afterwards. I was not able to establish
from this run whether the retention policy genuinely had nothing eligible or
whether the procedure needed different arguments, and I am reporting it as
unresolved rather than describing behaviour I did not observe. The Iceberg
expiry above was measured end to end; this one was not.
What is certain from the file counts is the structural point, which matches Iceberg exactly: compaction alone left every pre-compaction file on disk.
What to schedule, and how often
Compaction: as often as log files accumulate. The signal is log files per file group and how stale your read-optimized consumers can tolerate being, not a calendar.
Cleaning: as often as compaction, and never less. It controls storage cost, and its retention is a real recovery capability: the file versions cleaning keeps are the ones an incremental or point-in-time query can still reach.
Archival: when the timeline gets long. A long active timeline slows every operation that reads it.
Sequence them. Compact first, then clean. Cleaning before compacting keeps the log files as the current state, and the compaction that follows creates a new generation of files that nothing yet references.
Maintenance jobs are writers. Compaction and cleaning commit to the same timeline as ingestion, so decide deliberately whether they run inline with the writer or as a separate job with its own compute.
Symptoms and causes
Storage grows while the table’s row count is flat. Cleaning is not removing what compaction left behind.
A read-optimized query returns old values. Compaction has not run for those
file groups. This is by design. Check that compaction is scheduled, or that no
writer sets hoodie.compact.inline=false with nothing taking over.
Reads get slower week by week with no data growth. Log files are accumulating, so every read merges more of them. Check log files per file group.
A snapshot read and a read-optimized read disagree. That is the stale-read
behaviour above, not corruption. Compare sum or a changed column, because row
counts will match.
Key takeaways
A Merge-on-Read table has two independent problems: a debt of log files, and the old versions that paying it leaves behind. Compaction pays the debt. Cleaning removes what is left. Hudi cleans by default and a Spark batch writer compacts inline unless told not to, so check what your tables actually do.
When a read-optimized query looks stale, compaction has not run. When storage grows after compaction, cleaning has not removed what it replaced.
References
- Hudi compaction and cleaning for the timeline operations and their trigger strategies
- Apache Iceberg table maintenance for the same problem in a snapshot and manifest table
- Hudi Merge-on-Read for the write path that produces the log files compaction merges
- Open table formats in practice for why the two storage models differ
Trademarks
Apache Hudi, Apache Iceberg, Apache Spark, Apache Parquet, Apache and the Apache feather logo are either registered trademarks or trademarks of The Apache Software Foundation in the United States and other countries.
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.