Sub topic 4
Building a multi-task workflow with dependencies
This chapter assumes you have created a Databricks job with one notebook task, know what a cluster is, and can read JSON. You will turn a single-task job into a multi-task workflow, and you will learn how Databricks decides what runs and when.
Tasks form a directed acyclic graph
A job with several tasks is a directed acyclic graph (DAG). Each task is a node. Each entry in a task's depends_on list is an edge that points from an upstream task to it. "Acyclic" means no task can depend on itself, directly or through other tasks. The Jobs API rejects a definition that contains a cycle.
Two rules explain most of the behavior you will see:
- A task starts only after every task it depends on has finished. By default, "finished" means "succeeded".
- Tasks with no dependency between them can run at the same time.
A task with no depends_on is a root task. A job can have several root tasks, and all of them start when the run starts.
Declaring dependencies
You declare a dependency by adding depends_on to a task. Each item names the upstream task by its task_key. The task_key must be unique within the job. It is the identifier you use in dependencies, in task values and in repair requests, so choose a short, stable name.
The following job definition chains three notebook tasks. All three share one job cluster, run daily at 06:00 UTC, and allow only one concurrent run. The ingest task retries twice. The node type shown is AWS-specific and varies by cloud.
{
"name": "daily-sales-pipeline",
"max_concurrent_runs": 1,
"schedule": {
"quartz_cron_expression": "0 0 6 * * ?",
"timezone_id": "UTC",
"pause_status": "UNPAUSED"
},
"job_clusters": [
{
"job_cluster_key": "etl_cluster",
"new_cluster": {
"spark_version": "15.4.x-scala2.12",
"node_type_id": "i3.xlarge",
"num_workers": 2
}
}
],
"tasks": [
{
"task_key": "ingest",
"job_cluster_key": "etl_cluster",
"notebook_task": {"notebook_path": "/Workspace/Shared/ingest"},
"max_retries": 2,
"min_retry_interval_millis": 60000
},
{
"task_key": "transform",
"depends_on": [{"task_key": "ingest"}],
"job_cluster_key": "etl_cluster",
"notebook_task": {"notebook_path": "/Workspace/Shared/transform"}
},
{
"task_key": "publish",
"depends_on": [{"task_key": "transform"}],
"job_cluster_key": "etl_cluster",
"notebook_task": {"notebook_path": "/Workspace/Shared/publish"}
}
]
}Read the tasks array from the top. ingest is the only root task. transform lists ingest in depends_on, and publish lists transform. The order of the array does not matter to the scheduler. Only the depends_on edges do.
The same graph is available in the workspace UI. In the job editor, you select a task and use the Depends on field. The DAG view draws the edges for you.
Fan-out and fan-in
To run tasks in parallel, give them the same upstream task, or give them none. To wait for several tasks, list all of them in depends_on. In the diagram above, transform would be written like this:
{
"task_key": "transform",
"depends_on": [
{"task_key": "ingest_orders"},
{"task_key": "ingest_customers"}
],
"job_cluster_key": "etl_cluster",
"notebook_task": {"notebook_path": "/Workspace/Shared/transform"}
}By default, transform starts only when both ingest tasks have succeeded. If either one fails, transform does not run.
How a run progresses
Each task in a run has its own lifecycle state and its own result. A task that has not yet been released by its dependencies waits. A task whose dependencies cannot be met is skipped, or reported as "upstream failed", depending on the dependency rule in force.
The state names above are a teaching model. The run page in the UI and the state fields in the API use their own labels, such as PENDING, RUNNING, TERMINATED and SKIPPED, together with a result state such as SUCCESS or FAILED. Check the API reference for the exact values in your workspace.
The key consequence is this. When ingest fails after its retries, transform and publish do not run. The run as a whole is marked as failed, and the downstream tasks appear as upstream-failed or excluded in the run page.
Sharing compute across tasks
The example uses job_cluster_key on every task. A job cluster is defined once in job_clusters and then shared by the tasks that reference it. Databricks starts it when the first task that needs it is ready, and it terminates after the last task that uses it finishes.
Sharing has two effects:
- You pay the startup time once per run, not once per task.
- Parallel tasks on the same cluster compete for the same workers. Two heavy parallel tasks on a two-worker cluster can each run slower than they would alone.
If parallel branches need different sizes, define more than one job cluster and point each branch at the one that suits it. The next chapter on compute covers the options in detail.
Passing information down the graph
Dependencies control order. They do not move data. Tasks pass data through tables and files, or through small values. A task can publish a value, and a downstream task can read it by the upstream task's key.
# In the notebook for task "ingest"
row_count = spark.table("sales.raw_orders").count()
dbutils.jobs.taskValues.set(key="row_count", value=row_count)
# In a notebook for a downstream task
row_count = dbutils.jobs.taskValues.get(
taskKey="ingest", key="row_count", default=0, debugValue=0
)
print(f"Ingest produced {row_count} rows")The taskKey argument refers to the task_key in the job definition. The debugValue argument is returned when the notebook runs interactively outside a job. Without it, the get call fails in an interactive session because there is no job run to read from. A later chapter covers task values fully.
Declaring the graph as code
The same dependency structure appears in a Databricks Asset Bundle. This resource declares a scheduled two-task job with a failure email. Compute settings are omitted here and depend on the workspace.
resources:
jobs:
daily_sales:
name: daily-sales-pipeline
schedule:
quartz_cron_expression: "0 0 6 * * ?"
timezone_id: UTC
email_notifications:
on_failure:
- [email protected]
tasks:
- task_key: ingest
notebook_task:
notebook_path: ../src/ingest.ipynb
- task_key: transform
depends_on:
- task_key: ingest
notebook_task:
notebook_path: ../src/transform.ipynbThe depends_on syntax is identical to the JSON form. Only the notation changes.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| The job fails to save with a cycle or dependency error | A task depends on itself or on a descendant | Redraw the graph and remove the back edge |
| The API rejects a dependency | `depends_on` names a `task_key` that does not exist, often through a typo or a rename | Match the key exactly, including case |
| A task never starts | An upstream task failed, or the dependency rule requires an outcome that did not happen | Check the upstream task's result and the rule on the edge |
| Parallel tasks run slowly | They share a small job cluster | Add workers or use separate job clusters |
| `taskValues.get` fails in a notebook you test by hand | No `debugValue` is set | Pass `debugValue` so the notebook also runs interactively |
Design guidelines
- Split work where a failure should stop the flow. If a bad ingest must block publishing, make them separate tasks with a dependency between them.
- Run independent work in parallel, but size the cluster for the busiest stage.
- Name tasks after what they do, such as
ingest_orders, so the DAG reads like a description of the pipeline. - Pass large data through tables or files, and pass only small values, such as counts or paths, as task values.
- Define the graph in code, so changes to dependencies go through review.
Exercise
A job has tasks a, b, c and d. Task b depends on a. Task c depends on a. Task d depends on both b and c. Which tasks can run in parallel, and what happens to d if c fails with the default dependency behavior?
Show answer
Tasks b and c can run in parallel, because both depend only on a and not on each other. Task d waits for both b and c. If c fails, d does not run, because the default rule requires every upstream task to succeed. The run is reported as failed.
Exercise
You rename the task ingest to ingest_orders in the job editor, and the next run fails when the transform notebook calls dbutils.jobs.taskValues.get(taskKey="ingest", key="row_count"). Why, and how do you fix it?
Show answer
The taskKey argument refers to the upstream task's task_key. After the rename, no task has the key ingest, so the lookup finds no value. Change the argument to taskKey="ingest_orders", and check that depends_on in transform also names ingest_orders.