Sub topic 13
Enforcing Limits with Cluster Policies
This chapter assumes you know how to create a Databricks cluster from the UI or the Clusters API. It also assumes you have read the earlier chapters on autoscaling bounds and auto-termination. Here you will see how an administrator turns those settings into rules that users cannot override by accident.
Why policies exist
Every setting from the earlier chapters is a choice made by whoever creates the cluster. A user can set max_workers to 200, turn off auto-termination, or pick a GPU node type for a job that needs none of it. Each choice is legal, and each can produce a large bill.
A cluster policy is a set of rules attached to cluster creation. It controls which settings a user can change, which values are allowed, and which values are fixed. Users who have permission to use a policy see only the options the policy permits. Administrators define policies and decide who may use them.
Policies handle three jobs:
- Cost control: cap worker counts, require auto-termination, and restrict expensive node types.
- Consistency: pin the runtime version, tags, and Spark configuration so clusters look alike.
- Simplicity: hide settings that most users should not touch, so the creation form is shorter.
How a policy definition works
A policy definition is a JSON document. Each key is the path of a cluster attribute. Each value is a rule that says how that attribute may be set. Attribute paths use dots for nested fields, so autoscale.max_workers refers to the max_workers field inside the autoscale object.
The example below caps the autoscaling maximum and limits the idle timeout.
{
"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 set more than 8 maximum workers. The form starts at 4, so a user who changes nothing gets 4. The idle timeout must fall between 10 and 120 minutes, and it starts at 30.
Rule types
| Rule type | What it does | Typical use |
|---|---|---|
| `fixed` | Sets one value that the user cannot change. Add `"hidden": true` to remove it from the form. | Runtime version, tags, a required Spark setting |
| `range` | Allows a number between `minValue` and `maxValue`. | Worker counts, idle timeout |
| `allowlist` | Allows only values from a list, and the user picks one. | Approved node types |
| `blocklist` | Allows any value except those listed. | Banning specific node types |
| `regex` | Requires a string to match a pattern. | Naming conventions, tag values |
| `unlimited` | Imposes no limit, and can mark an attribute as required or supply a default. | Leaving a setting open but giving it a starting value |
The defaultValue field sets what the form shows first. It is a convenience and not a restriction. The limit comes from minValue, maxValue, or the list.
A more complete policy
Real policies combine several rules. This one suits a team that runs ETL on AWS and wants to apply the cost practices from earlier chapters.
{
"spark_version": {
"type": "fixed",
"value": "15.4.x-scala2.12"
},
"node_type_id": {
"type": "allowlist",
"values": ["i3.xlarge", "i3.2xlarge", "m5d.2xlarge"],
"defaultValue": "i3.xlarge"
},
"autoscale.min_workers": {
"type": "range",
"minValue": 1,
"maxValue": 4,
"defaultValue": 2
},
"autoscale.max_workers": {
"type": "range",
"minValue": 2,
"maxValue": 8,
"defaultValue": 8
},
"autotermination_minutes": {
"type": "range",
"minValue": 10,
"maxValue": 60,
"defaultValue": 30
},
"aws_attributes.availability": {
"type": "fixed",
"value": "SPOT_WITH_FALLBACK"
},
"aws_attributes.first_on_demand": {
"type": "fixed",
"value": 1
},
"custom_tags.team": {
"type": "fixed",
"value": "data-engineering"
}
}Each rule maps to an earlier chapter:
- The fixed runtime version avoids surprise upgrades.
- The node type allowlist keeps users on instance families you have already sized.
- The worker ranges bound the autoscaling window.
- The idle timeout range makes sure no cluster runs forever.
- The spot settings keep the driver on demand and put workers on spot capacity with fallback.
- The
teamtag lets you attribute cost to a team in billing reports.
A user who creates a cluster under this policy can still choose the node type, the worker range, and the idle timeout, but only inside the limits.
Policies and the Clusters API
When a user creates a cluster through the API, the request can include a policy_id. The platform checks the request against the policy. If a value violates a rule, the request fails. If the user omits a fixed attribute, the policy fills it in. This behavior is useful for automation, because a job definition that references a policy inherits the guardrails without repeating them.
The following request creates a cluster under a policy. Replace the policy ID with your own.
{
"cluster_name": "etl-nightly",
"policy_id": "ABC123DEF456",
"apply_policy_default_values": true,
"autoscale": {
"min_workers": 2,
"max_workers": 6
}
}Setting apply_policy_default_values to true tells the platform to use the policy's defaults for any attribute you leave out. Without it, an omitted attribute that has no fixed value may be missing from the request and cause a validation error.
Common errors
- Setting only a default, expecting a limit. A
defaultValuedoes not restrict anything. If you want a cap, addmaxValue. - Forgetting that
max_workersandmin_workersare separate attributes. Cappingautoscale.max_workersalone lets a user set a high minimum, and the cluster then holds many nodes at all times. Constrain both. - Fixing a value that existing clusters already break. Editing a policy does not instantly rewrite running clusters. A cluster that no longer matches the policy is flagged as out of compliance and may need an edit before it can restart.
- Hiding attributes that users must set. If you hide an attribute and give it no fixed value, users cannot supply it, and creation can fail.
- Allowing the policy to be the only control. Policies bound what users can do, but they do not tell you whether the chosen size is right. You still need the monitoring and tuning loop covered in later chapters.
Who gets which policy
Create several policies instead of one. A common set looks like this:
- A small policy for interactive development, with a low worker cap and a short idle timeout.
- A job policy for scheduled ETL, with a larger cap and spot workers.
- A policy for machine learning that allows GPU node types and nothing else does.
Administrators grant the CAN_USE permission on each policy to specific users or groups. A user who holds only that permission and has no unrestricted cluster creation right can create clusters only through the policies they have been given. This is what makes the guardrails binding. If users also hold unrestricted creation rights, they can bypass every policy.
Summary
Cluster policies turn sizing decisions into enforced rules. Use range for numbers, allowlist for node types, and fixed for values nobody should change. Constrain both ends of the autoscaling window, require auto-termination, and grant users access to policies instead of unrestricted creation. Then keep measuring, since a policy sets the boundaries and the tuning loop finds the right size inside them.
Exercise
A policy contains only this rule: "autoscale.maxworkers": {"type": "range", "maxValue": 8, "defaultValue": 4}. A user sets minworkers to 8 and max_workers to 8, then leaves the cluster running. What does the policy fail to prevent, and which rule would you add?
Show answer
The policy caps only the maximum, so a user can set a high minimum, and the cluster then holds 8 workers at all times and never scales down. Add a rule such as "autoscale.minworkers": {"type": "range", "minValue": 1, "maxValue": 4, "defaultValue": 2}. Also add a range rule on autoterminationminutes so idle clusters stop.
Exercise
You want every cluster to run runtime 15.4.x-scala2.12, keep the driver on demand, run workers on spot with fallback, and carry the tag team=analytics. Which rule type do you use for each, and why?
Show answer
Use "fixed" for all of them: sparkversion with the runtime string, awsattributes.firstondemand with 1, awsattributes.availability with SPOTWITHFALLBACK, and customtags.team with "analytics". Fixed rules set one value that users cannot change, which is what you need when the value must be the same on every cluster.