Query icon
If icon
Commands icon
SlackIncomingWebhook icon
Log icon
Schedule icon
Webhook icon

Apache Iceberg Partition Compaction and Lakehouse Maintenance Gate

Automated Lakehouse maintenance for Apache Iceberg. Profiles Parquet small-file debt in DuckDB, triggers compaction, and expires stale snapshots.

Categories
BusinessDataInfrastructure

In modern data lakehouses built on Apache Iceberg, Delta Lake, or Apache Hudi, streaming ingestion and frequent micro-batch writers inevitably cause the notorious small file problem. Over time, tables accumulate tens of thousands of 2MB to 5MB Parquet files along with redundant commit metadata snapshots. When query engines (such as Trino, Snowflake, DuckDB, or AWS Athena) scan these fragmented partitions, metadata parsing latency multiplies, query performance degrades by up to 10x, and cloud object storage request costs escalate.

This blueprint provides an autonomous Lakehouse maintenance gate. It leverages an in-memory DuckDB query to profile partition-level file size distributions, isolates partitions where small files exceed the configured fragmentation ratio, executes bin-packing compaction to consolidate small files into optimized 128MB targets, expires historical snapshots older than the retention threshold, and posts an itemized health scorecard to Slack.

How it works

  1. profile_partition_fragmentation (io.kestra.plugin.jdbc.duckdb.Query) profiles table partition statistics, computing the ratio of small files (< 64MB) against total partition files and calculating projected query scan speedup.
  2. evaluate_compaction_verdict (io.kestra.plugin.core.flow.If) evaluates whether any partitions breach the fragmentation ratio threshold (0.35).
  3. execute_bin_pack_compaction (io.kestra.plugin.scripts.shell.Commands) triggers targeted bin-packing compaction on degraded partitions, rewriting small files into target 128MB Parquet chunks.
  4. expire_historical_snapshots (io.kestra.plugin.scripts.shell.Commands) vacuums historical metadata snapshots and manifest lists older than inputs.snapshot_retention_days.
  5. notify_lakehouse_health_card (io.kestra.plugin.slack.notifications.SlackIncomingWebhook) dispatches a Lakehouse Health Scorecard detailing eliminated files, consolidated volume, and query scan speedup.

What you get

  • Automated detection and remediation of small-file fragmentation debt across lakehouse partitions.
  • Conditional execution that skips compaction on already-optimized partitions to conserve compute credits.
  • Snapshot vacuuming to prevent unbounded growth of object storage metadata pointers.
  • Zero-setup demo execution via the seed_demo_data toggle with pre-packaged fragmented partition telemetry.

Who it's for

  • Data Platform Engineers operating Apache Iceberg or Delta Lake storage layers on S3, GCS, or MinIO.
  • Analytics Engineers optimizing Trino, Snowflake, or Athena query performance and latency.
  • FinOps Leads seeking to minimize S3 PUT/GET API charges caused by runaway small-file proliferation.

Why orchestrate this with Kestra

Table maintenance requires coordinated scheduling, conditional threshold evaluation, compute execution, and stakeholder notifications. Kestra provides native DuckDB metadata profiling, declarative flow control, and webhook integration, replacing ad-hoc cron jobs with an audited, observable maintenance pipeline.

Pitfalls

  • Concurrent write conflicts: Compaction operations commit new table metadata; schedule maintenance during low-ingestion windows to minimize optimistic concurrency write retries.
  • Target file sizing: Consolidating files to 128MB to 512MB optimizes parallel read splits; avoid over-compacting into multi-gigabyte files that limit reader concurrency.
  • Snapshot retention limits: Ensure snapshot expiration retention (snapshot_retention_days) exceeds the maximum time-travel query duration required by audit teams.

Prerequisites

  • Read and write access to the target Lakehouse catalog and underlying object storage.
  • Slack incoming webhook endpoint for maintenance notifications.

Secrets

  • SLACK_WEBHOOK_URL: Slack incoming webhook URL for dispatching Lakehouse health cards.
  • LAKEHOUSE_WEBHOOK_KEY: Secret authentication key for event-driven compaction triggers.

Quick start

  1. Execute the flow with seed_demo_data: true to observe automated profiling, small-file consolidation, and health card generation.
  2. Configure SLACK_WEBHOOK_URL in your Kestra namespace.
  3. Enable the weekly schedule trigger for off-peak automated lakehouse maintenance.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.