Operating Apache Iceberg tables at scale demands continuous maintenance to keep query performance high and storage costs low. Compaction, snapshot expiration, and orphan file cleanup are the three pillars that prevent metadata bloat and file fragmentation in a busy data lakehouse.
Why Iceberg Table Maintenance Matters at Scale
Apache Iceberg’s metadata layer tracks every snapshot, manifest, and data file. Without regular upkeep, the metadata grows unbounded, slowing down planning and increasing cloud storage bills. A disciplined maintenance routine ensures that readers see a compacted, up-to-date view while writers continue streaming data without interruption.
Compaction Strategies for Query Performance
Compaction rewrites small files into larger ones, reducing the number of file opens during scans. Iceberg supports several compaction policies: binpack merges files to a target size, sort additionally sorts data by a specified column, and rewrite_data_files can be customized with Spark or Flink actions. Choosing the right policy depends on your read patterns — sort compaction accelerates predicate pushdowns, while binpack minimizes file count.
Snapshot Expiration and Time Travel Trade-offs
Snapshots enable time travel and rollback, but each retained snapshot adds manifest files. A retention policy (e.g., keep last 7 days or last 100 snapshots) balances audit requirements against metadata overhead. Expiring snapshots also triggers orphan file cleanup, because files no longer referenced by any snapshot become candidates for deletion.
Orphan File Cleanup to Reclaim Storage
Orphan files arise from failed writes, aborted transactions, or expired snapshots. The remove_orphan_files action scans the table’s manifest list and deletes files not referenced by any valid snapshot. Running this regularly prevents “zombie” data from inflating storage costs.
Compaction with Spark Structured Streaming
Spark Structured Streaming can run compaction as a micro-batch job alongside your streaming ingestion. Use the rewriteDataFiles action with a spark.sql.iceberg.compaction.target-file-size-bytes configuration to control output file size. Schedule compaction during low-traffic windows or trigger it based on file count thresholds.
Choosing the Right Compaction Policy
- Binpack: Best for general write-heavy workloads; merges small files up to a target size (default 512 MB).
- Sort: Ideal when queries filter on a high-cardinality column; sorts data before writing to enable predicate pushdown.
- Custom: Implement a
RewriteFilesActionfor advanced logic like partition-level compaction or z-order clustering.
Scheduling and Monitoring Compaction Jobs
Run compaction as a separate Spark job or embed it in a streaming foreachBatch. Monitor file count per partition, average file size, and compaction duration via Spark UI or Iceberg’s spark.sql.iceberg.compaction.metrics.enabled. Alert when file count exceeds a multiple of the ideal partition file count.
Snapshot Expiration and Orphan Cleanup with Flink
Flink’s Iceberg integration provides SQL and DataStream APIs for maintenance. The CALL system.expire_snapshots(...) and CALL system.remove_orphan_files(...) procedures can be scheduled via Flink SQL jobs or triggered from a Flink job’s checkpoint callback.
Configuring Snapshot Retention Policies
Set snapshot-retention.max-age-ms and snapshot-retention.max-snapshots in the table properties. Flink respects these when you call expire_snapshots. For multi-engine environments, define the policy once in the table metadata so both Spark and Flink honor the same rules.
Automating Maintenance with Flink SQL
Create a periodic Flink SQL job that runs:
CALL system.expire_snapshots(table => 'catalog.db.tbl', older_than => TIMESTAMP '2024-01-01', retain_last => 100);
CALL system.remove_orphan_files(table => 'catalog.db.tbl', older_than => TIMESTAMP '2024-01-01');
Schedule this job daily using Flink’s built-in scheduler or an external orchestrator like Airflow.
Operational Best Practices and Trade-offs
Balancing maintenance frequency with write throughput is critical. Over-aggressive compaction can contend with streaming writers for I/O and CPU, while infrequent compaction leads to scan degradation. The following checklist captures the essential operational guards:
- Set a target file size (512 MB–1 GB) and monitor average file size per partition weekly.
- Expire snapshots older than your compliance window; keep at least 2–3 snapshots for quick rollback.
- Run orphan file cleanup after every snapshot expiration to avoid double-charging for dead data.
- Coordinate maintenance windows across Spark and Flink clusters to prevent concurrent metadata writes.
- Track Iceberg metrics: total-files, total-manifests, snapshot-size-bytes via Prometheus or CloudWatch.
Conclusion
Effective Iceberg table operations at scale hinge on three automated routines: compaction to optimize read performance, snapshot expiration to bound metadata growth, and orphan file cleanup to reclaim storage. By embedding these actions into your Spark Structured Streaming and Flink pipelines — and by monitoring the right metrics — you maintain a lean, high-performance lakehouse. Actionable tip: Start with a daily Flink SQL maintenance job that expires snapshots older than 7 days and removes orphan files, then tune compaction frequency based on observed file-size distributions.