Sub topic 6
Job clusters for scheduled and automated work
This chapter assumes you know the driver and worker layout of a cluster, the difference between all-purpose clusters and other compute, and how to read JSON. It covers what a job cluster is, how to define one in the Jobs API, and the errors people hit most often.
What a job cluster is
A job cluster is compute that a job run creates when it starts and terminates when the run finishes. You do not start it, attach to it, or stop it. Nobody can attach a notebook to it for interactive work.
This lifecycle is the main reason to use one for scheduled work. Cost stops the moment the run ends, and each run starts from a clean cluster, so one run cannot leave behind state that affects the next. Job compute is also billed at a lower DBU rate than all-purpose compute, so the same work usually costs less.
Defining a job cluster
You declare a job cluster in the job_clusters list of a job and give it a job_cluster_key. Each task then names that key, as in this Jobs API 2.1 definition.
{
"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"
}
}
]
}The new_cluster block uses the same fields as a cluster definition elsewhere: runtime version, node type, worker count, and access mode. The node type shown is an AWS example and differs on Azure and GCP.
Sharing one cluster across tasks
If several tasks name the same job_cluster_key, they share one cluster within a run. This avoids paying for a start-up per task. The tasks still run in the order set by their dependencies, and a task that depends on another waits for it.
If you instead put a new_cluster block inside a task, that task gets its own cluster. This isolates the task but adds a start-up and a separate bill for each one.
Fixed size or autoscaling
The example uses num_workers: 2, a fixed size. A job cluster can also use autoscale with minimum and maximum workers. For predictable batch work, a fixed size is often easier to reason about for both runtime and cost. Autoscaling suits runs whose load varies a lot between stages.
Access mode
The example sets data_security_mode to USER_ISOLATION, which is standard access mode. Choose the mode that matches your Unity Catalog needs, because it decides which languages and data access features work on the cluster. A job normally runs as a single identity, so the run-as user or service principal must have permission on the data it reads.
Common errors
- Start-up time on every run. A new cluster takes minutes to provision. For frequent, short jobs this overhead can exceed the work itself. Instance pools reduce it, and so does serverless compute for jobs.
- Cluster definition drift. Editing a cluster in the UI does not change a job cluster. Change the job definition, then redeploy it.
- Missing permissions. The run-as identity needs rights on the notebook, the data, and the cluster policy, if one applies. A run that works under your account can fail under a service principal.
- Capacity errors. If the cloud cannot supply the requested instances, the run fails during cluster creation. Retries on the task help with transient failures.
- Using an all-purpose cluster by habit. Pointing a scheduled job at an existing all-purpose cluster works, but you pay the higher rate and share resources with interactive users.
Exercise
Two tasks in one job, "ingest" and "transform", both set job_cluster_key to "etlcluster". How many clusters does one run create, and what changes if "transform" instead contains its own `newcluster` block?
Show answer
One run creates one cluster, shared by both tasks, so you pay for one start-up. If "transform" has its own new_cluster block, the run creates two clusters, one per task. That isolates the transform task but adds a second start-up and a separate cluster cost.