Troubleshooting, Monitoring, and Optimization · Topic 2 of 2

Spark Bottlenecks, Table Layout and Compute Failures

Databricks DEA: skew, shuffle and spill in Spark UI stage metrics, Liquid Clustering, predictive optimization, and startup, library and OOM failures.

  • 5 min
In a nutshell
  • Skew shows as Max far above Median in stage Summary Metrics.
  • Spill (Memory) and Spill (Disk) mean tasks ran out of execution memory.
  • AQE skew handling helps joins; salting fixes skewed aggregations.

What it is

When a job is slow or fails, the cause is usually one of a few patterns: uneven data across tasks, too much data moved between nodes, not enough memory per task, a poor table layout, or compute that cannot start or run the code.

The Spark UI shows stage and task metrics for slow queries. Liquid Clustering and predictive optimization keep Delta tables laid out well. The compute event log, library status and driver logs explain startup, library and memory failures.

SymptomSpark UI stage metricsskew, shuffle, spillTable layoutclustering, predictive optimizationCompute logsstartup, libraries, memory

How it works

Reading stage metrics in the Spark UI

Open the Spark UI from the cluster or the query, find the slowest stage, and open its detail page. The Summary Metrics table shows task duration, input, shuffle read and write and spill at Min, 25th percentile, Median, 75th percentile and Max.

BottleneckWhat the stage showsTypical fix
Data skewMax task duration and input far above the median; a few tasks run long after the rest finishAQE skew join handling for joins; salting the key for skewed aggregations
Heavy shuffleLarge Shuffle Read and Shuffle Write sizes; exchange steps in the query planBroadcast the small side of a join, filter and select columns early
Disk spillingSpill (Memory) and Spill (Disk) values on the stageMore memory per task, more shuffle partitions, larger worker type

Adaptive Query Execution splits skewed partitions in joins, not in groupBy aggregations. For a skewed aggregation, add a random salt to the key, aggregate, then aggregate again without it.

from pyspark.sql import functions as F
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"))

Liquid Clustering

Liquid Clustering organizes a Delta table's data by up to four clustering keys, chosen from columns used in filters. Databricks recommends it for all new tables, including streaming tables and materialized views.

CREATE TABLE sales.orders (order_id BIGINT, customer_id BIGINT, order_date DATE)
CLUSTER BY (customer_id, order_date);
ALTER TABLE sales.orders CLUSTER BY (order_date);
OPTIMIZE sales.orders;

Clustering is incremental: OPTIMIZE rewrites only data that is not yet clustered. Changing keys with ALTER TABLE ... CLUSTER BY does not rewrite existing data; OPTIMIZE ... FULL reclusters it.

CLUSTER BY AUTO lets Databricks choose the keys from the query workload; it needs predictive optimization and a Unity Catalog managed table. Liquid Clustering cannot be combined with PARTITIONED BY or ZORDER.

Predictive optimization

Predictive optimization runs OPTIMIZE, VACUUM and ANALYZE automatically on serverless compute, when Databricks judges they are worth the cost. It covers Unity Catalog managed tables only, never external tables.

ALTER CATALOG sales ENABLE PREDICTIVE OPTIMIZATION;
ALTER SCHEMA sales.staging INHERIT PREDICTIVE OPTIMIZATION;

It is set at account, catalog, schema or table level, and lower levels inherit by default. Its operations are recorded in system.storage.predictive_optimization_operations_history.

Cluster startup failures

If a cluster never reaches running, the Spark UI has nothing to show. Open the compute's event log, which lists each step and the error that stopped it.

CauseWhat you see
Cloud capacity or quotaThe provider could not supply the instance type, or the account limit was reached
Init script failureThe init script returned an error; check its logs
Network or permissionsNodes cannot reach the control plane or storage

Fixes include another instance type or zone, a quota increase, correcting the init script, or serverless compute where the workload allows it.

Library conflicts

A library conflict happens when an installed package needs a different version of a package that the Databricks Runtime already includes, or when two libraries need incompatible versions. The compute's Libraries tab shows a failed install; imports fail or behave oddly.

Notebook-scoped libraries installed with %pip install affect only that notebook session. Pin versions that match the runtime, then restart Python with dbutils.library.restartPython().

Out of memory errors

WhereCommon causeFix
Drivercollect() or toPandas() on a large DataFrame, or a broadcast that is too bigKeep results distributed, write to a table, larger driver
ExecutorSkewed or oversized partitions, wide aggregationsFix skew, more shuffle partitions, larger workers

A driver out of memory error typically shows as the Spark driver stopping unexpectedly and restarting. Executor problems show as failed tasks and lost executors in the Spark UI.

How the exam asks about this

  • "In one stage, the max task duration is 40 times the median." Data skew; for a join rely on AQE skew handling, for an aggregation salt the key.
  • "Queries filter on customer_id and the table is not partitioned; reduce files scanned with low upkeep." Liquid Clustering with CLUSTER BY (customer_id).
  • "The cluster stays in pending and then terminates." Read the compute event log for the cloud or init script error.

What to remember for the exam

  • Skew shows as Max far above Median in stage Summary Metrics.
  • Spill (Memory) and Spill (Disk) mean tasks ran out of execution memory.
  • AQE skew handling helps joins; salting fixes skewed aggregations.
  • Liquid Clustering: up to four keys, incremental OPTIMIZE, no PARTITIONED BY or ZORDER.
  • Predictive optimization runs OPTIMIZE, VACUUM and ANALYZE on Unity Catalog managed tables.
  • Startup failures are read in the event log; driver out of memory errors often come from collect().
Watch out

Predictive optimization does nothing for external tables. If a scenario needs automatic maintenance on an external table, convert it to managed or schedule OPTIMIZE and VACUUM yourself.

Common mistakes

Adding workers to fix skew. The slow tasks still hold most of the data, so more nodes barely help. Spread the hot key instead.

Expecting ALTER TABLE CLUSTER BY to rewrite the table. New keys apply to future writes and OPTIMIZE; run OPTIMIZE ... FULL to recluster existing data.

Looking in the Spark UI for a cluster that never started. No Spark application exists yet; the event log holds the error.

Check yourself

  • Which Spark UI metric pattern indicates data skew?

    In the stage's Summary Metrics, the Max task duration and input are far higher than the Median, so a few tasks run long after the rest.

  • Which tables does predictive optimization maintain?

    Only Unity Catalog managed tables; it runs OPTIMIZE, VACUUM and ANALYZE on them automatically.

  • What is a common cause of a driver out of memory error?

    Pulling a large DataFrame to the driver with collect() or toPandas(); keep the data distributed or write it to a table.

Topic 2 of 2 in Troubleshooting, Monitoring, and Optimization

Test yourself on Troubleshooting, Monitoring, and Optimization

The full Databricks Data Engineer Associate question bank has 598 questions, with timed practice exams and an explanation for every answer.

This study guide is independent and is not affiliated with, endorsed by, or sponsored by Databricks. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.