Sub topic 8
Autoscaling and auto termination
This chapter covers the two settings that most affect idle spend on classic compute: autoscaling, which changes the number of workers while work runs, and auto termination, which shuts the cluster down when nobody is using it. It assumes you know the driver and worker structure of a cluster, the difference between all-purpose and job clusters, and how to read JSON. Pricing details come in the next chapter.
Two controls, two kinds of waste
A classic cluster wastes money in two ways:
- Over-provisioning. The cluster has more workers than the current work needs.
- Idleness. The cluster is running but nothing is executing on it.
Autoscaling addresses the first. Auto termination addresses the second. They are independent settings and you normally use both on an all-purpose cluster.
| Setting | Problem it addresses | Applies to | Where you set it |
|---|---|---|---|
| Autoscaling (`autoscale`) | Wrong number of workers for the current load | All-purpose and job clusters | `min_workers` and `max_workers` |
| Auto termination (`autotermination_minutes`) | Cluster running with no activity | All-purpose clusters | Minutes of inactivity |
Job clusters do not need auto termination. They are created when a run starts and terminated when it finishes. Serverless compute has neither setting, because Databricks manages sizing and shutdown for you.
Autoscaling
Fixed size versus autoscaling
A cluster definition has one of two sizing forms:
num_workersgives a fixed size. The cluster has exactly that many workers.autoscalegives a range, withmin_workersandmax_workers. Databricks picks a worker count inside the range.
You cannot set both. The driver is never autoscaled. There is always exactly one, so a driver that is too small is not fixed by a wide worker range.
How scaling decisions work
Databricks watches the Spark workload, not just CPU use. The details below describe standard autoscaling on classic compute. Treat the specific timings as behavior that can change between releases.
- Scale up. When tasks are waiting for free slots, the cluster adds workers. It does not add them one at a time. A large backlog can take the cluster from the minimum toward the maximum in a small number of steps.
- Scale down. When workers are underused, the cluster removes them. Databricks looks at whether a worker still holds shuffle data that later stages need, so it can remove workers even while the cluster is not completely idle.
- Different patience by type. Job clusters scale down more quickly than all-purpose clusters. This fits their purposes. A job cluster serves one known workload, while an all-purpose cluster serves a person who may run another query a few seconds later.
Newly added workers are not available instantly. A cloud virtual machine must start and join the cluster, which takes minutes. A short query that arrives after a scale-down can run on fewer workers than you expected, or wait for new ones.
Choosing min and max
Start from the workload, then adjust from what you observe.
- Set
min_workersto the smallest size that still gives acceptable latency for a single small query. For many interactive clusters this is 1 or 2. - Set
max_workersto a ceiling you are willing to pay for. This is a cost cap as much as a performance setting. It limits the worst case when someone runs an unbounded query. - Keep the range proportional. A range like 2 to 200 for a team notebook cluster invites a surprise bill. A range like 1 to 4 is predictable.
When autoscaling helps least:
- Steady, predictable jobs. If a nightly job always needs eight workers, a fixed
num_workersof 8 avoids scaling churn and gives repeatable run times. - Short jobs. If the whole job runs for a few minutes, scale-up delay can take a large share of the run.
- Structured Streaming. Classic autoscaling reacts to batch backlog and is a poor fit for continuous streams. Pipelines built on Lakeflow Declarative Pipelines (formerly Delta Live Tables) have a separate feature, enhanced autoscaling, designed for streaming workloads. It is not the same mechanism.
- Jobs with heavy shuffles. Removing workers that hold shuffle data forces recomputation or data movement. A fixed size can finish faster.
Autoscaling in a job definition
You can use autoscaling in a job cluster as well. This definition replaces the fixed num_workers from a job cluster with a range:
{
"name": "nightly-etl",
"job_clusters": [
{
"job_cluster_key": "etl_cluster",
"new_cluster": {
"spark_version": "15.4.x-scala2.12",
"node_type_id": "i3.xlarge",
"autoscale": {
"min_workers": 2,
"max_workers": 8
},
"data_security_mode": "USER_ISOLATION"
}
}
],
"tasks": [
{
"task_key": "ingest",
"job_cluster_key": "etl_cluster",
"notebook_task": {
"notebook_path": "/Workspace/Users/[email protected]/ingest"
}
}
]
}The node type i3.xlarge is an AWS example. Azure and GCP use different names.
Auto termination
What it does
Auto termination stops an all-purpose cluster after a set number of minutes with no activity. Termination releases the virtual machines, so you stop paying the cloud provider for them. Databricks charges for DBUs while the cluster is running, so those stop as well.
A terminated cluster is not deleted. Its configuration stays in the workspace, and you can start it again. Databricks retains the configuration of terminated clusters for a limited period unless you pin the cluster. If you need a cluster definition to live permanently, pin it or keep it in code (covered in the chapter on defining compute with the APIs).
What counts as activity
A cluster counts as inactive when all commands on it have finished. That includes Spark jobs, Structured Streaming queries, and JDBC calls. The clock starts when the last of them ends.
Several consequences follow from this:
- A notebook that is open but not running anything does not keep the cluster alive.
- A streaming query that runs continuously does keep it active, so the cluster never terminates. Use a job cluster for streaming that has a defined lifecycle.
- A long-running cell, such as a
sleepor a polling loop, counts as activity. Forgotten loops are a common reason a cluster never shuts down. - Attached notebooks, open browser tabs, and a user who is "just thinking" do not count as activity.
Choosing the timeout
The setting is autotermination_minutes. The allowed range is 10 to 10000 minutes, and 0 means no automatic termination. The workspace UI proposes a default of 120 minutes. A cluster created through the Clusters API with the field omitted has auto termination disabled, which is a common source of runaway cost in scripts.
Match the value to how people work:
- 15 to 30 minutes suits personal development clusters. Restart cost is low and you are likely to notice the pause.
- 60 minutes suits shared team clusters, where use is spread through the day with gaps.
- Longer values rarely pay off. Compare the cost of the idle time with the cost of waiting for a restart, which is usually a few minutes.
Each restart means a wait. A cluster that terminates in the middle of a lunch break costs you startup time, but a cluster that runs overnight costs far more than that wait. Instance pools (a later chapter) shorten restart time by keeping prepared virtual machines on hand.
Combining both settings
This is the all-purpose cluster body from the Clusters API (POST /api/2.1/clusters/create), with both settings:
{
"cluster_name": "team-interactive",
"spark_version": "15.4.x-scala2.12",
"node_type_id": "i3.xlarge",
"autoscale": {
"min_workers": 1,
"max_workers": 4
},
"autotermination_minutes": 60,
"data_security_mode": "USER_ISOLATION"
}This cluster runs on one worker when it is quiet, grows to at most four workers under load, and shuts down after an hour of inactivity. The data_security_mode value USER_ISOLATION is standard access mode, which allows several users to share the cluster under Unity Catalog.
Lifecycle of an autoscaling cluster
The diagram shows the states an all-purpose cluster passes through, and which setting triggers each transition.
Common mistakes
- Leaving auto termination off in API scripts. Always set
autotermination_minutesexplicitly for all-purpose clusters you create from code. - Setting
max_workersvery high "just in case". One expensive query can then scale the cluster to a size you did not intend. Cluster policies (a later chapter) can cap this range for everyone. - Using
min_workersof 0 or a large minimum without thinking. A very low minimum saves money but slows the first query after idle time. A high minimum pays for workers you rarely use. - Expecting autoscaling to fix a slow driver. Driver bottlenecks, such as
collect()on a large result, do not improve with more workers. - Using autoscaling for a latency-sensitive streaming job. Use a fixed size, or a pipeline with enhanced autoscaling.
Exercise
A team shares one all-purpose cluster for ad hoc SQL and notebook work during business hours. It is created through the Clusters API with num_workers: 6 and no autotermination_minutes. Users report the cluster is often running at night, and queries are slow at 9 a.m. only when many people start at once. Rewrite the sizing and termination settings, and explain each choice.
Show answer
Replace num_workers: 6 with autoscale of about min_workers: 1 and max_workers: 6 (or a similar cap), and add autotermination_minutes: 60. The omitted field disabled auto termination through the API, which explains the cluster running at night. The 60-minute value shuts it down after an idle hour while avoiding frequent restarts during the working day. Autoscaling lets the cluster sit small when quiet and grow when many users start at once. The max_workers value caps cost. Scale-up takes minutes, so the first queries at 9 a.m. can still be slow. If that matters, raise min_workers or use instance pools to cut the wait.