Sub topic 7
Auto-termination and autoscaling
This chapter assumes you know how Databricks clusters work, can read a cluster configuration in JSON, and understand that compute is billed per DBU plus cloud instance time. Two settings control most idle and oversized-cluster waste: auto-termination and autoscaling.
Why these two settings matter
A running cluster costs money whether or not it is doing work. Both the DBU charge and the cloud instance charge accrue for as long as the nodes exist. Two common problems follow:
- Idle clusters. Someone finishes a notebook session and leaves the cluster running overnight.
- Oversized clusters. A cluster is sized for its peak load and sits mostly empty the rest of the time.
Auto-termination addresses the first problem. Autoscaling addresses the second.
Auto-termination
Auto-termination shuts a cluster down after a set number of minutes with no activity. You set it with autotermination_minutes on all-purpose compute. When the timer expires, the cluster terminates and billing for its nodes stops. The configuration is kept, so you can restart it later.
Some points to know:
- Activity means running commands. A cluster with an attached but idle notebook is inactive. A running streaming query or a long Spark job counts as activity.
- Choose the timeout by usage pattern. For interactive development, 30 to 60 minutes is common. Shorter values save more but add restart waits of a few minutes.
- Job clusters need no timer. A job cluster is created for a run and ends when the run finishes. This is one reason jobs compute is usually cheaper than all-purpose compute for scheduled work.
- Restart cost. Termination clears the Spark cache and any local state. Data in tables or cloud storage is unaffected.
Autoscaling
With autoscaling, you give a minimum and maximum number of workers instead of a fixed count. Databricks adds workers when the workload has pending tasks and removes them when they are no longer needed.
{
"cluster_name": "etl-interactive",
"spark_version": "15.4.x-scala2.12",
"node_type_id": "i3.xlarge",
"autoscale": { "min_workers": 2, "max_workers": 8 },
"autotermination_minutes": 30
}This cluster runs on two workers at minimum and grows to eight under load. After 30 idle minutes, it terminates.
The diagram below shows how the two mechanisms act over the life of one cluster.
Choosing minimum and maximum
- Minimum. A higher minimum gives faster starts for bursts but you pay for those workers even at low load. Keep it as low as your latency needs allow.
- Maximum. The maximum is your cost ceiling for the cluster. A runaway query cannot scale past it. Cluster policies, covered in the next chapter, can cap this value for everyone.
Common errors
| Problem | Effect | Fix |
|---|---|---|
| Auto-termination disabled or set very high | Idle clusters bill for days | Set a default of 30 to 60 minutes and cap it in a policy |
| Fixed-size cluster for a bursty workload | Paying for peak size all day | Use autoscaling with a modest minimum |
| Minimum workers set equal to maximum | Autoscaling has no effect | Leave a real range between the two |
| Very large maximum with no policy | One heavy query can use a large share of the budget | Cap `autoscale.max_workers` in a policy |
| Autoscaling used for short, steady jobs | Little gain, since the load never changes | Use a fixed size matched to the job |
Checking the effect
After changing settings, confirm the results in billing data. Compare DBU usage per day for the affected clusters before and after the change, using system.billing.usage as shown in the earlier chapter on billing system tables. Cluster event logs also list resize and termination events, which show whether the settings behave as you expect.
Exercise
A data team runs an interactive cluster with a fixed size of 10 workers. Usage in the billing tables shows it active only during working hours, but DBUs are consumed around the clock. Name two settings you would change and the effect of each.
Show answer
Set autotermination_minutes (for example to 30) so the cluster shuts down after idle periods, which stops overnight DBU and instance charges. Replace the fixed 10 workers with autoscale (for example min 2, max 10) so the cluster uses fewer workers when load is light while keeping the same ceiling for peaks.