Requirements and scope:- Ensure daily revenue metric (ETL pipeline → warehouse → BI dashboard) is accurate and trustworthy.- Detect ingestion failures, schema drift, data loss, reconciliation mismatches, and anomalies.High-level architecture:- Instrumented ETL (Airflow), data warehouse (Redshift/BigQuery), monitoring DB/table, alerting (PagerDuty/Slack/email), and dashboards.Core automated checks (run after each ETL job and nightly/full-run):1. Ingestion health- Row-count compare (source vs staging)sql
-- source_count and staging_count are results from respective sources
SELECT
src.day,
src.count AS source_count,
stg.count AS staging_count,
(stg.count - src.count) AS diff,
SAFE_DIVIDE(stg.count, src.count) AS ratio
FROM source_counts src JOIN staging_counts stg USING(day);
Threshold: fail if ratio < 0.99 or >1.01.2. Null-rate & schema checkssql
SELECT
'revenue' AS col,
COUNT(*) AS total_rows,
SUM(CASE WHEN revenue IS NULL THEN 1 ELSE 0 END) AS nulls,
SAFE_DIVIDE(SUM(CASE WHEN revenue IS NULL THEN 1 ELSE 0 END), COUNT(*)) AS null_rate
FROM staging.revenue
WHERE day = DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY);
Alert if null_rate > 0.005 (0.5%) or any unexpected column type change.3. Reconciliation totals (staging → warehouse → dashboard)sql
SELECT
SUM(revenue) AS stg_total
FROM staging.revenue WHERE day = '2025-12-04';
SELECT
SUM(revenue) AS fact_total
FROM analytics.fact_revenue WHERE day = '2025-12-04';
Threshold: absolute difference > $500 or relative > 0.5% triggers high-severity alert.4. Duplicate / uniqueness checkssql
SELECT id, COUNT(*) c FROM staging.revenue GROUP BY id HAVING c > 1 LIMIT 10;
Alert on duplicates > 0.5. Freshness / latency- Check last_ingest_timestamp per source; alert if > 2x expected interval.6. Statistical anomaly detection (daily revenue vs 7-day rolling mean ± 3σ)sql
WITH stats AS (
SELECT
day,
SUM(revenue) AS daily_rev,
AVG(SUM(revenue)) OVER (ORDER BY day ROWS BETWEEN 7 PRECEDING AND 1 PRECEDING) AS mu,
STDDEV_POP(SUM(revenue)) OVER (ORDER BY day ROWS BETWEEN 7 PRECEDING AND 1 PRECEDING) AS sigma
FROM staging.revenue
GROUP BY day
)
SELECT day, daily_rev, mu, sigma
FROM stats
WHERE ABS(daily_rev - mu) > 3*sigma;
Alert as warning; escalate if sustained or paired with reconciliation failure.Alerting cadence & severity:- Critical (P0): ingestion missing, reconciliation fail, schema change — immediate page (PagerDuty) + Slack + email; require acknowledgement within 15 min.- High (P1): duplicates, high null_rate, large drift — Slack + email, review within 1 hour.- Medium (P2): single-day anomaly without other failures — daily digest for analyst review.- Low (P3): minor threshold drift — weekly report.Alert content:- Job/run id, timestamps, failing check, SQL snippet, affected rows, links to raw logs and recent historical metrics, run playbook link.Triage playbook (when alert fires):1. Acknowledge and classify severity.2. Quick sanity checks (last successful run, pipeline logs, system resource alerts).3. Run targeted SQL queries: - Re-run counts with source and staging slices. - Sample raw rows for failed partitions.4. Root-cause hypotheses: - Upstream schema change, late upstream job, code bug, null propagation, duplicate ingestion, partition overlap.5. Fix or mitigate: - Re-run ETL for affected partition, roll back transform, apply hotfix, or mark dashboard as stale (disable auto-update).6. Postmortem: - Document RCA, time-to-detect, time-to-resolve, and add automated guards (tighten thresholds, add tests).7. Continuous improvement: - Track alert noise; adjust thresholds and introduce automated reconciliation jobs or checkpoints.Operational best practices:- Keep checks idempotent and fast (target < 5 minutes).- Maintain historical metrics & alert history to tune thresholds.- Implement test data and canary runs for schema or logic changes.- Include data owners in on-call rotation for critical business metrics.This design balances fast detection, prioritized alerts, actionable diagnostics (SQL snippets), and a clear triage workflow to restore trust in the revenue metric.