Compaction, File Sizing & Metadata Cleanup Strategies
Keep tables healthy with target file sizes, compaction jobs, and metadata garbage collection.
In plain words
File-size distribution and query/list overhead. Next, stable partitions beyond the active writer window. Finally, snapshots, manifests, and verified orphan files.
Think of it like…
Compaction is repacking tiny shipping boxes into truck-friendly pallets; metadata cleanup is discarding obsolete labels only after the old delivery chain has finished using them.
Where you’ve seen it
A streaming table produces 5 MB files each trigger; a daily job rewrites only partitions older than 2 hours into about 256 MB Parquet files, then expiration keeps 72 hours of snapshots for readers.
Words you’ll hear
- Compaction
- A table-aware rewrite of data files into larger or sorted files without changing logical rows.
- File Size
- The intended compressed/uncompressed output dimensions selected from engine parallelism and object/request costs.
- Cleanup
- Removal of obsolete snapshots, manifests, transaction logs, or unreferenced orphan files after retention.
- Operational boundary
- Compacting every table run rewrites unchanged data, Z-Order and sort options fight each other, and deleting old files before a reader/checkpoint's retention can break replay or active time travel. Trigger by small-file count, target-file deviation and read latency, compact hot partitions only, limit concurrent rewrites, and run metadata/orphan cleanup under snapshot and stream retention policies.
Visual diagram
Theory & deep dive
Compaction: the working mechanism
Compaction rewrites many small or fragmented files into target-sized logical bins, then table metadata commits the new file set; separate metadata cleanup expires snapshots, manifests, and orphan files.
File-size distribution and query/list overhead. Stable partitions beyond the active writer window, then target files, sort/locality, and atomic metadata; this exposes the state governed by Cleanup.
A streaming table produces 5 MB files each trigger; a daily job rewrites only partitions older than 2 hours into about 256 MB Parquet files, then expiration keeps 72 hours of snapshots for readers.
Compaction, File Sizing & Metadata Cleanup Strategies: failure mode and production response
Compacting every table run rewrites unchanged data, Z-Order and sort options fight each other, and deleting old files before a reader/checkpoint's retention can break replay or active time travel.
Trigger by small-file count, target-file deviation and read latency, compact hot partitions only, limit concurrent rewrites, and run metadata/orphan cleanup under snapshot and stream retention policies. Track Target file as "roughly 128–1,024 MB as a workload-dependent band", and make Rewrite ratio visible to the dataset owner.
How it works, step by step
Step 1 of 41. Measure
File-size distribution and query/list overhead. Compaction rewrites many small or fragmented files into target-sized logical bins, then table metadata commits the new file set; separate metadata cleanup expires snapshots, manifests, and orphan files.
- Target file
- roughly 128–1,024 MB as a workload-dependent band
- File Size
- The intended compressed/uncompressed output dimensions selected from engine parallelism and object/request costs.
When to use it & trade-offs
What it is
Compaction rewrites many small or fragmented files into target-sized logical bins, then table metadata commits the new file set; separate metadata cleanup expires snapshots, manifests, and orphan files. A streaming table produces 5 MB files each trigger; a daily job rewrites only partitions older than 2 hours into about 256 MB Parquet files, then expiration keeps 72 hours of snapshots for readers.
Why it matters: Compacting every table run rewrites unchanged data, Z-Order and sort options fight each other, and deleting old files before a reader/checkpoint's retention can break replay or active time travel. Trigger by small-file count, target-file deviation and read latency, compact hot partitions only, limit concurrent rewrites, and run metadata/orphan cleanup under snapshot and stream retention policies.
When to reach for it
- Keep tables healthy with target file sizes, compaction jobs, and metadata garbage collection.
- Choose Compaction because it provides this capability: A table-aware rewrite of data files into larger or sorted files without changing logical rows.
- Keep evidence for Target file at "roughly 128–1,024 MB as a workload-dependent band" so the last production stage-Expire - snapshots, manifests, and verified orphan files-remains testable.
When to avoid or be careful
- Compacting every table run rewrites unchanged data, Z-Order and sort options fight each other, and deleting old files before a reader/checkpoint's retention can break replay or active time travel.
- Do not conflate File Size with Compaction. The second concept contributes different behavior: The intended compressed/uncompressed output dimensions selected from engine parallelism and object/request costs.
- Release only after this guardrail is in place: Trigger by small-file count, target-file deviation and read latency, compact hot partitions only, limit concurrent rewrites, and run metadata/orphan cleanup under snapshot and stream retention policies.
Compaction / File Size / Cleanup - roles, limits, and operating cost
Strengths: A table-aware rewrite of data files into larger or sorted files without changing logical rows. Compaction rewrites many small or fragmented files into target-sized logical bins, then table metadata commits the new file set.
Watch out: Compacting every table run rewrites unchanged data, Z-Order and sort options fight each other, and deleting old files before a reader/checkpoint's retention can break replay or active time travel.
Strengths: The intended compressed/uncompressed output dimensions selected from engine parallelism and object/request costs. In this workflow it supports: Rewrite - target files, sort/locality, and atomic metadata.
Watch out: The operating cost is real: bytes rewritten versus logical rows changed.
Strengths: Removal of obsolete snapshots, manifests, transaction logs, or unreferenced orphan files after retention. Its output can be checked with Retention horizon.
Watch out: Operating guardrail: Trigger by small-file count, target-file deviation and read latency, compact hot partitions only, limit concurrent rewrites, and run metadata/orphan cleanup under snapshot and stream retention policies.
Interview signals & numbers to know
When this topic comes up
- “Where is the failure boundary for Compaction, and how would Target file reveal it?”
- “Why is File Size distinct from this behavior: removal of obsolete snapshots, manifests, transaction logs, or unreferenced orphan files after retention?”
- Production clue: Compacting every table run rewrites unchanged data, Z-Order and sort options fight each other, and deleting old files before a reader/checkpoint's retention can break replay or active time travel.
- Design probe: Trigger by small-file count, target-file deviation and read latency, compact hot partitions only, limit concurrent rewrites, and run metadata/orphan cleanup under snapshot and stream retention policies.
Benchmarks & rules of thumb
- Target file
- roughly 128–1,024 MB as a workload-dependent band
- Rewrite ratio
- bytes rewritten versus logical rows changed
- Retention horizon
- max of active readers, stream checkpoints, and recovery SLO
Common mistakes & follow-ups
Common interview mistakes
- Compacting every table run rewrites unchanged data, Z-Order and sort options fight each other, and deleting old files before a reader/checkpoint's retention can break replay or active time travel.
- Reporting success from a job exit code without checking Target file and Rewrite ratio.
- Treating this example as complete without replay, rollback, or access boundaries: A streaming table produces 5 MB files each trigger; a daily job rewrites only partitions older than 2 hours into about 256 MB Parquet files, then expiration keeps 72 hours of snapshots for readers.
Likely interviewer follow-ups
- How would you prove this contract instead of only checking that the command ran: A table-aware rewrite of data files into larger or sorted files without changing logical rows.
- What does the recovery plan do when this occurs? Compacting every table run rewrites unchanged data, Z-Order and sort options fight each other, and deleting old files before a reader/checkpoint's retention can break replay or active time travel.
- When is Compaction insufficient for Rewrite ratio, and what does adding File Size cost in Target file?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Compaction, File Sizing & Metadata Cleanup Strategies with data engineer interview Q&A and hands-on practice.