Sub topic 5
All-purpose clusters for interactive work
An all-purpose cluster is a Databricks cluster you create and keep running so that people can attach notebooks to it, run commands, and see results right away. This chapter explains how all-purpose clusters behave, how to configure them, and the mistakes that most often cause cost or access problems.
Prerequisites
This chapter assumes you already know the following:
- The Databricks workspace and how to attach a notebook to compute.
- The driver and worker structure of a Spark cluster.
- The meaning of access modes and Unity Catalog at a basic level. Earlier chapters cover both.
- How to read a JSON request body.
What an all-purpose cluster is for
All-purpose compute is built for interactive work. You use it for exploratory analysis, notebook development, debugging, and ad hoc queries. Three properties define it:
- Lifecycle. You or an administrator starts it, and it keeps running until someone terminates it or it shuts down after being idle. It does not depend on any one job run.
- Sharing. Several users and several notebooks can attach to the same cluster at the same time, if the access mode allows it.
- Cost profile. You pay for the virtual machines and for Databricks Units (DBUs) for as long as the cluster is running, whether or not anyone is running a command.
That last property drives most of the design decisions in this chapter. An interactive cluster spends most of its time waiting for a person to type, so the settings that limit idle time matter more here than on any other kind of compute.
Creating a cluster
You can create an all-purpose cluster in the workspace UI, with the Clusters API, or with infrastructure tools that wrap that API. The request below creates a small shared cluster.
{
"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"
}You send this body to POST /api/2.1/clusters/create. The node type i3.xlarge is an AWS example. Azure and GCP use different instance names, so check the node types available in your cloud and region.
Each field does a specific job:
| Field | What it controls |
|---|---|
| `cluster_name` | The label users see when they pick compute. |
| `spark_version` | The Databricks Runtime version. |
| `node_type_id` | The virtual machine type for the driver and workers. |
| `autoscale` | The minimum and maximum number of workers. |
| `autotermination_minutes` | How many idle minutes pass before the cluster terminates. |
| `data_security_mode` | The access mode. `USER_ISOLATION` is shown as standard access mode in the UI. |
If you set autoscale, you do not set num_workers. If you want a fixed size, use num_workers and omit autoscale.
Access mode on a shared cluster
The access mode decides who can use the cluster and what governance is enforced on data access. For an all-purpose cluster that several people share, standard access mode (USER_ISOLATION) is the usual choice. It isolates users from one another and enforces Unity Catalog permissions, so each person sees only the data they are allowed to see.
Dedicated access mode assigns the cluster to a single user or group. Use it when a workload needs features that standard mode restricts, such as certain languages or libraries that need unrestricted access to the machine. Because the cluster is tied to one identity, it is a poor fit for a general team cluster.
A common error is to pick a mode for convenience and then discover that Unity Catalog tables are not available or that a command is blocked. If a notebook fails with a permission or "not supported" message that mentions the cluster, check the access mode first.
Autoscaling
Autoscaling lets the cluster add workers when load is high and remove them when load drops, within the limits you set. For interactive use, autoscaling helps in two ways. A small minimum keeps idle cost low. A higher maximum lets a heavy query finish faster without you resizing the cluster by hand.
Keep these points in mind:
- The driver is never autoscaled. A cluster always has exactly one driver, so a driver that is too small is not fixed by raising
max_workers. - Scaling takes time. New workers must be provisioned and join the cluster, so a short query may finish before extra capacity arrives.
- Scaling down only happens when workers are idle. Cached data or shuffle data held on a worker can delay its removal.
- A minimum of 1 keeps one worker ready. A minimum of 0 is not the same as a single-node cluster, and a cluster with zero workers has no place to run Spark tasks. Use a single-node configuration if you intend to run without workers.
The following diagram shows the idea. Load rises and falls, and the worker count follows it inside the minimum and maximum.
Auto termination
Auto termination shuts the cluster down after it has been idle for a set number of minutes. In the example, autotermination_minutes is 60. This is the single most effective setting for limiting waste on all-purpose compute.
What counts as idle matters. A cluster is idle when no commands are running on it. A notebook that is open but not executing does not keep the cluster alive. A running streaming query or a scheduled command does count as activity.
Choose the timeout as a trade-off:
- A short timeout, such as 20 to 30 minutes, saves money but means people restart the cluster more often.
- A long timeout is convenient but pays for every idle minute. Over a week, a cluster left running overnight and through a weekend costs far more than the work done on it.
A terminated cluster is not deleted. Its configuration is kept, and anyone with permission can restart it. Start-up takes a few minutes while the cloud provider supplies virtual machines, installs the runtime, and starts Spark. Libraries installed on the cluster are reinstalled at start.
A common error is setting auto termination to a very large value or disabling it. Treat that as a cost incident waiting to happen, and use a cluster policy to enforce a maximum.
Cost behavior
An all-purpose cluster bills while it is running. Two charges apply:
- Cloud infrastructure. The provider charges for the driver and worker virtual machines, billed to your cloud account.
- DBUs. Databricks charges DBUs based on the node types, the runtime, and the compute type. All-purpose compute has a higher DBU rate than job compute for the same hardware.
Because of the higher rate and the idle time, running scheduled production work on an all-purpose cluster is usually the most expensive way to run it. Keep all-purpose clusters for development and exploration, and move recurring work to job compute or serverless jobs. Later chapters cover that comparison in detail.
Sharing and contention
Sharing a cluster saves money because several people split one bill. It also creates contention. All users compete for the same driver and workers, so one large query can slow everyone else.
Practical ways to reduce contention:
- Keep one cluster per team or purpose, not one for the whole company.
- Use autoscaling with a maximum that fits the heaviest normal query.
- Ask people to avoid collecting large results to the driver, for example with
collect()on a big DataFrame, because that can exhaust driver memory and affect every attached notebook. - Restart the cluster during a quiet period if its driver memory has grown from long-lived sessions.
Managing clusters from code
You can read and change an existing cluster through the Clusters API. The Python snippet below lists all-purpose clusters in a workspace and prints their state and idle timeout. It uses the requests library and reads the host and token from environment variables.
import os
import requests
host = os.environ["DATABRICKS_HOST"].rstrip("/")
token = os.environ["DATABRICKS_TOKEN"]
response = requests.get(
f"{host}/api/2.1/clusters/list",
headers={"Authorization": f"Bearer {token}"},
timeout=30,
)
response.raise_for_status()
for cluster in response.json().get("clusters", []):
if cluster.get("cluster_source") in ("UI", "API"):
print(
cluster["cluster_name"],
cluster["state"],
cluster.get("autotermination_minutes", "not set"),
)The cluster_source field filters out clusters that Databricks created for job runs. A listing that shows not set or 0 for the idle timeout points to a cluster that never shuts down by itself.
Common problems
| Symptom | Likely cause | What to check |
|---|---|---|
| Cluster bill is high and usage looks low | Auto termination is off or very long | `autotermination_minutes` on each cluster |
| Unity Catalog table not visible | The access mode does not enforce Unity Catalog | `data_security_mode` |
| Query is slow only at certain times | Other users share the cluster | Who else is attached, and the driver load |
| Notebook disconnects with out-of-memory errors | Large results collected to the driver | Driver node size and the query that ran |
| Cluster does not start | Cloud capacity or quota limit for the node type | The termination reason in the cluster event log |
| Libraries missing after a restart | Libraries were installed in a notebook session only | Install libraries at the cluster level or in the notebook each time |
Summary
An all-purpose cluster is long-lived, shareable, and billed for as long as it runs. Pick standard access mode for team clusters that use Unity Catalog, use autoscaling with a small minimum, and always set a bounded auto termination time. Use it for development and ad hoc work, and move scheduled workloads elsewhere.
Exercise
A team cluster is configured with autoscale minworkers 2, maxworkers 8, and autotermination_minutes 0. The monthly bill is much higher than the team's actual usage suggests. What is the most likely cause, and which two changes would you make?
Show answer
An autoterminationminutes value of 0 disables auto termination, so the cluster keeps running, and billing, while nobody is using it. Set autoterminationminutes to a bounded value such as 30 or 60, and lower min_workers to 1 so that the idle floor is smaller. You can also add a cluster policy that caps the idle timeout so new clusters cannot disable it.
Exercise
Several analysts share one all-purpose cluster and need Unity Catalog permissions enforced per person. Which datasecuritymode do you choose, and why is dedicated access mode a poor fit?
Show answer
Choose USER_ISOLATION (standard access mode). It isolates users and enforces each person's Unity Catalog permissions. Dedicated access mode ties the cluster to a single user or group, so it does not suit a cluster that many individuals share.