Sub topic 7
How Autoscaling Works
This chapter assumes you know Spark jobs, stages, tasks, and shuffles. It also assumes you have run notebooks or jobs on a Databricks cluster and know the difference between the driver and workers.
From fixed size to a range
A fixed-size cluster sets num_workers to one number. You pay for that many workers for the whole run, even when a stage needs fewer. Autoscaling replaces that number with a range. You set min_workers and max_workers, and Databricks adds or removes workers within that range while the cluster runs.
{
"cluster_name": "etl-autoscale",
"spark_version": "15.4.x-scala2.12",
"node_type_id": "i3.xlarge",
"autoscale": {
"min_workers": 2,
"max_workers": 8
},
"autotermination_minutes": 30
}This Clusters API specification starts with 2 workers and can grow to 8. It also stops the cluster after 30 idle minutes. Only workers scale. The driver stays one node for the life of the cluster.
What triggers a change
Autoscaling reacts to the load on the cluster, mainly the number of pending tasks. If many tasks are waiting for a free task slot, the cluster requests more workers, up to the maximum. If workers sit idle, it removes them, down to the minimum.
Scaling up is not instant. The cloud provider must supply new instances, and the runtime must start on them. A short job can finish before the new workers help.
What happens on scale-down
Removing a worker is riskier than adding one. A worker may hold shuffle files from an earlier stage. If that worker disappears, Spark may have to recompute that data. Databricks reduces this risk with features such as shuffle tracking and by not removing workers that still hold needed data. Even so, an aggressive scale-down can cost time on shuffle-heavy jobs.
Common errors
- Setting the minimum too low. A minimum of 1 means every job starts small and waits for scale-up. If the job runs often, raise the minimum.
- Setting the maximum too high. A large range does not make a job faster if it has few partitions. Extra workers receive no tasks and still cost money.
- Expecting autoscaling to fix a skewed stage. One long task on one worker leaves the others idle. More workers do not shorten it.
- Forgetting the driver. If the driver is too small, adding workers does not help.
Seeing the effect
You can check what the cluster currently offers from a notebook.
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}")Run this cell before and during a heavy stage. If defaultParallelism grows, workers were added. If shuffle partitions are far below the total cores, the job cannot use the extra capacity.
Exercise
A job has an autoscaling range of 2 to 16 workers, but its heavy stage only produces 8 tasks. Each worker has 4 cores. Why is a maximum of 16 wasteful for this stage?
Show answer
Eight tasks need only 8 task slots, which 2 workers (8 cores) already provide. The extra workers would have no tasks to run, so they add cost without adding speed. Raise the partition count or lower the maximum.