Retrying a Failed Build
Concepts covered: Run artifacts
A nightly build has 300 nodes. Node 280 is a test that finds a null customer id, and the 20 nodes that depend on it are skipped. Once the data is fixed, running dbt build again rebuilds the 279 nodes that already succeeded before it reaches the 21 that did not. dbt retry runs only those 21.
dbt retry works from the last run_results.json
You do not pass dbt retry a selection. It opens target/run_results.json, the file the failed command left, and reads 2 things from it: the command that ran, and the status of every node. It reruns that command, in lineage order, over every node whose status was error or fail, plus every node marked skipped, and leaves each success and pass alone. It reads the project as it stands now, so a corrected model file is picked up, and it writes a new run_results.json when it finishes, so a second failure can be retried in turn.
In the animation, the build's first node is a not_null test on raw.orders.customer_id. Order 4 has no customer id, so the test fails and both models are skipped. Order 4 is then deleted from the raw table, and dbt retry runs.
select
order_id,
customer_id,
order_date,
amount,
amount > 50 as is_large
from {{ ref('stg_orders') }}
- dbt build
- dbt retry
fct_orders ends with 3 rows, orders 1 to 3. Every node reran here because the failure sat at the very start of the lineage, before anything had succeeded. Retry saves work in proportion to how much of the build passed before the failure.
When the fix lives under a node that passed
Retry reruns the nodes that did not succeed. It has no way to know which nodes your fix touched. Take the project from the selection section, add a not_null test on stg_customers.region, and say customer 103 arrives in raw.customers with no region.
| Node | Status after dbt build | Rerun by dbt retry? |
|---|---|---|
| stg_orders | success | No |
| stg_customers | success | No |
| not_null on stg_customers.region | fail | Yes |
| fct_orders | skipped | Yes |
| daily_revenue | skipped | Yes |
The table stg_customers built successfully with the null in it, and only then did its test run. Fix the region in raw.customers and run dbt retry: it reruns the test against the stg_customers table from the failed build, which still holds the null, and the test fails again. The fix sits upstream of a node that passed, so the rebuild has to start at that node. dbt build --select stg_customers+ rebuilds stg_customers from the corrected source and tests it, then builds fct_orders and daily_revenue behind it.
Retry and a + selector answer different questions. Retry asks which nodes did not succeed. The selector asks which tables are stale. When the 2 lists match, retry is shorter to type and cannot be mistyped. When they differ, the selector is the one that matches the data.
Practicing a recovery
On your dev target, add a not_null test to a staging model and put a null into a source table you control. Run dbt build, read the statuses in run_results.json, and write down what dbt retry will rerun before changing anything. Correct the source and retry. If the test fails again, you have reproduced the case above, and the rebuild starts from the model that passed.
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.