Sub topic 8
Autoscaling and auto termination
This chapter assumes you know what a driver and workers are, can create an all-purpose cluster in the workspace, and can read JSON. Autoscaling and auto termination are the two settings that most affect idle spend on classic compute. Classic compute is any cluster that runs in your own cloud account.
What each setting does
Autoscaling lets Databricks change the number of workers while a workload runs. You set a minimum and a maximum. The driver always runs, and the worker count stays between those two bounds.
Auto termination shuts a cluster down after a set number of minutes with no activity. A terminated cluster keeps its configuration and can be restarted. The virtual machines are released, so you stop paying the cloud provider for them.
The request body below creates an all-purpose cluster with both settings. It is the same body used with POST /api/2.1/clusters/create. The node type is an AWS example and differs on Azure and GCP.
{
"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 with 1 to 4 workers and stops after 60 idle minutes. USER_ISOLATION is standard access mode, covered in an earlier chapter.
How autoscaling behaves
Databricks looks at the pending work in the Spark stages. If tasks are queued and the cluster is below its maximum, it adds workers. If workers sit unused, it removes them, down to the minimum.
Choose the bounds with these points in mind:
- Set the minimum high enough that a typical query does not wait for new nodes. A minimum of 1 saves money but slows the first heavy query after a quiet period.
- Set the maximum as a cost ceiling. Autoscaling adds workers when it sees queued work, so a maximum of 4 caps how much the cluster can spend.
- If you set the minimum equal to the maximum, you get a fixed-size cluster. The Jobs example in the Jobs API uses
num_workersfor this.
How auto termination behaves
The timer counts minutes without activity on the cluster. Running a command resets it. A cluster with an attached notebook that is not running anything still counts as idle, so an open browser tab does not keep it alive.
The value is set in autotermination_minutes. Pick it based on how people work:
- 30 to 60 minutes suits a shared team cluster used through the day.
- 10 to 20 minutes suits personal development clusters.
- Setting 0 disables termination in the API. This is the most common cause of a surprise bill, because an idle cluster keeps accruing both charges.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| First query after a break is slow | Cluster terminated, or scaled to the minimum | Raise the minimum, or accept the restart time |
| Cluster never scales up | Maximum equals minimum, or the workload is not parallel | Check the bounds and the number of Spark tasks |
| Cluster scales up but the job is no faster | Work is stuck on the driver, such as `collect()` or single-threaded Python | Move the work into Spark operations |
| Large bill for an idle cluster | `autotermination_minutes` is 0 or very large | Set a timeout and enforce it with a policy |
Later chapters cover enforcing these values with cluster policies and reducing restart time with instance pools.
Exercise
A team cluster has autoscale minworkers 2, maxworkers 8, and autotermination_minutes 0. Developers finish at 18:00 and nobody uses it overnight. Name the problem and give a change that fixes it.
Show answer
With autoterminationminutes set to 0, the cluster never shuts down, so it keeps billing for the driver and at least 2 workers all night. Set autoterminationminutes to a value such as 60 (or 30 for lighter use). Autoscaling alone cannot fix this, because it cannot go below the minimum of 2 workers.
Exercise
A cluster has minworkers 1 and maxworkers 6. A user runs a heavy query after lunch, and it is slow for the first few minutes. Give one change that reduces the delay and one trade-off it brings.
Show answer
Raise min_workers, for example to 3. More workers are ready when the query starts, so less time is spent adding nodes. The trade-off is higher cost, since those workers bill whenever the cluster is running, even when it is quiet.