Post

Databricks Workflows for Scheduled Jobs: A Practical Guide

In this article, let us create a Databricks Workflow for a small scheduled data pipeline. We will configure multiple tasks, pass parameters, add a schedule, and see what happens when a task fails. This approach is useful when notebooks or Python code already run in Databricks and we want orchestration without bringing in another tool such as Airflow.

For our example, a pipeline loads daily order data into a Bronze Delta table, transforms it into a Silver table, and finally runs a data quality check. The example is simple, but it has the same basic pieces that I normally need in a production batch job.

What is a Databricks Workflow?

A Workflow is Databricks’ built-in orchestration feature. A job contains one or more tasks, and each task can run a notebook, Python wheel, SQL query, dbt task, JAR, or another Databricks job. Dependencies between tasks form a directed acyclic graph.

The naming can be slightly confusing at first: the job is the complete workflow, while a task is one unit inside it. A job can run manually, from a schedule, through the REST API, or after another system triggers it.

OptionWhere it fitsMain trade-off
Databricks WorkflowsPipelines mostly running in DatabricksSimple setup, but tied to Databricks
Apache AirflowPipelines spanning many platformsFlexible, but another service to operate
Cloud scheduler plus APIA few simple triggersCheap and direct, but limited orchestration

For our use case, all processing happens in Databricks, so a Workflow keeps the setup straightforward.

1. Create the notebooks

Let us start with three notebooks. The first notebook accepts a processing date and appends source data to a Bronze table.

1
2
3
4
5
6
7
8
9
10
11
# 01_bronze_orders
dbutils.widgets.text("process_date", "")
process_date = dbutils.widgets.get("process_date")

source_path = f"/Volumes/demo/raw/orders/{process_date}"
orders_df = spark.read.option("header", True).csv(source_path)

(orders_df.write
    .format("delta")
    .mode("append")
    .saveAsTable("demo.bronze.orders"))

The Silver notebook cleans the records for only that processing date. Using a parameter here is important. If a retry processes the complete Bronze table every time, the job can become slower and can also create duplicates.

1
2
3
4
5
6
7
8
9
10
11
12
DELETE FROM demo.silver.orders
WHERE order_date = '${process_date}';

INSERT INTO demo.silver.orders
SELECT
  CAST(order_id AS BIGINT) AS order_id,
  CAST(customer_id AS BIGINT) AS customer_id,
  CAST(order_total AS DECIMAL(12, 2)) AS order_total,
  CAST(order_date AS DATE) AS order_date
FROM demo.bronze.orders
WHERE order_date = '${process_date}'
  AND order_id IS NOT NULL;

The last notebook performs a basic quality check and fails the task if no records were loaded.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 03_check_orders
dbutils.widgets.text("process_date", "")
process_date = dbutils.widgets.get("process_date")

row_count = spark.sql(f"""
    SELECT COUNT(*) AS count
    FROM demo.silver.orders
    WHERE order_date = '{process_date}'
""").first()["count"]

if row_count == 0:
    raise ValueError(f"No Silver orders found for {process_date}")

print(f"Quality check passed with {row_count} rows")

This is only a demo check. In a production pipeline, I would check duplicates, null percentages, expected ranges, and freshness, possibly using a dedicated data quality framework.

2. Configure the Workflow

From the Databricks workspace, open Workflows, choose Jobs & Pipelines, and click Create job. Give the job a useful name such as daily-orders-pipeline. Names matter more than we think once there are dozens of jobs, so I normally include the domain and frequency.

Create the first task with these values:

  1. Task name: bronze_orders
  2. Type: Notebook
  3. Source: Workspace
  4. Notebook: the path to 01_bronze_orders
  5. Parameter: process_date with value ``
  6. Compute: a job cluster or serverless compute

Databricks supports dynamic value references such as job start time, run ID, and task values. Check the dynamic value picker in the UI because supported references can change and the required date may need a timezone adjustment. For a daily Australian pipeline, the UTC job start date is not always the same as the Melbourne business date.

Add silver_orders as the second task and make it depend on bronze_orders. Pass the same process_date. Then add check_orders, depending on silver_orders. The graph should now be a straight line:

1
bronze_orders -> silver_orders -> check_orders

By default, a downstream task runs only when its dependencies succeed. Databricks also supports run-if conditions such as running when at least one upstream task fails. That is useful for cleanup or notification tasks, although I prefer platform notifications for basic failure alerts.

3. Choose the compute

For scheduled jobs, job clusters are usually a better choice than an all-purpose interactive cluster. A job cluster starts for the run and terminates afterwards, which gives better isolation and avoids leaving compute running. Serverless jobs can reduce startup and management work further if the workspace and workload support them.

For this small example, choose a single-node or small autoscaling job cluster. Set the Databricks Runtime explicitly instead of always selecting the newest runtime. Production upgrades should be tested, especially when Python packages or Spark behaviour may change.

4. Add a schedule and notifications

Open the job schedule and select Scheduled. Configure it to run every day at the required time and choose the correct timezone. For example, a job scheduled at 2:00 AM in Australia/Melbourne follows daylight-saving changes, while a UTC schedule stays fixed.

Also configure notifications for failed runs and, if needed, duration warnings. A duration warning is useful for detecting a job that is technically still running but is much slower than normal. Avoid success emails for every daily job because they quickly become noise.

Databricks allows concurrent runs depending on job settings. For a daily overwrite or merge pipeline, set the maximum concurrent runs to one unless the code safely handles overlapping dates. Queueing a run is normally safer than two runs modifying the same Delta table together.

5. Configure retries and timeouts

A temporary network issue should not require someone to restart the complete job. On each task, configure perhaps two retries with a five-minute interval. Also add a realistic timeout. Retries help with transient failures, but they should not hide deterministic problems such as invalid SQL or missing source files.

Idempotency is important here. Our Silver task deletes the date partition before inserting it again, so rerunning the same date gives the same result. For larger tables I would use MERGE or replace only the relevant partition instead of running a broad delete. The Bronze task in this example appends, so it is not fully idempotent. In production, add a source file identifier, use Auto Loader checkpoints, or merge on a stable business key.

6. Test and monitor the job

Before enabling the schedule, click Run now and provide a known date with sample input. Open the run details to see task duration, cluster startup, notebook output, and error messages. Deliberately test a date with no data as well. We should see the quality task fail rather than silently report a successful pipeline.

The Runs page is enough for basic operations. For a larger platform, use system tables or the Jobs API to collect run history into an operational dashboard. Useful measurements include success rate, queue time, execution duration, retry count, and estimated compute cost.

One practical limitation is that a green Workflow only means every configured task succeeded. It does not prove that the correct number of records arrived or that downstream users received fresh data. Quality and freshness checks have to be part of the workflow.

Another thing to watch is source availability. A fixed 2:00 AM schedule assumes the upstream file has arrived by then. If arrival time varies, either add a sensor-like check with controlled retries or let the upstream system trigger the job through the API. Keeping a cluster alive while waiting for hours is normally wasteful.

Conclusion

Databricks Workflows provide a practical way to schedule notebooks, SQL, and packaged code when most of the pipeline already lives in Databricks. The main work is not creating three boxes in the task graph; it is making each task parameterised, idempotent, observable, and safe to retry. Start with a small workflow like this one, test both success and failure paths, and then add production controls as the pipeline becomes important.

This post is licensed under CC BY 4.0 by the author.