Interview's MateData Engineer
Interview's Mate

Data Engineer

SQL · Spark · Lakehouse · Streaming

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Data Engineer topics
Lesson 92/ 160
05. Data Lakehouses & Open Table Formats

Compaction, File Sizing & Metadata Cleanup Strategies

Keep tables healthy with target file sizes, compaction jobs, and metadata garbage collection.

AdvancedCompaction · File Size · Cleanup3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

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

Loading interactive 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.

Compaction, File Sizing & Metadata Cleanup Strategies in one concrete design

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.

Key takeaway: Compaction rewrites many small or fragmented files into target-sized logical bins, then table metadata commits the new file set

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.

Key takeaway: 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.

How it works, step by step

Step 1 of 4
Compaction

1. 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.

file-size distribution and query/list overhead
Key numbers at this step
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

Compaction
Best for: A table-aware rewrite of data files into larger or sorted files without changing logical rows.

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.

File Size
Best for: The intended compressed/uncompressed output dimensions selected from engine parallelism and object/request costs.

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.

Cleanup
Best for: Removal of obsolete snapshots, manifests, transaction logs, or unreferenced orphan files after retention.

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?
Interview tip: Lead with the data path and failure boundary: 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. Then make the answer operational: 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.

Knowledge check

Loading quiz

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.

Open Data Engineer Q&A →Machine Learning Q&A
Previous topic
Schema Evolution & Schema Enforcement in Lakehouses
Next topic
Open Catalogs & Interoperability (Unity Catalog, Apache Polaris, AWS Glue, Nessie)