Why Test Source Freshness?
Source freshness checks serve several important purposes:How Source Freshness Works
dbt’s source freshness checking works by:- Examining a timestamp column in your source tables
- Comparing that timestamp to the current time
- Evaluating the difference against your defined thresholds
Example Freshness Check FailuresWhen data exceeds your freshness thresholds, dbt will report failures like these:
When data exceeds your freshness thresholds, dbt will report failures like these:The warnings and errors include the source name, how much data is missing, the timestamp of the most recent record, and your configured threshold.
When data exceeds your freshness thresholds, dbt will report failures like these:The warnings and errors include the source name, how much data is missing, the timestamp of the most recent record, and your configured threshold.
Configuring Freshness Checks
To enable freshness checks, you need to add two key elements to yoursources.yml file:
- A
loaded_at_fieldthat identifies the timestamp column - A
freshnessconfiguration that defines your thresholds
Understanding the Configuration
loaded_at_field: The column containing the timestamp when data was loadedwarn_after: When to issue a warning about stale dataerror_after: When to report an error about stale data (typically more lenient than warnings)
Valid Time Periods for Freshness ChecksWhen configuring source freshness thresholds, you must specify both a count and a time period. The time period can be:
minute- For data that updates very frequently (e.g., real-time systems)hour- For data that updates throughout the day (e.g., transaction systems)day- For data that updates daily or less frequently (e.g., batch uploads)
warn_after: {count: 6, period: hour} would warn if data is more than 6 hours old.Table-Specific Freshness Settings
You can override source-level freshness settings for specific tables:Running Freshness Checks
To check the freshness of your source data, run:- Queries each source table for the most recent
loaded_at_fieldvalue - Compares that timestamp against the current time
- Evaluates it against your thresholds
- Reports success, warning, or error for each source
Where Freshness Check Results AppearWhen you run
dbt source freshness, the results appear in multiple places:- Terminal Output: Warnings and errors are displayed in your command line interface immediately when running the command
- Log Files: Results are written to the log files in your project’s
logs/directory - Artifacts: Detailed results are stored in the
target/sources.jsonfile - dbt Cloud (if using): Results appear in the run history and can trigger notifications
- Paradime Interface (if using): Results are displayed in the Paradime UI with history tracking
Example Output
Integrating Freshness Checks into Workflows
Freshness checks can be integrated into your data pipeline in several ways:Scheduled Checks
Run freshness checks on a regular schedule to proactively monitor your data:Pre-run Validation
Execute freshness checks before running models to prevent building on stale data:CI/CD Pipeline Integration
Include freshness checks in your CI/CD workflows:Optimizing Freshness Checks for Large Tables
For very large datasets, running freshness checks can be resource-intensive. You can optimize them using filters:filter clause limits the rows that dbt examines when checking freshness, improving performance for large tables.
Handling Different Data Loading Patterns
Different sources may have various data loading patterns that affect how you configure freshness:Troubleshooting Freshness Issues
If your freshness checks are failing, consider these common issues:- Incorrect timestamp column: Verify that
loaded_at_fieldis the right column - Timezone differences: Check if there are timezone discrepancies between source timestamps and dbt
- Data loading failures: Investigate upstream ETL/ELT processes
- Unrealistic expectations: Adjust thresholds to match actual data loading patterns
Best Practices
Automating Source Generation with DinoAIWith DinoAI (Paradime’s AI Agent), you can automatically generate source definitions with appropriate freshness configurations. See step by step instructions for more details.