Sub topic 10
Stopping idle clusters automatically
This chapter assumes you know how to create a Databricks cluster, you understand the DBU-plus-infrastructure cost model from the earlier chapter, and you have used the Clusters API or the cluster UI. Auto-termination is the simplest cost control available. It is also easy to misconfigure, so this chapter covers the rules, the edge cases, and how to choose a value.
Why idle clusters are expensive
An all-purpose cluster bills for two things while it is running: DBUs to Databricks and virtual machines to your cloud provider. Both charges continue when no one is running a command. A cluster that sits idle overnight costs almost as much as one that runs a job all night.
Auto-termination stops a cluster after a period with no activity. The cluster configuration is kept, so you can restart it later. Only the running machines are released.
How auto-termination works
You set autotermination_minutes in the cluster specification. Databricks tracks activity on the cluster. When no activity has occurred for that many minutes, the cluster terminates.
The setting has these properties:
- The unit is whole minutes. The UI accepts values from 10 to 10,000 minutes. A value of 0 disables auto-termination in the API.
- The timer counts time with no Spark job running and no command running. Commands in an attached notebook reset it.
- A terminated cluster keeps its configuration. You can restart it manually, or a job or notebook attach can start it, depending on how it is set up.
- Libraries installed at the cluster level are reinstalled on restart. Data cached in memory or on local disk is lost.
Here is the cluster specification from the autoscaling chapter, with the relevant field highlighted by its position at the end:
{
"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 cluster scales between 2 and 8 workers and shuts down after 30 idle minutes.
What counts as activity
Auto-termination looks at whether work is running, not at whether a user is logged in. This leads to some cases that surprise people.
| Situation | Does it reset the idle timer? |
|---|---|
| A notebook command is running | Yes |
| A Spark job or stream is running | Yes |
| A notebook is open but nothing is executing | No |
| A browser tab is left open on the cluster page | No |
| A long-running Structured Streaming query | Yes, for as long as the query runs |
| A background thread in your code with no Spark job | Not reliably, treat it as idle |
The streaming case matters. A cluster that hosts a continuous stream will never reach the idle threshold, so auto-termination does not stop it. That is usually the right behavior. It also means a forgotten test stream keeps a cluster alive indefinitely. Stop test streams explicitly when you are done.
The last row is a common trap. If you run a long computation that does not use Spark, such as a single-node Python loop on the driver, the cluster may treat the time as idle and terminate under you. Keep the timeout longer than your longest quiet stretch, or run the work as a job.
Choosing the timeout
The right value balances two costs: the money wasted while idle, and the time you lose waiting for a restart.
- A restart takes a few minutes for a cluster built from scratch. With an instance pool it is shorter, as the pools chapter explains.
- A short timeout saves money but interrupts people who step away for a coffee or a meeting.
- A long timeout is comfortable but wastes spend, especially on large clusters.
A useful rule is to scale the timeout with the hourly cost of the cluster. A small personal cluster can idle for an hour with little harm. A large shared cluster of many workers should stop much sooner.
| Cluster type | Suggested range | Reasoning |
|---|---|---|
| Personal development cluster | 30 to 60 minutes | Low cost, users pause often |
| Shared interactive cluster | 20 to 30 minutes | Higher cost, many users keep it busy during the day |
| Large ML or exploration cluster | 10 to 20 minutes | High hourly cost, restart delay is acceptable |
| Cluster with expensive cached data | Longer, or avoid termination | Cache is lost on restart |
These are starting points, not rules. Check your own usage in the cluster event log and adjust.
Job clusters need no timeout
A job cluster is created for one run of a job and terminates when the run finishes. It does not need autotermination_minutes. This is one reason scheduled production work should run on job clusters, not on a shared all-purpose cluster. Each run pays only for the time it needs, and there is no idle tail.
Enforcing a timeout with a policy
A timeout is only a guardrail if users cannot disable it. Cluster policies let an administrator set bounds on the field. This policy fragment limits the timeout 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
}
}With this policy, a user cannot create a cluster with a timeout of 0 or 500. The policy is covered further in its own chapter. The point here is that setting a default alone does not protect you, because users can change it. A range with a maximum does.
Edge cases
- Terminated is not deleted. A terminated cluster remains in the list and can be restarted. Clusters that go unused for a long time may be removed from the list unless they are pinned.
- Restart does not restore state. Notebook variables, temporary views, and cached DataFrames are gone after restart. Reattach the notebook and rerun the cells.
- Pools hold idle instances separately. An instance pool can keep idle instances ready, and those have their own idle timeout and may incur cloud charges. Terminating the cluster releases instances to the pool, not back to the cloud provider.
- Spot loss is not auto-termination. If a spot worker is reclaimed, that is a different event. Auto-termination only reflects idleness.
- Time zone and schedules are not considered. The timer is relative, so it does not know about office hours. For a hard nightly stop, add a scheduled job that terminates the cluster through the API.
Check your understanding
Exercise
A team runs a shared interactive cluster with 8 workers. Developers complain that it stops while they are in lunch meetings, so an administrator sets autotermination_minutes to 0. What is wrong with this fix, and what would you do instead?
Show answer
A value of 0 disables auto-termination, so the cluster runs until someone stops it, and a large cluster left on overnight or over a weekend wastes DBUs and cloud spend. Instead, choose a moderate timeout such as 30 to 45 minutes that covers a typical break, and enforce a range through a cluster policy (for example minValue 10, maxValue 120) so no one can disable it. If restart delay is the real complaint, add an instance pool to shorten startup.
Exercise
A notebook runs a three-hour single-threaded Python computation on the driver with no Spark jobs. The cluster terminates after 30 minutes. Why, and how can you prevent it?
Show answer
Auto-termination tracks Spark and command activity, and a long computation that does not run Spark jobs may not be counted as activity, so the cluster can look idle. Prevent it by running the work as a job on a job cluster, by setting a timeout longer than the computation, or by moving the heavy part into Spark so activity is registered.