Sub topic 15
Reading utilization in the Spark UI and metrics
This chapter assumes you know Spark jobs, stages, tasks, partitions, and shuffles, and that you can run notebooks on a Databricks cluster. Sizing starts with evidence. The Spark UI and the cluster metrics tell you whether a cluster is undersized, oversized, or fine.
Two sources of evidence
The cluster metrics tab shows the machines: CPU, memory, and network per node over time. The Spark UI shows the work: jobs, stages, tasks, executors, and storage. Use metrics to ask "are the machines busy?" and the Spark UI to ask "why?"
First, check what the cluster offers. This cell shows the task slots and the shuffle settings a job will use.
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 core count with the partition counts you see in the Spark UI stages. A stage with fewer tasks than cores leaves slots idle.
What to check
- Cluster metrics, CPU. Sustained CPU near 100% during a job suggests the cluster is compute-bound and may be too small. CPU that stays low while the job runs suggests idle capacity or a bottleneck elsewhere, such as I/O.
- Cluster metrics, memory. Memory close to the limit on many nodes points to a memory-bound job. Choose a memory-optimized node type before you add more workers.
- Stages tab, task counts. Count tasks per stage against total cores. Fewer tasks than cores wastes slots. Many more tasks than cores is normal, because tasks run in waves.
- Stages tab, task duration. Open the summary metrics. If the maximum task time is far above the median, the stage has skew. More workers will not fix skew.
- Spill. Nonzero "Spill (memory)" and "Spill (disk)" columns mean executors lacked memory for a shuffle or sort. This is a sign of too little memory per core.
- Executors tab. Look at GC time. High garbage collection time relative to task time also points to memory pressure.
Reading the pattern
| Symptom | Likely cause | Typical response |
|---|---|---|
| CPU near 100%, no spill, long runtime | Undersized for compute | Add workers or cores |
| Spill to disk, high GC time | Too little memory per core | Use a memory-optimized node type |
| Low CPU, few tasks per stage | Too little parallelism | Repartition, or reduce cluster size |
| One task far slower than the median | Data skew | Fix the skew, not the cluster size |
| Workers idle for long stretches | Oversized or poorly bounded autoscaling | Lower max workers or tune the workload |
Common errors
- Reading metrics from an idle period. Look at the time window of the job itself.
- Judging from a single run. Compare runs on similar data volumes.
- Adding workers to fix skew or spill, which raises cost without a speedup.
Exercise
A stage shows 40 tasks on a cluster with 128 total cores. CPU averages 25%, and there is no spill. Name two possible responses.
Show answer
Only 40 of 128 slots can run at once, so the stage is under-parallelized. Either raise the partition count so more tasks run in parallel, or shrink the cluster to match the work. Adding workers would not help.