Schedule icon
Webhook icon
Assert icon
WorkingDirectory icon
Script icon
Docker icon
Set icon
Log icon
If icon
SlackIncomingWebhook icon

Delta Lake Uncompacted Small Files Compactor and Safe Vacuum Gate

Inspect Delta Lake partitions, execute bin-packing compaction when small files exceed 40%, and enforce safe vacuum retention. Track compaction stats in KV.

Categories
CloudData

Continuous streaming and micro-batch ingestion create thousands of small Parquet files in Delta Lake tables. As small files accumulate across table partitions, metadata overhead surges, object store request costs rise, and query scan latency degrades severely. External compaction requires careful coordination: merging small files must balance bin-packing target sizes with multidimensional Z-Ordering clustering, while tombstone vacuuming must enforce strict retention windows to prevent corrupting concurrent readers.

This blueprint implements an automated Day 2 operations maintenance gate for Delta Lake tables:

  • Partition-Level Inspection: Queries Delta transaction log metadata via deltalake and DuckDB to measure average file sizes and small-file fragmentation ratios per partition.
  • Query Latency Benchmarking: Executes partition scan queries through DuckDB to detect read performance regressions against configured SLA thresholds.
  • Dynamic Compaction Gate: Triggers bin-packing compaction and multidimensional Z-Ordering when small files exceed 40% of active partition files, rewriting micro-batches into 128 MB Parquet files.
  • Safe Vacuum Enforcement: Runs VACUUM with a hard safety guard enforcing at least 168 hours (7 days) of retention, preventing premature deletion of historical files needed by active readers and time-travel queries.
  • Stateful KV Telemetry: Persists compaction metrics (files reduced, bytes rewritten, latency speedup) into the Kestra KV store and dispatches notifications to Slack.

How it works

  1. Configuration Validation: An Assert task validates that safety parameters are respected, strictly requiring vacuum retention to be at least 168 hours.
  2. Synthetic Data Seeding: If demo_mode is enabled, the flow writes 30 small micro-batch appends across partitioned dates to simulate streaming fragmentation.
  3. Partition & Latency Audit: A Python task scans transaction log commit actions, calculates small-file percentages across partitions, and benchmarks DuckDB read scan latency against SLA targets.
  4. Bin-Packing & Z-Order Compaction: If small files exceed the 40% threshold, the compaction engine executes OPTIMIZE with Z-Ordering clustering, measures rewritten bytes and reduced files, and benchmarks post-compaction read speedup.
  5. Safe Vacuum Execution: Delta-rs inspects tombstoned files and deletes only unreferenced Parquet files that have exceeded the 168-hour retention window.
  6. KV Telemetry & Slack Reporting: A kv.Set task records compaction metrics into Kestra KV under delta_compaction_{table_identifier}, logs human-readable summaries, and alerts team channels on Slack.

What you get

  • Automated detection of partition small-file fragmentation from streaming ingest.
  • Read query latency benchmarking before and after bin-packing compaction.
  • Safe Parquet file bin-packing into 128 MB targets with Z-Ordering clustering.
  • Enforced 168-hour retention guard protecting concurrent readers from time-travel corruption.
  • Historical compaction statistics stored natively in Kestra namespace KV store.
  • Formatted Slack alerts reporting files reduced, storage compacted, and latency speedup.

Who it's for

  • Data Platform Engineers operating high-frequency streaming ingest into Delta Lake.
  • Lakehouse Administrators looking to automate partition compaction and vacuuming.
  • FinOps & Cloud Infrastructure Teams reducing cloud object storage S3/GCS API request fees.

Why orchestrate this with Kestra

  • Serverless & Lightweight: Executes Delta Lake optimization and DuckDB queries in isolated Docker containers without requiring persistent Spark clusters or Databricks runtimes.
  • Deterministic Safety Guards: Prevents operational data corruption by asserting vacuum retention limits before touching storage.
  • Integrated KV Store: Retains historical compaction telemetry across executions directly in Kestra without external database setup.
  • Event-Driven & Scheduled Operations: Run automatically on a nightly schedule or trigger instantaneously via authenticated webhooks after batch ingest completes.

Prerequisites

  • Docker runner configured on Kestra workers to execute containerized Python tasks.
  • Read and write access to the Delta Lake storage URI (local filesystem or cloud object storage).
  • Slack Incoming Webhook configured in Kestra secrets (if Slack alerts are enabled).

Secrets

  • SLACK_WEBHOOK_URL: Slack Incoming Webhook URL for compaction audit summaries.
  • DELTA_COMPACTOR_WEBHOOK_KEY: Secret authentication key for triggering the compaction webhook.

Quick start

  1. Configure SLACK_WEBHOOK_URL and DELTA_COMPACTOR_WEBHOOK_KEY in your Kestra namespace secrets.
  2. Import this blueprint into your Kestra instance.
  3. Run the flow with demo_mode: true to generate synthetic partitioned micro-batches, observe bin-packing compaction, and inspect post-compaction latency improvements.
  4. Switch demo_mode: false and point table_uri to your production Delta Lake table to schedule automated maintenance.

How to extend

  • Cloud Object Storage Authentication: Pass cloud credentials (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AZURE_STORAGE_KEY, GOOGLE_APPLICATION_CREDENTIALS) into task env blocks to manage S3, ADLS Gen2, or GCS Delta tables.
  • Partition Range Filtering: Scope compaction to specific rolling partition windows (e.g. event_date >= current_date() - interval '7 days') using delta-rs partition filters.
  • Downstream Data Quality Gates: Add Great Expectations or Soda Core validation tasks following compaction to verify dataset invariants.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.