Sub topic 10
Stopping idle clusters automatically
This chapter assumes you know how to create a Databricks cluster, you understand that cost has a DBU part and a cloud infrastructure part, and you can read a Clusters API JSON specification.
Why idle clusters cost money
A running cluster bills for DBUs and for the cloud virtual machines, whether or not a query is running. A cluster that someone forgot over a weekend costs the same per hour as a busy one. Auto-termination is the setting that shuts a cluster down after a period with no activity. It is usually the cheapest saving you can make.
How auto-termination works
You set autotermination_minutes on the cluster. Databricks watches for activity, such as running Spark jobs, commands in a notebook, or active streaming queries. If none occurs for the set number of minutes, the cluster terminates. Termination releases the virtual machines, so billing stops. The cluster configuration is kept, and you can restart the cluster later.
{
"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 autoscales between 2 and 8 workers and terminates after 30 idle minutes. Setting the value to 0 disables auto-termination, so avoid it except where you have a specific reason.
Choosing the timeout
The right value balances savings against the cost of waiting for a restart. Starting a cluster takes several minutes, and caches in memory are lost on termination.
| Workload | Typical setting | Reason |
|---|---|---|
| Interactive notebooks | 30 to 60 minutes | People pause to think or read. A short timeout interrupts work. |
| Shared BI or ad hoc queries | 60 to 120 minutes | Usage is bursty and a cold start annoys users. |
| Scheduled jobs | Not needed | A job cluster terminates when the job finishes. |
Job clusters are created for a single run and removed afterward. For scheduled ETL, prefer a job cluster over an all-purpose cluster with a timeout. You pay only for the run, and the job also bills at a lower DBU rate than all-purpose compute.
Common errors
- Leaving the default or
0. Some workspaces create clusters with long timeouts or none. Check every all-purpose cluster. - Assuming an open notebook counts as activity. An attached but idle notebook does not keep the cluster alive. Only running commands and jobs count.
- Setting a very short timeout on an interactive cluster. Users then wait for restarts several times a day, and the lost time can cost more than the compute saved.
- Expecting termination during streaming. An active streaming query is activity, so the cluster stays up. This is correct, but it means a streaming cluster never idles out.
Enforcing a sensible range
Individual users will not always set a timeout. A cluster policy can require one. The next policy limits the value to between 10 and 120 minutes and sets a default of 30.
{
"autoscale.max_workers": {
"type": "range",
"maxValue": 8,
"defaultValue": 4
},
"autotermination_minutes": {
"type": "range",
"minValue": 10,
"maxValue": 120,
"defaultValue": 30
}
}The policy also caps max_workers at 8. A later chapter covers policies in more detail.
Exercise
A team's all-purpose cluster for notebook work has autotermination_minutes set to 0. A scheduled nightly ETL also runs on it. Name two changes that reduce cost, and say why.
Show answer
First, set a timeout such as 30 to 60 minutes, so the cluster stops when nobody is using it. Second, move the nightly ETL to a job cluster, which exists only for the run and bills at the lower jobs DBU rate. A cluster policy can then keep the timeout from being turned off again.