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.
- 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
| Setting | Value |
|---|---|
| Schedule | 0 6 * * *, which is 06:00 every day |
| Code | the latest commit on the main branch |
| Profile | one whose default target is prod |
| Step 1 | dbt source freshness |
| Step 2 | dbt build --target prod, only if step 1 passed |
| On failure | stop, 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.
- It builds reviewed code: The job checks out main, so a model reaches production only after someone reviewed and merged it.
- Only the job writes to prod: The prod target lives in the scheduler's profile, and engineers build into their own schemas.
- A second run changes nothing: Table models rebuild from scratch, and an incremental model that keeps only rows newer than the latest in {{ this }} adds none when nothing new arrived.
- Tests run inside the build: dbt build tests each model before its children, so a failed check stops bad rows short of the tables dashboards read.
- Sources are checked first: dbt source freshness fails the job when a loader is late, instead of building today's tables from yesterday's data.
- Runs never overlap: The schedule leaves room for the longest build, and the scheduler refuses to start a run while the last one is going.
- A failure reaches a person: dbt exits with a nonzero code on failure, the job alerts on it, and the saved run_results.json shows what to retry.
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.