Sub topic 7
How Autoscaling Works
This chapter assumes you know Spark jobs, stages, tasks, and shuffles. It also assumes you can run notebooks or jobs in a Databricks workspace and know the difference between on-demand and spot instances. You should already have a starting size for a fixed cluster from the previous chapter.
What autoscaling does
A fixed-size cluster has one worker count, set in num_workers. It stays that size for its whole life. An autoscaling cluster has a range instead, with a minimum and a maximum number of workers. Databricks adds workers when the workload needs more capacity and removes them when it needs less.
The driver is never scaled. An autoscaling cluster always has exactly one driver, and the range applies only to workers. Each worker is the same node type, so scaling changes the number of nodes and never their size.
The goal is to avoid paying for idle workers during light phases of a job, and to avoid a slow job during heavy phases. A typical ETL job reads a lot, shuffles, and then writes a smaller result. The middle stages may need many cores and the final stages few.
The cluster specification
You turn autoscaling on by replacing num_workers with an autoscale block in the Clusters API specification.
{
"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
}The cluster starts with min_workers workers. It can grow to max_workers, and it can shrink back to min_workers. It never goes outside that range.
The autotermination_minutes field is separate from autoscaling. Autoscaling removes workers while the cluster is busy. Auto-termination stops the whole cluster after it has been idle for the given time. A later chapter covers it in detail.
The scaling signal
Autoscaling reacts to how much work is waiting, not to CPU percentages. The scheduler looks at the tasks that are running and the tasks that are queued for the current stages. If there are many more pending tasks than free task slots, the cluster needs more workers. If many slots sit empty, it can release workers.
This design is why Spark parallelism matters for autoscaling. A stage with 10 tasks can use at most 10 slots, so a cluster with 64 cores gains nothing from more workers during that stage. A stage with 2,000 tasks can keep a large cluster busy. Autoscaling can only help when there are enough tasks to spread out.
The exact algorithm depends on the compute type and runtime version, and Databricks has changed it over time. Treat the details as behavior to observe, not as a contract. What stays stable is the principle: scale on queued work, within your bounds.
Scaling up
When a stage produces more tasks than the current cores can run, the cluster requests more workers from the cloud provider. Two points matter here.
- Scale-up is not instant. Each new worker has to be provisioned by the cloud and start its Spark processes. On AWS that can take a few minutes, depending on the instance type and whether you use an instance pool. A short job may finish before the new workers are ready.
- Scale-up can be large. The cluster does not have to add one worker at a time. It can move from the minimum toward the maximum in a small number of steps when the backlog is large.
New workers help only the work that has not started yet. Tasks already running keep their slots. Later stages in the same job, and other jobs, use the new capacity.
Scaling down
Scale-down is more careful, because removing a worker can cost work. A worker holds three things that matter: running tasks, cached data, and shuffle files written by earlier stages.
- If a worker is running tasks, removing it kills them, and Spark must retry them.
- If a worker holds cached partitions, they are lost and must be recomputed or reread.
- If a worker holds shuffle output that later stages still need, removing it forces Spark to regenerate that output.
To limit this, Databricks removes workers only after they have been underused for a period, and it takes shuffle data into account. Some recent runtimes and compute types can keep a worker alive until its shuffle data is no longer needed. Others may remove it and let Spark recompute. This is a common reason an autoscaling job looks slower than the same job on a fixed cluster. The work is correct, but some of it runs twice.
A timeline of one job
The diagram shows a cluster with a range of 2 to 8 workers running a job with a heavy middle stage.
The delay in "Requesting workers" is where autoscaling loses time compared with a cluster that was already large. The delay in "Releasing idle workers" is where it can still charge you for capacity you no longer use.
Checking what the cluster offers
Before you trust a range, check how many task slots the cluster has right now and what parallelism your job will ask for. This cell prints both. Run it at different points in a job to see the cluster 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 at your maximum size, extra workers will sit idle. If adaptive query execution is enabled, Spark can coalesce small shuffle partitions at run time, which reduces the number of tasks and so reduces what autoscaling sees as demand.
Autoscaling and spot instances
Autoscaling combines with spot instances. The next specification keeps the driver on-demand and runs workers on spot capacity, with a fallback to on-demand.
{
"cluster_name": "etl-spot",
"spark_version": "15.4.x-scala2.12",
"node_type_id": "i3.xlarge",
"autoscale": {
"min_workers": 2,
"max_workers": 8
},
"aws_attributes": {
"first_on_demand": 1,
"availability": "SPOT_WITH_FALLBACK"
},
"autotermination_minutes": 30
}first_on_demand: 1 makes the first node, the driver, on-demand. Losing the driver ends the whole cluster, so it should not run on a node the cloud can reclaim. Workers use spot capacity when it is available and on-demand capacity when it is not. Autoscaling is what makes this useful: the cluster pays spot prices for the extra workers it adds during peaks.
Common errors
- Setting the minimum too low for a steady load. If every run needs at least 6 workers, a minimum of 1 only adds startup delay while the cluster scales up each time.
- Setting the minimum equal to the maximum. This is a fixed-size cluster with extra steps. Use
num_workersinstead. - Expecting more workers to fix a low-parallelism job. A stage with few tasks, or one skewed task that takes most of the time, does not speed up when workers are added.
- Caching heavily on an autoscaling cluster. Scale-down can evict cached data. Either keep the minimum high enough to hold it, or accept the recomputation cost.
- Judging cost by the maximum. The cluster only reaches the maximum if the workload demands it. Check the actual worker count over time in the cluster metrics before you decide the maximum is too high.
Summary of behavior
| Aspect | Fixed-size cluster | Autoscaling cluster |
|---|---|---|
| Worker count | Constant | Between minimum and maximum |
| Driver | One, constant | One, constant |
| Scaling signal | None | Queued and running tasks |
| Startup delay during a job | None after start | Present when scaling up |
| Risk on scale-down | None | Lost cache and shuffle data |
| Best fit | Predictable, short, or streaming work | Variable batch and interactive work |
Exercise
A job on a cluster with autoscale 2 to 10 workers has a shuffle stage with only 8 tasks. Each worker has 4 cores. The cluster is at 2 workers. Will the cluster likely scale up for this stage, and why?
Show answer
Probably not. At 2 workers there are 8 cores, which is enough to run all 8 tasks at once, so there is no queue of waiting tasks. Autoscaling responds to queued work, and this stage creates none. To use more workers, the job would need more partitions, for example a higher shuffle partition count, and enough data to justify them.
Exercise
Why does the spot configuration set firstondemand to 1, and what does the autoscale range add to that setup?
Show answer
firstondemand set to 1 keeps the driver on an on-demand node, because losing the driver ends the cluster. The autoscale range lets the cluster add spot workers only when the workload needs them, so the cheaper capacity is paid for only during peaks.