Goal
A multi-task job with ten tasks and ten new_cluster blocks pays for ten VM start-ups (several minutes each) and ten idle tails. Defining one shared job cluster and pointing tasks at it with job_cluster_key removes most of that overhead. Sharing has side effects, though, and a lot of "flaky task" tickets trace back to them. This post shows how to set up sharing deliberately.
How shared job clusters behave
From the Databricks Jobs documentation, and worth knowing before you design:
- A shared job cluster is scoped to one job run. Other jobs can't use it, and neither can other runs of the same job.
- It starts when the first task that uses it starts, and it terminates after the last task that uses it finishes.
- While tasks that don't use it are running, for example a task on serverless or a SQL task between two cluster tasks, it stays up and idle. You pay for that time.
- Libraries can't be declared on the shared cluster definition. Declare them per task (dependent libraries) or in a job environment.
- Tasks on the same shared cluster run on the same driver JVM. Scala singletons, companion-object state and libraries loaded by an earlier task are still there for later tasks in the run.
Configure it in a bundle
resources:
jobs:
nightly_sales:
name: nightly_sales
job_clusters:
- job_cluster_key: etl_shared
new_cluster:
spark_version: 15.4.x-scala2.12
node_type_id: Standard_E8ds_v5
autoscale: { min_workers: 2, max_workers: 8 }
data_security_mode: SINGLE_USER
custom_tags: { cost_center: sales-data }
- job_cluster_key: heavy_merge
new_cluster:
spark_version: 15.4.x-scala2.12
node_type_id: Standard_E16ds_v5
num_workers: 12
data_security_mode: SINGLE_USER
tasks:
- task_key: ingest_orders
job_cluster_key: etl_shared
notebook_task: { notebook_path: ../src/ingest_orders.py }
libraries:
- whl: ../dist/sales_lib-*.whl
- task_key: ingest_customers
job_cluster_key: etl_shared
notebook_task: { notebook_path: ../src/ingest_customers.py }
libraries:
- whl: ../dist/sales_lib-*.whl
- task_key: merge_fact
depends_on: [{ task_key: ingest_orders }, { task_key: ingest_customers }]
job_cluster_key: heavy_merge
notebook_task: { notebook_path: ../src/merge_fact.py }
Here two light ingest tasks share a small cluster. The heavy MERGE gets its own cluster with a different shape. Runtime versions and VM names are examples, so use what your workspace offers.
databricks bundle validate -t prod
databricks bundle deploy -t prod
Find tasks that don't share today
For jobs created by hand, list the tasks that carry their own new_cluster:
databricks jobs get 123456789 -o json |
jq -r '.settings.tasks[] |
[.task_key, (if .new_cluster then "own new_cluster"
elif .job_cluster_key then "shared:" + .job_cluster_key
elif .existing_cluster_id then "ALL-PURPOSE " + .existing_cluster_id
else "serverless/other" end)] | @tsv'
Tasks that point at existing_cluster_id run on all-purpose compute. Databricks doesn't recommend that for production jobs, so move them first.
The trade-offs
| Aspect | One shared cluster | Cluster per task |
|---|---|---|
| Start-up overhead | Paid once per run | Paid per task, often several minutes each |
| Idle cost | Idle while unrelated tasks run | Idle only during each task's own tail |
| Isolation | Shared driver memory, JVM state, Spark config, temp views in the same session scope | Full isolation |
| Failure blast radius | A driver OOM in one task can kill parallel tasks | Only the failing task |
| Sizing | Must fit the biggest concurrent combination | Sized per workload |
| Spark config | One set for all tasks (spark.sql.shuffle.partitions and so on) | Tuned per task |
Parallel tasks compete for one driver
When ingest_orders and ingest_customers run at the same time on etl_shared, they submit Spark jobs to the same driver and share executor slots. Two failure modes are common:
- Driver memory pressure. One task calls
collect()ortoPandas()and the driver runs out of memory or stalls in GC. Every task on the cluster fails or hangs. The errors look unrelated ("driver is up but is not responsive"). - Slot starvation. One task's big shuffle takes every core, and the small task's SLA slips. Autoscaling helps but reacts slowly.
Warning
Don't share a cluster between a task that collects large results to the driver and anything time-critical. Give the driver-heavy task its own cluster, or rewrite it so it doesn't collect.
Hidden state between tasks
A Python task that sets spark.conf.set("spark.sql.shuffle.partitions", "2000") changes only its own Spark session. Scala object state, JVM system properties and installed libraries persist across tasks on the same driver, though. Two tasks that need different versions of the same library can't safely share a cluster. The first task to load a library wins for the rest of the run.
Measure whether sharing pays off
Compare DBUs per run before and after the change:
SELECT usage_metadata.job_id,
usage_metadata.job_run_id,
SUM(usage_quantity) AS dbus,
MIN(usage_start_time) AS run_start
FROM system.billing.usage
WHERE usage_metadata.job_id = '123456789'
AND usage_date >= current_date() - 30
GROUP BY ALL
ORDER BY run_start;
Compare wall-clock duration from the task timeline:
SELECT run_id, task_key,
MIN(period_start_time) AS started,
MAX(period_end_time) AS ended,
timestampdiff(SECOND, MIN(period_start_time), MAX(period_end_time)) AS secs,
MAX(result_state) AS result
FROM system.lakeflow.job_task_run_timeline
WHERE job_id = '123456789' AND period_start_time >= current_date() - 30
GROUP BY run_id, task_key
ORDER BY started;
Also check system.compute.node_timeline for the shared cluster. Long stretches of low CPU while a non-cluster task runs show the idle cost you are paying.
Repairs and retries
- Repair run. The original run's shared cluster has already terminated, so the repaired tasks start new compute. A repair that reruns three tasks on the same
job_cluster_keyshares one new cluster. - Task retries. These happen inside the run and reuse the shared cluster if it is still up. A retry after a driver crash starts on a cluster that just failed. If that keeps happening, move the task to its own cluster rather than adding retries.
Decision rules
- Group tasks that run one after another and use similar resources onto one shared cluster. That is the clearest win.
- Give heavy, differently shaped tasks their own
job_cluster_key, such as a large MERGE, an ML training step or a GPU task. - Keep driver-heavy or library-conflicting tasks isolated.
- If a long non-Spark step (SQL task, external API wait) sits between cluster tasks, consider moving the later tasks to serverless jobs compute. That stops the shared cluster from sitting idle.
- Re-measure DBUs and duration after every regrouping. Don't assume sharing always saves money.
Get the weekly HubDatAI email
One email a week with everything new: posts, learning sessions and videos. Free, no spam, unsubscribe in one click. What you get