Design a Data Warehouse from Scratch
Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2022-02-07
Problem Statement
Launching a central data warehouse (DW) to consolidate data from multiple operational systems (e.g., product catalog, orders/payments, user activity, marketing, support). The DW will power analytics, BI dashboards, ad-hoc SQL, and ML feature generation. You must design the end-to-end architecture, data models, and serving strategy.
✅ Functional Requirements
1.<span></span>Source Ingestion
2.<span></span>Batch ingestion from OLTP DBs (e.g., MySQL/Postgres) and SaaS APIs (CRM/Ad platforms).
3.<span></span>Streaming ingestion for clickstream/app events.
4.<span></span>Support both full loads (initial backfills) and incremental loads (CDC, append-only).
5.<span></span>Data Modeling
6.<span></span>Raw/staging layer for immutable copies of sources.
7.<span></span>Curated dimensional models (facts & dimensions) for analytics.
8.<span></span>Conformed dimensions across domains (User, Product, Time, Geography).
9.<span></span>Slowly Changing Dimensions (SCD Type 1 & 2) where needed.
10.<span></span>Transformations & Orchestration
11.<span></span>Declarative transformations (SQL-first; e.g., dbt/Spark SQL).
12.<span></span>Orchestrate dependencies, retries, SLAs, backfills.
13.<span></span>Data quality checks (schema, nulls, referential integrity, freshness).
14.<span></span>Serving & Access
15.<span></span>SQL access for analysts (BI tools).
16.<span></span>Aggregated marts for dashboards (daily/hourly KPIs).
17.<span></span>Secure data-sharing interfaces (ML feature store, downstream services).
18.<span></span>Governance
19.<span></span>Centralized catalog/lineage, schema versioning.
20.<span></span>Row/column-level security; PII handling and masking.
21.<span></span>Auditing & access controls (RBAC/ABAC).
⚙️ Non-Functional Requirements
1.<span></span>Scalability: TB→PB scale, thousands of concurrent queries.
2.<span></span>Performance: BI queries < 2–5s on hot aggregates; large scans acceptable via warehouses.
3.<span></span>Reliability: Backfills & pipelines with retries; data correctness guarantees.
4.<span></span>Cost Efficiency: Storage/compute separation, auto-suspend/auto-scale.
5.<span></span>Observability: End-to-end monitoring (freshness, SLAs, failures, query costs).
🔐 Constraints & Assumptions
1.<span></span>Data sources: 5–10 OLTP DBs, 2–3 SaaS APIs, clickstream (~50–200K events/sec peak).
2.<span></span>Privacy/Regulatory: GDPR/CCPA compliance; PII must be tokenized/masked.
3.<span></span>Multi-region users; acceptable eventual consistency (≤ 1 hour) for most marts, but finance requires daily exact reconciliation.
4.<span></span>Budget favors managed storage; team prefers SQL-centric transforms.
🧱 Key Components to Design
1.<span></span>Landing/Raw Layer (Data Lake): immutable, partitioned by source/date (Parquet/ORC).
2.<span></span>Warehouse Engine: columnar MPP (e.g., Snowflake/BigQuery/Redshift/Iceberg+Trino).
3.<span></span>Ingestion Layer:
4.<span></span>Batch: CDC from OLTP (Debezium/DMS), API extractors with incremental cursors.
5.<span></span>Streaming: Kafka/PubSub → stream processor → lake/warehouse sinks.
6.<span></span>Transformation Layer: dbt/Spark for staging → core → marts; tests & documentation.
7.<span></span>Orchestration: Airflow/Prefect; SLAs, retries, backfills, parameterized runs.
8.<span></span>Metadata & Governance: Data catalog/lineage (e.g., OpenLineage/Amundsen/Unity Catalog), RBAC, policy engine.
9.<span></span>Quality & Observability: Freshness monitors, anomaly detection on volumes & metrics.
10.<span></span>Serving Layer: BI (Looker/Tableau/Power BI), semantic layer, ML feature store, APIs.
11.<span></span>Security: KMS encryption, row/column masking, tokenization, VPC/private endpoints.
🧪 Example Scope (E-commerce)
1.<span></span>Facts: orders_fct, payments_fct, sessions_fct, clicks_fct, shipments_fct.
2.<span></span>Dimensions: dim_user (SCD2), dim_product, dim_seller, dim_geo, dim_time.
3.<span></span>Marts: Marketing (attribution, ROAS), Product (conversion funnel), Finance (revenue, refunds), Ops (fulfillment SLAs).
📈 SLAs & Success Metrics
1.<span></span>Data freshness: clickstream ≤ 15 min; core sales marts ≤ 1 hr; finance close T+1 06:00.
2.<span></span>Pipeline reliability: ≥ 99.9% on-time runs; auto-retry with alerting.
3.<span></span>Query performance: P95 dashboard queries ≤ 3s on aggregates.
4.<span></span>Data quality: < 0.1% rows failing critical checks; zero PII leakage.
🔄 Trade-offs to Discuss
1.<span></span>Lakehouse vs. Cloud DW (cost vs. simplicity; open formats vs. managed features).
2.<span></span>CDC vs. periodic snapshots (history fidelity vs. complexity).
3.<span></span>SCD1 vs. SCD2 (simplicity vs. historical accuracy).
4.<span></span>Star/Snowflake vs. Data Vault (agility vs. normalization).
5.<span></span>Materialized views vs. precomputed tables (latency vs. storage/cost).
6.<span></span>Single-region vs. multi-region (latency, egress, governance).
🔍 Interview Prompts (Deep Dives)
1.<span></span>Pick a PII handling strategy end-to-end (masking, tokenization, join keys).
2.<span></span>Design idempotent backfills and reprocessing for late/duplicate events.
3.<span></span>Choose a partitioning & clustering scheme for large facts to optimize costs.
4.<span></span>Outline cost controls (compute quotas, auto-suspend, caching strategies).
5.<span></span>Propose a semantic layer (metrics-as-code) to unify KPI definitions.