Sub topic 1
What a job, a task, and a run are
Prerequisites
This chapter assumes you have:
- Used Databricks notebooks to run Python or SQL.
- A general idea of what clusters and compute are in Databricks.
- A basic understanding of cron-style schedules, which later chapters cover in detail.
You do not need to have created a job before. The examples use the Jobs API 2.1 JSON format, but everything shown can also be done in the Jobs UI.
The three core objects
Databricks Jobs (also called Lakeflow Jobs in newer documentation) is the built-in orchestrator for running work without someone clicking "Run all". Three objects make up its model. Keeping them apart is the foundation for every later chapter.
| Object | What it is | Lifetime |
|---|---|---|
| Job | The saved definition: tasks, dependencies, compute, schedule, parameters, notifications. | Persists until you delete it. |
| Task | One unit of work inside a job, such as a notebook, a SQL query, or a pipeline update. | Part of the job definition. |
| Run | One execution of a job. It has a start time, an end time, and a result. | Created each time the job fires. Kept in run history. |
A useful analogy from software: a job is like a class definition, and a run is like an instance of it. Editing the job changes future runs. It does not change runs that have already finished.
Each task in a run also produces its own record, often called a task run. A job run is a container for the task runs inside it. Its overall state is derived from them.
Identifiers you will see
job_ididentifies the job definition. It stays the same across edits.run_ididentifies a single run. Every run has a new one.task_keyidentifies a task within a job. It must be unique inside that job, and other tasks refer to it independs_on.
Task keys matter more than they first appear. Renaming a task_key is a structural change. Anything that referenced the old key, such as depends_on entries or {{tasks.ingest.values.row_count}} references, must be updated too.
Anatomy of a job definition
The following Jobs API 2.1 definition has three notebook tasks that run in sequence on one shared cluster. It runs daily at 06:00 UTC, retries the first task twice, and allows only one run at a time.
{
"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"}
}
]
}The node type i3.xlarge is specific to AWS. On Azure or Google Cloud you would use a different value.
Read the definition in layers:
- Job-level settings describe the whole job:
name,max_concurrent_runs, andschedule. They apply to every run. - Shared resources are declared once and referenced by key. Here
job_clustersdefinesetl_cluster, and each task points to it withjob_cluster_key. - Tasks describe the work. Each has a unique
task_key, a task-type block (herenotebook_task), and optional settings such asdepends_onandmax_retries.
Settings placed on a task, such as retries, affect only that task. Settings placed on the job, such as the schedule, affect the job as a whole. Several later chapters are about deciding which level a setting belongs to.
The job as a graph
Tasks in a job form a directed acyclic graph (DAG). Each depends_on entry is an edge, and cycles are not allowed. The job above is a straight line. Later chapters add branches, parallel tasks, and conditions.
A task with no depends_on is a root task. Databricks starts root tasks as soon as the run begins. If a job has several root tasks, they start in parallel, subject to compute availability.
What happens during a run
A run moves through a lifecycle. A run starts when something triggers the job: the schedule, a manual "Run now" click, an API call, a file-arrival trigger, or another job. The trigger type is recorded on the run, which helps when you investigate why something ran at an odd time.
The diagram is simplified. The Jobs API reports a life cycle state (for example PENDING, RUNNING, TERMINATED) separately from a result state (for example SUCCESS or FAILED). A run can be TERMINATED and still have failed. When you script against the API, check the result state, not only whether the run has finished.
Within a run, each task goes through its own lifecycle:
- The task waits until every task it depends on has finished.
- Databricks checks the dependency outcomes. By default a task runs only if all its dependencies succeeded.
- The task gets compute. This may mean starting a new cluster, which takes minutes, or reusing one that is already running.
- The task executes and ends with a result.
If a task fails after its retries are used up, its downstream tasks do not run by default. They are marked as skipped or upstream failed, and the run as a whole is marked failed. The run_if setting changes this behavior, and a later chapter covers it.
Run history and what is recorded
Each run stores its own record, which you can open from the job's Runs tab or fetch from the API. A run record includes:
- The trigger type and start and end times.
- The result of each task and any error message.
- The notebook output, with the rendered notebook as it executed. Notebook tasks keep a snapshot of the executed notebook, including cell outputs.
- The parameters the run used.
- Links to Spark UI and logs for the compute that executed each task.
Because the record is separate from the job, you can still inspect an old run after you change the job. The run keeps the settings it started with.
Retention is limited. Databricks keeps run history for a fixed period and caps the number of runs it keeps per workspace, so export results you need to keep long term to a table or storage location.
Common mistakes at this stage
- Confusing job and run. "The job failed" is ambiguous. State which run failed. Fixing the job definition does not repair the failed run.
- Putting everything in one task. One huge notebook gives you one retry unit, one result, and no partial reruns. Splitting work into tasks gives you finer retries and clearer failure points.
- Splitting too finely. Each task adds scheduling overhead and, with separate compute, a cluster start. Splitting at natural boundaries such as ingest, transform, and publish works better than one task per cell.
- Duplicate or changing task keys. Keys must be unique within the job. Treat them as stable identifiers.
- Expecting edits to apply to a running run. A change to a job takes effect for runs that start afterward.
Creating a job with the API
You can create the job above by sending the definition to the Jobs API. This Python example uses the requests library and reads credentials from environment variables, so no token appears in the code. It assumes the JSON is saved as job.json.
import json
import os
import requests
host = os.environ["DATABRICKS_HOST"].rstrip("/")
token = os.environ["DATABRICKS_TOKEN"]
headers = {"Authorization": f"Bearer {token}"}
with open("job.json") as f:
job_definition = json.load(f)
create = requests.post(
f"{host}/api/2.1/jobs/create", headers=headers, json=job_definition
)
create.raise_for_status()
job_id = create.json()["job_id"]
print("Created job", job_id)
run = requests.post(
f"{host}/api/2.1/jobs/run-now", headers=headers, json={"job_id": job_id}
)
run.raise_for_status()
print("Started run", run.json()["run_id"])The create call returns a job_id, which names the definition. The run-now call returns a run_id, which names one execution. Calling run-now again produces a second run of the same job with a new run_id.
Summary of the model
- A job is the reusable definition. A run is one execution of it.
- A task is a unit of work in the job. Tasks form a DAG through
depends_on. - Job-level settings apply to the whole job, and task-level settings apply to one task.
- Run records are independent of later edits to the job.
Exercise
A teammate says, "I fixed the job, so the failed 06:00 run is fixed now." Is that correct? Explain using the terms job, task, and run.
Show answer
No. Editing the job changes the definition used by future runs. The failed 06:00 run is a separate record that keeps the settings it started with, so it stays failed. To get its results you must start a new run, or repair the failed run, which reruns only the failed task runs and the ones that depend on them. That is covered in a later chapter.
Exercise
In the definition above, which setting would you change to let transform run on a larger cluster than ingest, and what must you add to the job?
Show answer
Give transform a different job_cluster_key, and add a second entry to job_clusters with that key and a larger new_cluster configuration. Compute is chosen per task through the key, while the cluster definitions are declared once at the job level.