Troubleshooting and optimization
Read stage metrics to distinguish skew, shuffle, and spill. Match the diagnosis to an optimization and check it against run history.
Reading the Spark UI
Open it from a cluster (Spark UI tab), from a job task run, or from a notebook cell's "Spark Jobs" link. For SQL warehouses and serverless, use the query profile, which shows the same information as an operator graph.
| Tab | Shows |
|---|---|
| Jobs | One entry per action; duration; stages |
| Stages | Per stage: duration, input, output, shuffle read/write, spill (memory/disk), and the task summary metrics (Min, 25th percentile, Median, 75th percentile, Max) for duration, GC time, shuffle read size, spill |
| SQL / DataFrame | The physical plan as a graph with rows and time per operator, join strategy (BroadcastHashJoin vs SortMergeJoin), AQE changes |
| Executors | Memory use, GC time, failed tasks per executor; driver and executor logs |
| Storage | Cached DataFrames |
| Structured Streaming | Input rate, processing rate, batch duration |
The three classic bottlenecks
Data skew
Symptom: in a stage, most tasks finish fast but one or a few take far longer. Task summary shows Max duration or shuffle read far above the Median (e.g. median 400 MB, max 5 GB). Cause: one key value (a "hot key", or null) holds most rows.
Fixes:
- AQE skew join handling (
spark.sql.adaptive.skewJoin.enabled, on by default) splits skewed partitions, but only for joins (sort-merge and shuffle-hash), not forgroupByaggregations. - Salting: add a random suffix to the key so one hot key spreads over N tasks, aggregate on (key, salt), then aggregate again on the key.
- Broadcast the small side of a join, filter out or handle null keys separately, use skew hints.
- Adding executors does not help: the one huge task still runs alone. Lowering shuffle partitions makes partitions bigger and makes it worse.
salted = df.withColumn("salt", (F.rand() * 16).cast("int"))
partial = salted.groupBy("customer_id", "salt").agg(F.sum("amount").alias("s"))
result = partial.groupBy("customer_id").agg(F.sum("s").alias("amount"))
Shuffle
Symptom: large shuffle read/write in stage metrics; stage boundaries at joins and aggregations; long network time. Fixes: filter and select columns early, broadcast small tables, avoid unnecessary distinct/orderBy/repartition, tune spark.sql.shuffle.partitions (or rely on AQE coalescing), cluster tables on join keys.
Disk spill
Symptom: "Spill (Memory)" and "Spill (Disk)" non-zero in stage metrics; slow tasks. Spark ran out of execution memory and wrote intermediate data to disk. Fixes: more partitions so each is smaller (raise shuffle partitions), memory-optimized or larger nodes, fix skew, reduce data before wide operations.
Small files
Many tiny files slow reads (file listing and opening overhead). Fixes: OPTIMIZE, auto compaction and optimized writes, predictive optimization, avoid over-partitioning.
Liquid clustering
- Declared with
CLUSTER BY (cols)at create time or withALTER TABLE ... CLUSTER BY. Up to 4 clustering keys. - Replaces Hive-style partitioning and Z-order. Cannot be combined with
PARTITIONED BYorZORDER. - Keys can be changed at any time without rewriting existing data; new layout applies to newly clustered data.
- Incremental:
OPTIMIZEonly clusters data that needs it.OPTIMIZE FULLreclusters all records. - Good for: high-cardinality filter columns, skewed distributions, fast-growing tables, changing access patterns, concurrent writes (row-level concurrency).
- Automatic liquid clustering:
CLUSTER BY AUTOon Unity Catalog managed tables; Databricks chooses and adapts keys based on query history. Requires predictive optimization.
Predictive optimization
- Automatically runs
OPTIMIZE,VACUUMandANALYZE(statistics) on Unity Catalog managed tables when Databricks predicts it is worthwhile. - Runs on serverless compute; no scheduling or tuning by you. Enabled by default for newer accounts; can be enabled or disabled at account, catalog or schema level (
ALTER CATALOG c ENABLE PREDICTIVE OPTIMIZATION). - Does not apply to external tables. History is in
system.storage.predictive_optimization_operations_history.
Common failures and fixes
| Problem | Typical causes | Fixes |
|---|---|---|
| Cluster fails to start | Cloud quota or capacity (instance type unavailable in region), spot capacity, a failing init script, networking (VPC, firewall, subnet IPs exhausted), policy or permission restrictions, incompatible library install | Read the cluster event log and termination reason; check init script logs; choose another instance type or zone; raise cloud quotas; fix network; use pools; try serverless |
| Library conflicts | A library version incompatible with the runtime's built-in version, or two libraries needing different versions of a dependency; cluster libraries and notebook %pip clashing | Pin versions; use notebook-scoped %pip and dbutils.library.restartPython(); pick a runtime that already includes the library; use serverless environments with explicit dependencies; check "Libraries" tab status |
| Driver out of memory | collect() or toPandas() on large data, very large broadcast, too many small tasks, heavy driver-side Python | Avoid collecting; write results to tables; lower broadcast threshold; larger driver |
| Executor out of memory | Skewed or very large partitions, big joins, wide rows, too few partitions | More shuffle partitions, fix skew, memory-optimized workers, avoid UDFs that hold large objects |
| Job slower than usual | Data volume growth, skew from a new source, small files, a changed plan | Compare with run history baseline; Spark UI stage metrics; OPTIMIZE; clustering |
A frequent case of the first row is QuotaExceeded on cluster start (for example "current usage 0, additional required 4" cores for a VM family): the cloud subscription has no vCPU quota for that instance family in that region. Fix by requesting a quota increase from the cloud provider, picking another instance type or region, or retrying later if it is a capacity issue.
Questions describe metrics and ask for the diagnosis or fix. Map: one slow task with huge max shuffle read = skew (salt it). Non-zero disk spill = memory pressure (more partitions or memory). Large shuffle bytes = shuffle (broadcast, filter early). Driver OOM after collect() = do not collect.
Read the stage distribution
In one stage, 95 tasks finish within 12 seconds and one runs for four minutes. Compare per-task shuffle reads before changing cluster size. What pattern would indicate skew?
Check your reasoning
A much larger shuffle read on the slow task points to an uneven partition. Inspect the key distribution and operator before choosing to rebalance, salt, or broadcast.
Product reference
Diagnose performance issues in the Spark UI in the Databricks documentation
Troubleshooting and optimization quiz
Answer 6 questions. Check each answer for an explanation.