Shelves / Data Engineering for Analytics
DE·08
Data Quality, Contracts and Observability
Keeping analytical data trustworthy: the dimensions of quality (freshness, volume, completeness, uniqueness, validity, consistency, accuracy, distribution), where to check (at ingestion, in transformations, before publishing), anomaly detection that respects seasonality, data contracts between producing and consuming teams, lineage for impact and root-cause analysis, circuit breakers that stop bad data from reaching dashboards, and running data incidents; with a lab that injects five silent failures into 60 days of loads and catches all five with no false alarms, where a "row count > 0" check catches none.
Start here: pipelines that succeed and still lie
The worst data failures don't fail. The pipeline runs, the dashboard refreshes, and the numbers are wrong: a partner feed delivered 40% of its rows, an app release stopped sending kitchen_id for some orders, a producer changed amounts from paise to rupees, a column was renamed upstream. Nobody notices until a manager asks why revenue fell 99% on Tuesday.
Data quality is the practice of checking data, not just pipelines, continuously and automatically, the way observability checks services. Data contracts make the producing teams part of it.
Five silent failures, measured
The lab simulated 60 days of order loads (with a weekly pattern) and injected five realistic failures. Checks ran on each load:
| Day | What went wrong | Check that caught it |
|---|---|---|
| 45 | a partner feed failed: 40% of rows missing | volume (compared with the same weekday in recent weeks) |
| 50 | 8% of orders without kitchen_id |
null rate |
| 53 | the load ran 9 hours late | freshness |
| 56 | amounts arrived in rupees, not paise | distribution (mean shift) |
| 58 | status renamed to order_status upstream |
schema contract |
All five caught, no false alarms on the other 55 days. A "row count > 0" check, the kind many pipelines have, caught none of them. And comparing volume with the same weekday (rather than a plain trailing average) detected drops as small as 14% instead of 32%.
How deep to go
Volume 17 defines six mastery levels. For data quality:
| Level | What you can do |
|---|---|
| L1 | Name the dimensions of data quality |
| L2 | Add tests and checks to pipelines |
| L3 | Explain anomaly detection, seasonality, contracts and lineage |
| L4 | Find the root cause of a wrong number across systems |
| L5 | Design quality, contracts and incident processes for a data platform |
| L6 | Run them across many teams with measurable trust (data SLOs) |
The dimensions of quality
| Dimension | Question | Example check |
|---|---|---|
| freshness | is it up to date? | latest loaded_at within 2 hours of expected |
| volume | is all of it here? | rows within the expected range for this weekday |
| completeness | are required fields present? | kitchen_id null rate < 0.1% |
| uniqueness | is each entity once? | order_id unique (chapter 04's lab: 37 duplicates) |
| validity | are values allowed? | status in the accepted set; amounts > 0 |
| consistency / referential | do related tables agree? | every payment's order_id exists |
| accuracy | does it match reality? | revenue reconciles with the payment provider's settlement |
| distribution | does it look like usual? | mean amount, category shares within normal bounds |
| schema | is the shape as agreed? | columns, types and names match the contract |
Where to check
- At ingestion (raw): schema against the contract, freshness, volume. Reject or quarantine early.
- In transformations (staging, marts): uniqueness of grain keys, referential integrity, validity; dbt tests (chapter 04).
- Before publishing (marts to dashboards): distribution and reconciliation checks; the circuit breaker lives here.
- In production use: dashboard-level checks (did yesterday's headline number move more than expected?).
Assign severity: blocking (stop publishing) versus warning (publish with a flag), and owners for every check.
Anomaly detection that doesn't cry wolf
Static thresholds ("rows > 10,000") break with growth and seasonality; naive statistics alarm on every Monday. Better:
- compare with comparable periods (same weekday, same hour, holiday calendars), as the lab did;
- use robust statistics (median and median absolute deviation) so past anomalies don't distort baselines;
- set a minimum meaningful change (the lab's 3% floor) to avoid alarms on tiny, irrelevant shifts;
- tune on history: replay checks over past months and count false alarms before turning on paging.
Data contracts
A contract for the orders.v1 events (Volume 19, chapter 211E) might say:
contract: orders.v1
owner: ordering-team
schema: contracts/events/orders.v1.avsc # types and required fields
semantics:
amount_paise: integer paise, INR, including GST, excluding delivery fee
placed_at: UTC, ISO-8601, device time corrected by server receipt time
quality:
- order_id unique per event_id
- kitchen_id present for status in (placed, cooked, packed, delivered)
freshness: events published within 60 s of the state change (p99)
change process: additive changes with notice; breaking changes as orders.v2 with 30 days' overlap
consumers: analytics (fct_orders), notifications, assistant ingestion
Enforce it where changes are made: the producer's CI checks schema compatibility (schema registry), runs contract tests, and fails builds that would break consumers. The lab's day-58 rename would have been blocked before deploy.
Lineage
Lineage records which jobs read which datasets and wrote which others, ideally at column level. Sources: dbt's graph, Spark and Airflow integrations emitting OpenLineage events, warehouse query logs. Uses:
- impact analysis before a change ("who uses
stg_payments.amount_paise?"); - root cause in an incident (walk upstream from a wrong dashboard);
- compliance (where does personal data flow? chapter 09).
Circuit breakers and data incidents
When a blocking check fails before publishing:
- Don't publish the new data; keep serving the last good partition; label dashboards "data delayed since …".
- Alert the owning team with the check, the evidence, and lineage links.
- Fix at the source if possible; otherwise patch in staging with a documented, temporary rule.
- Backfill the affected periods (idempotently, chapter 05).
- Postmortem like any incident (Volume 9): detection time, impact, root cause, prevention (often a new contract clause or check).
Track data SLOs: share of days each critical table was fresh and passed checks by its deadline.
Common traps
- Testing pipelines, not data.
- Static thresholds that ignore growth and seasonality.
- Alerts without owners.
- Publishing first, checking later.
- Contracts as documents nobody enforces in CI.
Lab: five silent failures in 60 days of loads
Setup. A Python simulation on the lab machine of 60 daily order loads with realistic variation: about 13,700 orders a day, a ±12% weekly pattern, random noise (σ = 300 rows), a tiny background null rate, load lag of 0.2–1.5 hours, and mean order value around ₹239 (in paise). Five failures were injected:
| Day | Failure |
|---|---|
| 45 | 40% of rows missing (a partner feed failed) |
| 50 | kitchen_id null in 8% of rows |
| 53 | load arrived 9 hours late |
| 56 | amounts sent in rupees instead of paise (mean ÷ 100) |
| 58 | column status renamed to order_status |
Checks run on each day's load:
- volume: |z| > 4 against the same weekday over the previous 4 weeks, with a minimum meaningful change of 3% of the mean;
- nulls:
kitchen_idnull rate > 1%; - freshness: lag > 4 hours;
- distribution: mean amount more than 6 standard deviations from the trailing 28 days;
- schema contract: column list equals the contract.
Results
| Day | Injected | Alerts |
|---|---|---|
| 45 | volume | volume |
| 50 | nulls | nulls |
| 53 | freshness | freshness |
| 56 | distribution | distribution |
| 58 | schema | schema contract |
False alarms on the other 55 days: 0. A "row count > 0" check would have caught 0 of 5.
How small a volume drop on day 45 each volume check can detect (holding everything else the same):
| Volume check | Smallest drop detected | False alarms on normal days |
|---|---|---|
| trailing 28 days (all weekdays), |z| > 3 | 32% | 0 |
| same weekday, last 4 weeks, |z| > 4, 3% floor | 14% | 0 |
What it shows
- Each failure type needs its own check. Volume, completeness, timeliness, value distributions and shape fail independently; no single metric covers them.
- The usual pipeline checks miss all of them: every load had rows and every job succeeded.
- Seasonality-aware baselines detect much smaller problems at the same false-alarm rate, because they compare like with like.
- Thresholds were tuned on this history. Real data needs the same step: replay candidate checks over months of past loads and count alarms before enabling them.
Try it yourself
- Add a holiday (day 30 at 50% volume, expected) and make the volume check use a holiday calendar so it doesn't alarm.
- Replace mean and standard deviation with median and MAD, inject an earlier anomaly into the baseline window, and compare robustness.
- Implement the same checks as dbt tests (dbt-utils, dbt-expectations) on chapter 04's project.
- Add a reconciliation check: daily revenue in the warehouse versus a simulated payment-provider settlement with 0.1% noise.
Revision sheet: data quality, contracts and observability
Key ideas
- Pipelines can succeed with wrong data; check the data.
- Dimensions: freshness, volume, completeness, uniqueness, validity, consistency, accuracy, distribution, schema.
- Check at ingestion, in transformations, before publishing; severities and owners.
- Seasonality-aware, robust, tuned anomaly detection.
- Data contracts owned by producers, enforced in producers' CI.
- Lineage (OpenLineage, dbt) for impact and root cause.
- Circuit breakers keep the last good data; data incidents get postmortems; data SLOs measure trust.
Numbers to remember
- Lab: 5 of 5 failures caught, 0 false alarms; "row count > 0" caught 0; same-weekday baseline detects 14% drops vs 32% for a plain trailing baseline.
Glossary
- Data contract: a versioned agreement on schema, semantics, quality and freshness.
- Circuit breaker: blocking publication when checks fail.
- Lineage: which jobs read and wrote which datasets (and columns).
- Reconciliation: comparing totals with the system of record.
- Data SLO: a target for freshness and quality of a dataset.