Why Inline Auto-Compaction Breaks Streaming SLAs (and How Out-of-Band OPTIMIZE Fixes It)
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
MERGEexecution:5.17s(2.16sscan,2.28srewrite). p90MERGEexecution:8.24s(Deletion Vectors kept rewrite overhead below4.5sfor 90% of commits).- The Problem: Because
autoCompact.minNumFileswas set to25, every ~30 microbatches the streaming loop halted completely to compact26–31small files synchronously—freezing the stream for26sto61sper 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:
- Raising
minNumFilesto100: Fewer pauses (8instead of35), much worse spikes—merging100+files synchronously froze the stream for up to165.3seconds. - Disabling
autoCompactWithout a Separate Job: Zero inline pauses, but at 1 batch every30s, the table accumulated2,880small Parquet files per day (20,160files in 7 days). Delta scan latency degraded from2sto62.5swithin a week—breaking the SLA anyway and forcing Power BI Direct Lake into fallback DirectQuery mode.
The Three-Part Solution
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–10strigger intervals, Deletion Vectors and Low Shuffle Merge generate~150–250active files every 2 hours. - Keeping active files below
250ensures OneLakeabfss://List Pathsreturns in a single50–100 msHTTP 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 duringMERGE, keepingscanTimeMsflat near1.5s–2.0sas 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
- 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 Scenario | OCC Conflict Rate | OPTIMIZE Success Rate | OPTIMIZE Failure Rate |
|---|---|---|---|
Unclustered + Continuous MERGE | 87.96% | 99.66% | 0.34% |
Liquid Clustered (CLUSTER BY) + Continuous MERGE | 16.14% | 100.00% | 0.00% |
| Liquid Clustered + Off-Peak Schedule | 1.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.
NOTEProtect Direct Lake Readers During
VACUUM: Never runVACUUM ... RETAIN 0 HOURSon tables served by Power BI Direct Lake. Removing Parquet files still referenced by an active Direct Lake framing triggers immediateFileNotFoundExceptionvisual failures; keep standardRETAIN 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.