Sub topic 8
Setting minimum and maximum workers
This chapter assumes you know how autoscaling adds and removes workers (covered in the previous chapter). It also assumes you can read Spark job, stage, and task metrics, and that you know how a shuffle moves data between workers. Here you will learn how to pick the two numbers that bound an autoscaling cluster, and what goes wrong when you pick them badly.
What the bounds control
An autoscaling cluster is defined by min_workers and max_workers. The driver is not counted in either number. Databricks starts the cluster with at least min_workers and never goes below that count while the cluster is running. It adds workers when tasks are queued and the cluster is busy, and it never goes above max_workers.
The two bounds do different jobs:
min_workerssets the floor. It decides your startup time, your idle cost, and the capacity available the moment a job begins.max_workerssets the ceiling. It decides your worst-case cost and your fastest possible run time.
A wide range gives Databricks more room to adapt. A wide range also makes cost and run time harder to predict. Your task is to make the range wide enough for the work and narrow enough to stay safe.
{
"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 specification replaces a fixed num_workers with an autoscale range of 2 to 8 workers. Do not set both num_workers and autoscale in the same request.
Choosing the minimum
Pick the minimum from the smallest amount of work the cluster will ever do while it is running, not from the average.
Consider these factors:
- Memory floor. Cached data and broadcast tables must fit on the workers that exist at the start. If a job caches a 40 GB dataset, a minimum of one small worker forces spills or out-of-memory errors until scale-up happens.
- Startup latency. Workers added during a run take minutes to arrive, and tasks that start before then run on fewer cores. A higher minimum reduces the time spent waiting for scale-up.
- Idle cost. Every worker at the minimum is billed for as long as the cluster is up. This is why a minimum of 1 or 2 suits shared interactive clusters, where long quiet periods are common.
- Fault tolerance. With a minimum of 1, the loss of that worker interrupts everything running. A minimum of 2 or more gives some headroom.
A minimum of 0 is not available on multi-node autoscaling clusters. If you need zero workers, use a single-node cluster, which has a driver only.
Choosing the maximum
Pick the maximum from three limits, and take the lowest.
Useful parallelism
More workers help only if there are tasks to run on them. The number of tasks in a stage is bounded by its partition count. If the widest stage has 200 shuffle partitions and each worker has 4 cores, then 50 workers can run every task in one wave. Workers beyond that sit idle during that stage.
You can check the numbers that matter 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}")Use the result as a ceiling. If shuffle_partitions is 200 and each worker has 4 cores, a max_workers above about 50 cannot speed up that stage. With adaptive query execution (AQE) enabled, Spark may coalesce shuffle partitions, so the real task count can be lower still.
Cost budget
Multiply the maximum by the hourly price of one worker (DBUs plus cloud instance cost) to get the worst-case hourly spend. If a runaway query would cost more than you accept, lower the maximum. Autoscaling responds to load, not to your budget, so the cap is the only protection you have.
Account and cloud limits
Cloud vCPU quotas, regional instance availability, and workspace limits can stop a cluster from reaching its maximum. If a request for workers fails, the cluster stays smaller than you configured. Check your quota before you set a large maximum.
A worked example
A nightly ETL job reads about 400 GB, writes a daily aggregate, and runs on i3.xlarge workers with 4 cores each. The job has a heavy join stage that uses 400 shuffle partitions. Most of the run is a light read-and-filter phase.
- Useful parallelism: 400 partitions on 4-core workers is 100 workers for one wave. Two waves need 50 workers, which usually costs little extra time.
- Cost budget: the team accepts about 12 workers at peak.
- Quota: the account has room for 20 instances.
The lowest limit is the budget, so max_workers is 12. The light phase needs about 3 workers, and the cached dimension table is small, so min_workers is 3. The join stage then runs in about 400 / 48 ≈ 9 waves, which the team accepts for the cost saving.
| Setting | Value | Reason |
|---|---|---|
| min_workers | 3 | Covers the light phase and the dimension table |
| max_workers | 12 | Budget limit, below quota of 20 and useful parallelism of 100 |
| autotermination_minutes | 30 | Stops billing after the run ends |
How scaling behaves inside the bounds
Autoscaling reacts to backlog. When many tasks are pending, the cluster scales up, and when workers are idle for a while, it scales down. Two effects follow from this.
- Scale-up is not instant. Instances take time to provision, so a short job may finish before new workers join. A job under a few minutes often gains little from a high maximum.
- Scale-down removes workers. Shuffle files stored on a removed worker can be lost, and Spark must recompute them. The cost appears as slower stages or retried tasks after a long job with a large shuffle.
The diagram below shows how a cluster moves between the bounds during one run.
Common mistakes
- Setting min equal to max. This is a fixed-size cluster with extra settings. Use
num_workersinstead if you want a fixed size. - Setting a very high maximum "just in case". One skewed or accidental cross join can then consume the whole range and run up a large bill.
- Setting the minimum too low for cached data. Executors spill or fail until scale-up completes.
- Ignoring the shuffle partition count. Workers beyond the useful parallelism cost money and add no speed.
- Expecting autoscaling to fix skew. If one task holds most of the data, extra workers do not help. Fix the skew instead.
You can enforce that cap with a policy definition:
{
"autoscale.max_workers": {
"type": "range",
"maxValue": 8,
"defaultValue": 4
},
"autotermination_minutes": {
"type": "range",
"minValue": 10,
"maxValue": 120,
"defaultValue": 30
}
}This policy limits the worker count to 8 and keeps the auto-termination setting between 10 and 120 minutes.
Starting values
If you have no measurements yet, use these as a first guess and then refine them with the Spark UI:
- Interactive shared cluster: min 1 or 2, max 4 to 8.
- Scheduled ETL job: min at the light-phase need, max at the lower of the budget and the useful parallelism.
- Cluster with large cached datasets: min sized so the cache fits without scale-up.
Exercise
A job has a widest stage of 160 tasks. Workers have 8 cores each, and the team's budget allows 30 workers. The account quota allows 25 instances. What is a sensible max_workers, and why?
Show answer
20. The useful parallelism is 160 tasks / 8 cores = 20 workers for a single wave. That is lower than the budget (30) and the quota (25), so it is the binding limit. Workers beyond 20 would sit idle in that stage.
Exercise
A cluster has min_workers 1 and runs a notebook that caches a 60 GB table at the start of each session. Users report slow first queries and occasional out-of-memory errors. What change to the bounds do you make, and why?
Show answer
Raise minworkers so the cached table fits in memory on the workers that exist at startup. With only one worker, the cache spills or fails until scale-up completes. Size the minimum from the cache plus working memory, and keep maxworkers unchanged unless you also see queued tasks at the limit.