Sub topic 6
Job Clusters for Scheduled and Automated Work
This chapter assumes you know what a Databricks workspace and notebook are, that a Spark cluster has a driver and workers, and that you can read JSON. It builds on the earlier chapters on cluster anatomy, access modes, and all-purpose clusters.
What a job cluster is
A job cluster is a classic (non-serverless) cluster that Databricks creates when a job run starts and terminates when the run finishes. You never start it by hand, and nobody can attach a notebook to it interactively. It exists for one purpose: running the tasks of a job.
The lifecycle is short and predictable:
- A trigger fires, such as a schedule, an API call, or a file arrival.
- Databricks requests virtual machines from the cloud provider and starts the cluster.
- The tasks run.
- The cluster terminates as soon as the run ends, whether it succeeded or failed.
Why use job clusters instead of an all-purpose cluster
Running scheduled work on an all-purpose cluster works, but it has costs. The differences matter in production.
| Concern | All-purpose cluster | Job cluster |
|---|---|---|
| Lifecycle | Started by a person or a schedule, stays up until auto termination | Created per run, ends with the run |
| Sharing | Many users and notebooks at once | One job run, isolated from other runs |
| Idle cost | Billed while idle until auto termination fires | No idle time after the run ends |
| Price per DBU | Higher all-purpose rate | Lower jobs compute rate |
| Interference | Another user's heavy query can slow your job | No neighbors on the cluster |
| Interactive use | Yes | No |
Two of these rows drive most decisions. First, Databricks bills job compute at a lower DBU rate than all-purpose compute for the same node type. Second, because each run gets its own cluster, a runaway notebook in someone's interactive session cannot starve your nightly pipeline. A DBU (Databricks Unit) is the unit of processing capability that Databricks bills per hour, and a later chapter covers how DBUs combine with cloud infrastructure charges.
The trade-off is startup time. Provisioning virtual machines and installing the runtime typically takes several minutes. For a nightly job this is irrelevant. For a job that runs every two minutes, the startup would dominate the run, and you would look at instance pools or serverless instead.
Defining a job cluster in the Jobs API
You declare a job cluster inside the job definition under job_clusters. Each entry has a job_cluster_key, which is a name that tasks use to refer to it, and a new_cluster object holding the cluster settings. A task then points at the cluster with the same key.
{
"name": "nightly-etl",
"job_clusters": [
{
"job_cluster_key": "etl_cluster",
"new_cluster": {
"spark_version": "15.4.x-scala2.12",
"node_type_id": "i3.xlarge",
"num_workers": 2,
"data_security_mode": "USER_ISOLATION"
}
}
],
"tasks": [
{
"task_key": "ingest",
"job_cluster_key": "etl_cluster",
"notebook_task": {
"notebook_path": "/Workspace/Users/[email protected]/ingest"
}
}
]
}Read the example in three parts:
job_clustersdeclares a reusable cluster definition. Nothing is created yet. It is a template for the run.new_clustercarries the same fields you would give the Clusters API: runtime version, node type, worker count, and access mode. Heredata_security_modeisUSER_ISOLATION, which is standard access mode and works with Unity Catalog.- The task's
job_cluster_keylinks the task to the template. The value must match a key injob_clusters, or the job definition is rejected.
The node_type_id shown is an AWS instance type. On Azure and GCP you use that cloud's node type names.
Differences from an all-purpose cluster definition
You will notice what is missing. The example sets no autotermination_minutes, because a job cluster terminates when the run ends. It also has a fixed num_workers instead of an autoscale block. You can use autoscale on a job cluster, but a fixed size is common for jobs because the workload is known and repeatable.
There is also no cluster_name. Databricks names job clusters itself, and you identify them through the job and run.
Sharing one job cluster across tasks
A job can have many tasks, and several can use the same job_cluster_key. They then share one cluster for the duration of the run. This saves the startup cost of creating a cluster per task.
{
"name": "nightly-pipeline",
"job_clusters": [
{
"job_cluster_key": "shared",
"new_cluster": {
"spark_version": "15.4.x-scala2.12",
"node_type_id": "i3.xlarge",
"autoscale": {
"min_workers": 1,
"max_workers": 4
},
"data_security_mode": "USER_ISOLATION"
}
}
],
"tasks": [
{
"task_key": "ingest",
"job_cluster_key": "shared",
"notebook_task": {
"notebook_path": "/Workspace/Users/[email protected]/ingest"
}
},
{
"task_key": "transform",
"depends_on": [{ "task_key": "ingest" }],
"job_cluster_key": "shared",
"notebook_task": {
"notebook_path": "/Workspace/Users/[email protected]/transform"
}
}
]
}The transform task waits for ingest through depends_on, then runs on the cluster ingest already started. The cluster terminates after the last task finishes.
Sharing has limits to keep in mind:
- Tasks that run in parallel on the same cluster compete for the same cores and memory. If two heavy tasks overlap, each slows down.
- Tasks that need different runtimes, libraries, or node types should use different job clusters. For example, a task that needs GPU nodes should not share a CPU cluster definition.
- Cluster-scoped libraries installed for one task are visible to the others on the same cluster.
Existing clusters versus new clusters
A task can also point at an existing all-purpose cluster with existing_cluster_id. This avoids startup time, because the cluster is already running, but it brings back the all-purpose billing rate and the interference problem. It is acceptable during development and testing of a job. For production, use new_cluster through a job_clusters entry.
Access mode and Unity Catalog on job clusters
Access mode controls which governance features work. For a job that reads and writes Unity Catalog tables, set data_security_mode explicitly. USER_ISOLATION is standard access mode. SINGLE_USER is dedicated access mode, and it needs an identity to run as. The job's run-as identity, either a user or a service principal, is used for permission checks.
In production, run jobs as a service principal rather than a person. If a job runs as an individual and that person leaves the organization, the job can fail because the identity loses access.
If you omit data_security_mode, behavior depends on workspace defaults and API version. Set it explicitly so the job behaves the same in every workspace.
Retries, timeouts, and failure behavior
Because the cluster exists only for the run, failure settings interact with startup cost.
- A task retry (
max_retries) reruns the task. If the cluster is still up, the retry reuses it. If the whole cluster failed, a new one is created. - A timeout (
timeout_seconds) stops a task or job that runs too long. This protects you from a stuck job billing for hours on a cluster that nobody is watching. - A failed run still terminates its cluster, so failed jobs do not leave idle machines behind.
Setting a timeout is the single most useful cost control for a job cluster, because the cluster has no idle-based auto termination to rescue you from a hung task.
Controlling job cluster configuration
Job clusters are created by definitions that anyone with permission can write, so they are a common source of oversized or inconsistent clusters. A cluster policy can restrict node types, cap the number of workers, and fix the access mode. You reference a policy in new_cluster with policy_id. Policies are covered in a later chapter.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Job creation fails with an unknown cluster key | A task's `job_cluster_key` does not match any entry in `job_clusters` | Make the key in the task identical to the one declared |
| Run fails before any task starts | Cloud quota exhausted, or the node type is unavailable in the zone | Check quotas, choose another node type, or retry |
| Unity Catalog table access denied | Access mode does not support Unity Catalog, or the run-as identity lacks grants | Set `data_security_mode` explicitly and grant privileges to the run-as identity |
| Job is slower than in development | Parallel tasks share one small cluster, or the cluster is smaller than the interactive one | Add workers, enable autoscaling, or split tasks onto separate clusters |
| Unexpectedly high bill | No timeout and a hung task, or all-purpose cluster reused by the job | Add `timeout_seconds` and use `new_cluster` |
| Libraries missing at run time | Libraries were installed on an all-purpose cluster by hand | Declare libraries on the task so they install on the job cluster |
When a job cluster is the wrong choice
A job cluster is not the best fit in these cases:
- Very frequent runs, where several minutes of startup per run is too much overhead.
- Short jobs where startup takes longer than the work itself.
- Workloads where you do not want to manage node types and runtime versions at all. Serverless compute for jobs removes those choices.
If your workspace has serverless compute for jobs enabled, a task with no cluster settings runs on serverless compute. That case is covered in the next chapters.
Exercise
A nightly job has two tasks, extract and load, in that order. Both use the same runtime and node type. You define a separate new_cluster for each task. What cost does this add, and how do you remove it?
Show answer
Each task pays its own cluster startup time, and the billing clock runs during both startups. Declare one entry under job_clusters, give it a job_cluster_key, and set that key on both tasks. The load task then runs on the cluster extract already started, and the cluster terminates after load finishes.
Exercise
A job runs on a job cluster with no timeout_seconds. One night a task hangs. Why does the cluster not shut itself down, and what setting prevents a long bill?
Show answer
A job cluster terminates when the run ends, not after an idle period, so a hung task keeps the run, and the cluster, alive. Setting timeout_seconds on the task or job cancels the run after the limit, which terminates the cluster.
Exercise
Why should a production job that reads Unity Catalog tables set data_security_mode explicitly and run as a service principal?
Show answer
An explicit data_security_mode such as USER_ISOLATION makes the access mode, and so Unity Catalog support, identical across workspaces instead of depending on defaults. A service principal keeps the job working when an individual employee leaves, because permissions are tied to the principal and not to a person.