Sub topic 16
Tuning size with a measure-and-adjust loop
This chapter assumes you know Spark jobs, stages, tasks, and shuffles, and that you can run notebooks or jobs in Databricks. Earlier chapters gave you ways to pick a starting size. A starting size is a guess. This chapter shows how to turn it into a size you can defend.
The loop
Tune in small steps and change one thing at a time. That way you know which change caused which result.
- Set a baseline. Run the real job on real data volumes, not a sample. Record the run time, the cluster size, and the cost.
- Measure. Look at the Spark UI and the cluster metrics.
- Change one setting. Examples are the worker count, the node type, or the shuffle partition count.
- Rerun and compare. Keep the change only if it helps run time or cost.
- Stop when further changes give small gains.
Start from the cluster's parallelism
Use this cell to record what the cluster offers before and after each change.
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}")If the shuffle partition count is far below the core count, some cores sit idle during shuffles. Adding workers will not help in that case.
Reading the signals
| What you see | Likely cause | Adjustment to try |
|---|---|---|
| CPU stays high, tasks queue, stages run in many waves | Too few cores | Add workers or raise the autoscaling maximum |
| CPU stays low, many cores idle | Too many cores, or too few partitions | Remove workers, or increase partitions |
| Spill to disk or out-of-memory errors | Not enough memory per core | Use a memory-optimized node type |
| A few tasks run far longer than the rest | Data skew | Fix the skew. More nodes will not help |
| Autoscaling sits at the maximum for the whole run | The maximum is too low | Raise the maximum and compare cost |
| Autoscaling never leaves the minimum | The minimum is higher than needed | Lower it, or use a smaller fixed size |
Guardrails for the changes
- Run each variant more than once. Cloud nodes and cached data cause run-to-run variation.
- Change the node type and the worker count in separate rounds.
- Use a cluster policy to cap the maximum workers, so an experiment cannot grow without limit.
- Repeat the loop when the data volume or the job changes. A size tuned for last quarter's data can be wrong now.
Exercise
A nightly ETL job runs on an autoscaling cluster with 2 to 8 workers. The Spark UI shows the cluster at 8 workers for the whole run, CPU near 90%, and stages with many task waves. What is your first change, and how do you decide whether to keep it?
Show answer
Raise the maximum worker count (for example from 8 to 12) and leave everything else unchanged. Rerun the job and compare run time and cost per run with the baseline. Keep the change if run time drops enough that the cost per run stays the same or falls. If the run time barely improves, check for skew or too few shuffle partitions before adding more workers.