Running dbt on a Schedule

Concepts covered: Running dbt in production

Dashboards read the prod schema, and a prod table is only as current as the last prod build. A scheduled job takes the person out of that loop. A scheduler starts the job at fixed times on a machine set up for production. The job checks out the code and runs dbt against the prod profile, then reports how the run ended.

The daily job: the loader lands raw data by 05:30, the job starts at 06:00, the freshness check gates the build, and dashboards read the prod schema from 07:00.
  • Raw loads (source, loader)
  • dbt source freshness (quality gate, scheduler); on error: stop job and alert
  • dbt build --target prod (transform, scheduler); on error: stop, alert, save target/; idempotency: rerun rebuilds same tables
  • analytics schema (storage, warehouse); freshness: current by 07:00
  • Dashboards (consumer, BI tool)
  • Raw loads -> dbt source freshness
  • dbt source freshness -> dbt build --target prod
  • dbt build --target prod -> analytics schema
  • analytics schema -> Dashboards
SettingValue
Schedule0 6 * * *, which is 06:00 every day
Codethe latest commit on the main branch
Profileone whose default target is prod
Step 1dbt source freshness
Step 2dbt build --target prod, only if step 1 passed
On failurestop, alert the team, save target/

0 6 * * * is a cron expression: minute 0, hour 6, any day of the month, any month, any day of the week. The hour is read in the scheduler's time zone, so teams usually run the server on UTC and write every job time in UTC to remove the doubt.

Conditions for a safe schedule

A schedule repeats whatever the job does, mistakes included, every day with nobody watching. Each condition below closes one way that repetition can go wrong.

A late loader, worked through

The freshness check reads 2 settings from the source's YAML. loaded_at_field names a timestamp column the loader fills as it writes each row, and error_after sets how old the newest value in that column may be. dbt source freshness selects the maximum of that column, subtracts it from the current time, and exits with a nonzero code when the gap is past the limit.

sources:
  - name: raw
    schema: raw
    tables:
      - name: orders
        config:
          loaded_at_field: loaded_at
          freshness:
            error_after:
              count: 12
              period: hour

Say the loader lands raw.orders by 05:30 on most days and the build takes 40 minutes, so dashboards are current by 06:40. One morning the loader finishes at 06:15. At 06:00 the newest loaded_at in raw.orders is still yesterday's 05:30 load, 24.5 hours old and past the 12-hour limit, so dbt source freshness exits with an error. The job stops before dbt build, the alert goes out, and every prod table still holds yesterday's correct data. An engineer reruns the job at 06:20, when the newest row is 5 minutes old, and the tables are current by 07:00. The failure cost 20 minutes of delay. Building on the stale source would have published tables that looked current and were not.

When a test fails in a scheduled build, the model it checks has already been rebuilt, but every model after it keeps the rows from its previous run. Dashboards reading those later tables see yesterday's complete data instead of today's broken data, which is the safe failure the conditions above aim for. Whether dbt retry or a + selector finishes the job depends on where the fix lands, as the previous section showed.

Practicing a deployment

You can rehearse the whole job on your own machine. Use the full target from the first section, write a short script that runs dbt source freshness and then dbt build --target full, stopping if the first command fails, and give it a cron entry a few minutes ahead. Corrupt one source row and let the job run. Once it has run, read run_results.json and recover.

About this lesson

This section is part of the Deploying dbt: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.