Sub topic 8
Enforcing limits with compute policies
This chapter assumes you can create clusters and jobs in a Databricks workspace. It also assumes you know what tags and DBUs are from the earlier chapters on attribution and billing. Here you will learn how a compute policy limits what users can create.
What a compute policy does
A compute policy is a set of rules attached to cluster creation. Each rule applies to one cluster attribute, such as the worker count or the auto-termination time. When a user creates compute under a policy, the form shows only allowed values. Fixed values are applied automatically.
Policies matter for cost because the other controls in this tutorial are advice until something enforces them. Auto-termination, autoscaling limits, and tags only help if every cluster uses them. A policy makes them the default, or the only option.
Anatomy of a policy definition
A policy definition is a JSON object. Each key is an attribute path, and each value is a rule. These are the rule types you will use most:
| Rule type | Effect |
|---|---|
| `fixed` | The attribute is set to one value. The user cannot change it. |
| `range` | The value must fall between `minValue` and `maxValue`. |
| `allowlist` | The value must be one of a listed set. |
| `forbidden` | The attribute cannot be set at all. |
| `unlimited` | No limit, but you can set a default or make the attribute required. |
Here is a policy that caps idle time, worker count, and DBU rate, and forces a team tag:
{
"autotermination_minutes": { "type": "range", "maxValue": 60, "defaultValue": 30 },
"autoscale.max_workers": { "type": "range", "maxValue": 8, "defaultValue": 4 },
"dbus_per_hour": { "type": "range", "maxValue": 20 },
"custom_tags.team": { "type": "fixed", "value": "data-eng" }
}Read it line by line:
autotermination_minutescan be at most 60. If the user does not choose, it is 30. This stops clusters from running idle all night.autoscale.max_workerscan be at most 8. This caps how far a cluster can scale out.dbus_per_hourcan be at most 20. This is a virtual attribute. It limits the DBU rate of the cluster, so it blocks large node types as well as large worker counts.custom_tags.teamis fixed todata-eng. Every cluster created under the policy carries that tag, so the usage appears under that team insystem.billing.usage.
How a policy is applied
A user sees only the policies they have permission to use. They pick one, and Databricks validates the cluster configuration against it.
Giving each team its own policy
A common design is one policy per team or environment. The tag value differs in each, so attribution comes for free. A development policy might allow small clusters and short idle times. A production policy might allow larger clusters but fix the env tag to prod.
Permissions control who can use which policy. Grant the "Can use" permission on a policy to a group. Then remove the unrestricted cluster creation entitlement from users who should work only through policies. If you skip this step, users can still create unrestricted compute, and your limits do nothing.
Common errors
- Forgetting the entitlement. Policies are optional for users who keep unrestricted cluster creation. This is the most common reason a policy appears to have no effect.
- Editing a policy and expecting old clusters to change. Existing clusters are marked as out of compliance with the policy. They are not fixed automatically, so you must edit or recreate them.
- Setting only a
maxValueand nodefaultValue. Users then start from a larger default than you intended. - Leaving jobs out. Job clusters also use policies. If you do not apply a policy to jobs, the scheduled workloads that often cost the most stay unrestricted.
- Using a fixed tag value in a shared policy. If several teams use the same policy, all their usage lands under one tag. Use one policy per team, or an
allowlistof tag values.
Creating a policy with the API
You can also manage policies as code, which lets you review changes. This Python example uses the Databricks SDK:
import json
from databricks.sdk import WorkspaceClient
w = WorkspaceClient()
definition = {
"autotermination_minutes": {"type": "range", "maxValue": 60, "defaultValue": 30},
"autoscale.max_workers": {"type": "range", "maxValue": 8, "defaultValue": 4},
"dbus_per_hour": {"type": "range", "maxValue": 20},
"custom_tags.team": {"type": "fixed", "value": "data-eng"},
}
policy = w.cluster_policies.create(
name="data-eng-cost-limits",
definition=json.dumps(definition),
)
print(policy.policy_id)Exercise
A team's policy fixes custom_tags.team and caps autoscale.max_workers at 8, yet a user still launched a 20-worker cluster without the tag. Name two likely causes.
Show answer
The user probably has the unrestricted cluster creation entitlement, so they can create compute without choosing a policy. Alternatively, the cluster is an older one created before the policy was changed, so it is only marked non-compliant and was not updated.
Exercise
Why is dbus_per_hour useful in addition to autoscale.max_workers?
Show answer
It limits the DBU rate of the whole cluster. A worker cap alone does not stop a user from picking a large, expensive node type, but the DBU rate limit does.