Sub topic 15
Reading utilization in the Spark UI and metrics
This chapter assumes you know Spark jobs, stages, tasks, partitions, and shuffles. It also assumes you can run a notebook or job on Databricks. You should know how the driver, workers, executors, and task slots relate. Sizing starts as a guess. Measurement tells you whether the guess was right, and this chapter shows where to look.
What you are trying to learn
Utilization data answers three questions about a run:
- Are the task slots busy, or do they sit idle?
- Is the work spread evenly across tasks, or do a few tasks dominate?
- Which resource runs out first: CPU, memory, disk, or network?
An undersized cluster shows saturated slots, spilling, and long stages. An oversized cluster shows idle slots, short tasks, and low CPU. A cluster of the right size can still look bad when the data is skewed, so you must separate a sizing problem from a data problem.
Start with the numbers the cluster gives you
Before you open any UI, record what the cluster offers and how the job is configured. This cell prints the approximate total cores, the shuffle partition count, and whether adaptive query execution is on.
total_cores = spark.sparkContext.defaultParallelism
shuffle_partitions = spark.conf.get("spark.sql.shuffle.partitions")
aqe_enabled = spark.conf.get("spark.sql.adaptive.enabled")
print(f"Default parallelism (approx. total cores): {total_cores}")
print(f"Shuffle partitions: {shuffle_partitions}")
print(f"Adaptive query execution enabled: {aqe_enabled}")Compare the shuffle partition count with the core count. If you have 64 cores and 200 shuffle partitions, tasks run in about three waves (200 / 64 is a little over 3). If you have 64 cores and 16 partitions, three quarters of the slots can never be used by that stage, no matter how large the cluster is. With adaptive query execution (AQE) on, Spark may coalesce small shuffle partitions at run time, so the count you see in the UI can be lower than the configured value.
Where to look
Databricks gives you two views, and they answer different questions.
| Tool | Scope | Best for |
|---|---|---|
| Spark UI | One application: jobs, stages, tasks, executors | Why a specific job was slow, skew, spill, shuffle volume |
| Cluster metrics tab | Whole cluster over time, per node | CPU, memory, network, and disk pressure; idle capacity |
Open the Spark UI from the cluster page or from a notebook run. Open the metrics from the Metrics tab of the cluster. Use the Spark UI for cause and the metrics for the overall shape of resource use.
The sequence below shows the order in which to read the evidence.
Reading the Spark UI
Executors tab
The Executors tab lists the driver and each executor with its cores, active tasks, failed tasks, task time, garbage collection (GC) time, and shuffle read and write. Look for these signals:
- Uneven task time. If one executor ran far more task time than the others, work was not spread evenly. This often points to skew or to a node that joined late during autoscaling.
- High GC time. GC time above roughly 10% of task time means executors are short of heap memory. Choose a node type with more memory per core.
- Failed tasks. Repeated failures with out-of-memory errors point to partitions that are too large for the memory available to each task.
- Idle executors. Executors with almost no task time during a stage that has many tasks suggest the stage had too few partitions, or the executors were added after the stage began.
Stages tab
Open a slow stage and read the summary metrics table. It shows minimum, 25th percentile, median, 75th percentile, and maximum values for duration, input size, shuffle read size, spill, and GC time.
- If the maximum duration is many times the median, you have skew or a straggler. Adding workers will not shorten the stage, because the stage ends only when the slowest task ends.
- If the median is a few milliseconds, tasks are too small. Scheduling overhead dominates. Reduce the partition count or let AQE coalesce them.
- If Spill (memory) and Spill (disk) are non-zero, tasks ran out of execution memory and wrote data to local disk. A little spill is harmless. Large spill in the median task means each core has too little memory.
- If shuffle read is large and tasks spend long in fetch wait, the stage is bound by network or disk, not CPU.
The event timeline on the stage page shows scheduler delay, task deserialization, shuffle read, executor computing time, and shuffle write as colored bars. A timeline that is mostly "executor computing time" is CPU-bound. A timeline with large shuffle read bars is network-bound.
Jobs tab and SQL tab
The Jobs tab shows the event timeline of executors being added and removed. For an autoscaling cluster this is the direct record of scaling behavior. Look for long gaps where the job waits while new executors start. The SQL tab shows the query plan with row counts and time per operator. Use it to find the operator that dominates, such as a sort-merge join with a large exchange.
Reading cluster metrics
The Metrics tab plots CPU, memory, network, and file system use for the whole cluster or for individual nodes. Read the patterns below.
| Pattern | Likely meaning | Typical adjustment |
|---|---|---|
| CPU under 30% for long periods during active jobs | Too many cores for the work, or too few partitions | Lower max workers, smaller nodes, more partitions |
| CPU near 100% on all nodes, stages long | Compute-bound and undersized | Add workers or move to a compute-optimized family |
| CPU high on one node only | Driver bottleneck or skew | Check driver work, such as `collect()` or pandas code |
| Memory near the limit with spill and GC | Memory-bound | Use a memory-optimized family |
| CPU idle but network or disk saturated | I/O-bound | Use storage-optimized nodes or reduce shuffle |
| Sawtooth CPU with gaps between peaks | Cluster idle between jobs | Shorter auto-termination, or a job cluster |
Look at the driver separately. A driver at high CPU or memory while workers are idle is a classic sign of work done on the driver, such as collecting a large result or running single-node Python libraries.
Separating sizing problems from data problems
Two runs can show the same slow stage for different reasons. Use this table to tell them apart.
| Evidence | Sizing problem | Data or code problem |
|---|---|---|
| Task duration spread | Narrow: max close to median | Wide: max many times the median |
| Effect of adding workers | Stage time falls roughly in proportion | Stage time barely changes |
| Spill | Present in most tasks | Present in a few tasks |
| Idle slots during the stage | None, all slots busy | Many slots idle while a few tasks run |
Adding workers to a skewed stage is the most common wasted spend. Fix skew with a better partition key, salting, or AQE skew join handling, and then re-measure.
Reading autoscaling behavior
For autoscaling clusters, line up the Jobs tab timeline with the cluster event log. You want to see the cluster grow when the queue of pending tasks is long, and shrink when slots go idle. Two patterns indicate poor bounds:
- The cluster sits at the maximum for most of the run and stages are still long. The maximum is too low, or the job is limited by something other than workers.
- The cluster never rises above the minimum. The minimum is already larger than the work needs, or the stages have too few tasks to trigger a scale-up.
Remember that a scale-up takes time to provision nodes. A short job can finish before new workers join, so the extra capacity was paid for and never used.
A short checklist
- Record cores, shuffle partitions, and AQE status.
- Look at the cluster metrics for the overall shape: idle, saturated, or imbalanced.
- Open the slowest stage and compare median and maximum task values.
- Check spill and GC time to judge memory per core.
- Check the executor timeline to see how autoscaling behaved.
- Make one change and measure again.
Exercise
A stage on a 64-core cluster has 400 tasks. The median task takes 6 seconds, and the maximum takes 190 seconds. CPU across the cluster is below 20% during the last two minutes of the stage. Spill is zero. Will doubling the workers shorten this stage? What should you investigate instead?
Show answer
No. The maximum task time is about 30 times the median, and CPU is low at the end while a few tasks run. That is skew: one or a few partitions hold far more data, and the stage ends only when the slowest task ends. More workers would add idle slots. Investigate the partition key and the data distribution, check whether AQE skew join handling is enabled, and consider salting the hot key. After fixing the skew, re-measure and then decide whether the cluster size needs to change.
Exercise
The Metrics tab shows memory near its limit on every worker, the Executors tab shows GC time at 25% of task time, and the Stages tab shows non-zero disk spill in the median task. CPU is about 50%. Which node change fits, and why?
Show answer
Move to a memory-optimized instance family, or one with more memory per core. The evidence shows tasks are short of memory: high GC time, spill in the median task, and memory pressure on all workers. CPU is not saturated, so extra cores would not help. You could also reduce the partition size so each task needs less memory, but the primary sizing fix is more memory per core.