Sub topic 3
Compute for job tasks
This chapter assumes you can run a notebook on a cluster and have read the earlier chapters on jobs, tasks, and runs. Every task in a job needs somewhere to run. This chapter covers the compute options, how a shared job cluster behaves, and the settings that affect cost and reliability.
The compute options
A task can run on one of these:
| Compute type | How you reference it | Typical use |
|---|---|---|
| Job cluster | `new_cluster` on a task, or `job_cluster_key` pointing to an entry in `job_clusters` | Production pipelines |
| All-purpose cluster | `existing_cluster_id` | Development and debugging |
| Serverless compute | Omit the cluster settings on the task | Notebook and Python tasks where you want no cluster management |
| SQL warehouse | `warehouse_id` on a SQL task | SQL queries, dashboards, alerts |
Pipeline tasks use the compute configured on the pipeline they trigger, not compute defined in the job.
Job clusters
A job cluster is created when a run starts and terminated when the last task that uses it finishes. No one can attach to it from outside the run, and a later run never reuses it. Databricks bills job compute at a lower rate than all-purpose compute, so job clusters are the usual choice for scheduled work.
You can define a cluster in two ways:
- Per task. Put a
new_clusterblock inside the task. Each such task gets its own cluster. - Shared. Define the cluster once under
job_clusterswith ajob_cluster_key, then reference that key from several tasks.
The job from the first chapter used the shared form. All three tasks point at etl_cluster:
{
"job_clusters": [
{
"job_cluster_key": "etl_cluster",
"new_cluster": {
"spark_version": "15.4.x-scala2.12",
"node_type_id": "i3.xlarge",
"num_workers": 2
}
}
]
}The node_type_id value is specific to AWS. Azure and Google Cloud use different instance names, so check which types your workspace offers.
Lifecycle of a shared cluster
Sharing a cluster has these effects:
- You pay for one cluster startup (often several minutes) instead of one per task.
- Tasks that can run in parallel compete for the same executors. If two parallel branches both need heavy compute, size the cluster for both or give them separate clusters.
- A cluster-level problem, such as an out-of-memory crash on the driver, can fail every task running on it at that moment.
- The cluster starts when the first task that needs it is ready, not at the start of the run. It stays up while tasks run, including during the gaps between dependent tasks.
Sizing and cost settings
You can set a fixed num_workers or let the cluster scale with autoscale. The example below adds autoscaling, Unity Catalog-compatible access mode, and spot instances with a fallback to on-demand. The aws_attributes block is AWS-specific.
{
"job_cluster_key": "etl_cluster",
"new_cluster": {
"spark_version": "15.4.x-scala2.12",
"node_type_id": "i3.xlarge",
"autoscale": {"min_workers": 1, "max_workers": 4},
"data_security_mode": "SINGLE_USER",
"aws_attributes": {
"availability": "SPOT_WITH_FALLBACK",
"first_on_demand": 1
}
}
}Points to weigh:
- Autoscaling helps when the data volume varies from run to run. It adds little for short jobs, because scaling up takes time.
- Spot instances lower cost.
SPOT_WITH_FALLBACKreplaces reclaimed spot nodes with on-demand nodes.first_on_demand: 1keeps the driver on an on-demand instance, so reclaiming a spot node cannot kill the driver. data_security_modecontrols isolation and Unity Catalog access. Pick the mode your data governance setup requires. A mode that does not support your workload fails at run time, not when you save the job.- Cluster policies let an administrator restrict or fix settings such as node types and maximum workers. If a policy applies, your cluster definition must satisfy it or the job cannot be saved.
Libraries
Libraries are attached per task. When tasks share a cluster, each task installs its own libraries on that cluster.
{
"task_key": "ingest",
"job_cluster_key": "etl_cluster",
"notebook_task": {"notebook_path": "/Workspace/Shared/ingest"},
"libraries": [
{"pypi": {"package": "requests==2.32.3"}}
]
}Pin versions. An unpinned package can change between runs and break a pipeline that did not change.
All-purpose clusters
You can point a task at an existing all-purpose cluster with existing_cluster_id. This is convenient when you iterate on a notebook, because there is no startup wait. It is a poor fit for production:
- It costs more per unit of compute.
- Other users share it, so their work can slow or interfere with your task.
- If the cluster is terminated, the run has to start it, and the task's behavior depends on the cluster's auto-termination settings.
Switch to a job cluster or serverless compute before you schedule anything.
Serverless compute
With serverless compute, you omit the cluster definition. Databricks provides and manages the compute, and startup is much faster than for a classic cluster. Trade-offs:
- You cannot choose node types, Spark configuration, or init scripts the way you can on a classic cluster.
- Some task types and library setups need an environment definition instead of per-task libraries.
- Not every workspace or region has it enabled.
Check availability and supported task types in your workspace before you plan around it.
SQL tasks use warehouses
A SQL task does not run on a cluster. It runs on a SQL warehouse, which you select by ID. Several tasks in different jobs can share one warehouse. A serverless or pro warehouse starts faster than a classic one, which matters when a job runs short queries on a schedule.
Common mistakes
- Using an all-purpose cluster for scheduled jobs. It raises cost and makes runs depend on other users' activity.
- Defining the same cluster per task. Each task then pays for its own startup. Use
job_clustersandjob_cluster_key. - Undersizing a shared cluster for parallel branches. Tasks slow down or fail with memory errors only when the branches overlap.
- Hard-coding a cloud-specific
node_type_idin a template for several workspaces. The job fails to create the cluster on a different cloud. - Forgetting that every run creates new compute. A job that runs every five minutes with a four-minute startup spends most of its time starting clusters. Consider serverless compute or a larger interval.
Exercise
A job has three tasks: two independent tasks load_a and load_b, and a merge task that depends on both. All three use the same job_cluster_key. Runs sometimes fail with out-of-memory errors in load_a or load_b, but never when you run one of them alone. What is the likely cause, and what are two ways to fix it?
Show answer
load_a and load_b have no dependency on each other, so they run in parallel on the same cluster and compete for memory. Fix it by either giving the cluster more or larger workers (or a higher autoscale maximum), or by defining a second job cluster so each load task gets its own, or by adding a dependency so the two tasks run one after the other.