// Muhammad Rasyad Caesarardhi & Gemini 3.8 Flash

Why Inline Auto-Compaction Breaks Streaming SLAs (and How Out-of-Band OPTIMIZE Fixes It)

Data EngineeringDelta LakeApache SparkPerformanceStreaming

When your Delta Lake MERGE execution takes 5.17 seconds at the median, you expect your streaming pipeline to comfortably clear a 60-second SLA.

Yet when we audited 1,000 consecutive production commits on a live CDC Silver table, 90th-percentile (p90) Bronze-to-Silver latency spiked to 100.97 seconds (~1.7 minutes).

Digging into the _delta_log commit metrics uncovered the silent culprit: 31 inline OPTIMIZE pauses injected directly into the streaming thread by spark.databricks.delta.autoCompact.enabled = true. Here is what happened inside those logs, why simply tweaking minNumFiles or turning compaction off fails, and how decoupling out-of-band OPTIMIZE with Liquid Clustering solved it.

Anatomy of the 101-Second p90 Latency Spike

Across 969 production MERGE commits and 31 OPTIMIZE commits (running Spark 4.1.1 / Delta Lake 4.2.0 with Deletion Vectors enabled):

  • Median MERGE execution: 5.17s (2.16s scan, 2.28s rewrite).
  • p90 MERGE execution: 8.24s (Deletion Vectors kept rewrite overhead below 4.5s for 90% of commits).
  • The Problem: Because autoCompact.minNumFiles was set to 25, every ~30 microbatches the streaming loop halted completely to compact 26–31 small files synchronously—freezing the stream for 26s to 61s per compaction!
v2501: removedFiles=30, addedFiles=1, DVsRemoved=2, pause=43.0s
v2471: removedFiles=31, addedFiles=1, DVsRemoved=2, pause=39.0s
v1541: removedFiles=27, addedFiles=1, DVsRemoved=3, pause=61.0s

Even worse, while a 45s inline compaction ran, incoming CDC rows queued up in Bronze—creating a 15–20 minute backlog catch-up ripple that polluted p85 and p90 metrics long after the compaction finished.

Why Naive Tweaks Don’t Work

We simulated five maintenance architectures across our 1,000-commit trace:

  1. Raising minNumFiles to 100: Fewer pauses (8 instead of 35), much worse spikes—merging 100+ files synchronously froze the stream for up to 165.3 seconds.
  2. Disabling autoCompact Without a Separate Job: Zero inline pauses, but at 1 batch every 30s, the table accumulated 2,880 small Parquet files per day (20,160 files in 7 days). Delta scan latency degraded from 2s to 62.5s within a week—breaking the SLA anyway and forcing Power BI Direct Lake into fallback DirectQuery mode.

The Three-Part Solution

flowchart TD subgraph Stream["Continuous Streaming Job (autoCompact = false)"] S1["Single-Pass Fused Validations<br/>(Drop Redundant Full-Table Joins)"] --> S2["Low-Latency Delta MERGE + Deletion Vectors"] end subgraph Maint["Out-of-Band Maintenance (Every 2 Hours)"] M1["Independent Small Spark Pool"] --> M2["OPTIMIZE ... CLUSTER BY (primary_key)"] end S2 -->| Fast Writes - No Freeze | DL["OneLake Silver Delta Table (Under 250 Active Files)"] M2 -->| Concurrent OCC Safe | DL

1. Decouple Compaction Every 2 Hours + Liquid Clustering (CLUSTER BY)

We disabled inline auto-compaction (autoCompact.enabled = false), enabled Liquid Clustering on the table’s merge key (CLUSTER BY (record_id)), and moved OPTIMIZE to an independent scheduled job every 2 hours.

Why 2 hours?

  • At 5–10s trigger intervals, Deletion Vectors and Low Shuffle Merge generate ~150–250 active files every 2 hours.
  • Keeping active files below 250 ensures OneLake abfss:// List Paths returns in a single 50–100 ms HTTP response instead of multi-page JSON pagination (+1.5s–3.0s), and stays well below Power BI Direct Lake’s small-file guardrails.
  • Liquid Clustering skips 90%+ of irrelevant Parquet files during MERGE, keeping scanTimeMs flat near 1.5s–2.0s as the table grows.

2. Audit and Drop Redundant Full-Table Pre-Merge Joins

Before optimizing compaction, our Silver pipeline ran two defensive anti-anomaly joins against the entire target Silver table on every microbatch (4s–8s overhead per batch) and ordered events by (pg_lsn, kafka_offset).

Querying 669,820 production Bronze rows (553,070 distinct keys across 4 partitions) proved 0 LSN regressions, 0 null LSNs, and 0 conflicting (tx_id, pg_lsn) pairs—while 4-partition Docker dual-connector tests proved kafka_partition/kafka_offset can actually diverge across connector restarts while database LSN stays monotonic. Removing both full-table joins cut local process_batch time by 27.0% (8.330s → 6.078s) and merge time by 33.2% (6.814s → 4.554s) with 100% row fidelity.

3. Fuse Pre-Merge Validations Into a Single Spark Job

Replacing multiple sequential .count() validation checks with one single-pass aggregate query cut pre-validation p50 latency by 57.7% (0.33s → 0.14s).

Benchmark & Concurrency Stress Test Results

Physical Spark 4.1.1 + Delta Lake 4.2.0 Benchmark

Physical Batch Latency: Inline Compaction vs Decoupled Steady State

Inline Compaction Baseline (p50)3.06s
Decoupled + Fused Checks (p50)2.35s
Inline Compaction Baseline (p90)5.11s
Decoupled + Fused Checks (p90)3.04s
  • p50 Latency: 3.06s → 2.35s (23.2% faster)
  • p90 Latency: 5.11s → 3.04s (40.5% faster)

10,000-Trial Monte Carlo Concurrency Stress Test (MERGE vs. OPTIMIZE)

Running OPTIMIZE concurrently with 5-second MERGE streams raises a classic question: Will Optimistic Concurrency Control (OCC) collisions break the pipeline? We simulated 10,000 overlapping maintenance windows under a 35% update rate:

Concurrency ScenarioOCC Conflict RateOPTIMIZE Success RateOPTIMIZE Failure Rate
Unclustered + Continuous MERGE87.96%99.66%0.34%
Liquid Clustered (CLUSTER BY) + Continuous MERGE16.14%100.00%0.00%
Liquid Clustered + Off-Peak Schedule1.94%100.00%0.00%

Because Delta MERGE with Deletion Vectors commits in 4–5s, the streaming job never loses the commit race. Liquid Clustering narrows updates down to 1–2 target files, slashing OCC collisions by over 80% (87.96% → 16.14%) and giving background OPTIMIZE a 100% completion rate.

NOTE

Protect Direct Lake Readers During VACUUM: Never run VACUUM ... RETAIN 0 HOURS on tables served by Power BI Direct Lake. Removing Parquet files still referenced by an active Direct Lake framing triggers immediate FileNotFoundException visual failures; keep standard RETAIN 168 HOURS (7 days).

Key Takeaway

Inline auto-compaction trades silent, catastrophic 30–60s tail-latency freezes for convenience. By combining Deletion Vectors, Liquid Clustering, single-pass validations, and an out-of-band 2-hour OPTIMIZE job, you keep streaming microbatches flat and sub-10-second without sacrificing read performance or ACID concurrency safety.

Co-authored with Gemini 3.8 Flash.