Dimensional Model for a Marketplace Analytics Warehouse
System DesignModel answerLast updated
Design fact and dimension tables for a marketplace to serve five business metrics, covering grain, keys, partitioning, late data, deduplication, anonymous users and changing seller attributes.
You are the data engineer for an online marketplace. The operational systems hold buyers, sellers, orders, order items, payments, and a stream of page-view events. Analysts want a warehouse that answers these five questions:
- Daily conversion rate from session to purchase.
- Average order value.
- Seven-day buyer retention, grouped by signup cohort.
- Order cancellation rate per seller.
- Daily gross merchandise value (GMV) per product category.
Design the warehouse model. No SQL is needed. Your answer should cover:
- The fact and dimension tables you would build, and the grain of each.
- Primary and foreign keys, including any surrogate keys, plus the main attributes and their data types.
- Your partitioning and clustering choices.
- How you handle late-arriving events, duplicate records, anonymous or null users, and seller attributes that change over time.
- Whether you choose a star or snowflake layout, and why.
State your assumptions about scale and metric definitions up front (for example, what counts as a session and whether cancelled orders count toward average order value).
Hints
Hint 1
Pin down each metric as a numerator and a denominator first, and decide the grain of the table each one needs before you list any columns.
Hint 2
Separate event time from ingestion time, partition by event time, and reprocess a sliding window with idempotent overwrites or MERGE so late data and duplicates are corrected safely.
Hint 3
Use a surrogate key with a reserved 'unknown' row for anonymous users, build sessions on an anonymous ID with an identity-bridge table, and use Type 2 history for sellers and product categories so past metrics stay attributed correctly.
Model answer
Hard Try it yourself first — then compare your approach, complexity and edge cases.
1. Pin down definitions and scale
Most mistakes here come from vague metrics rather than the schema, so I would confirm definitions first.
| Metric | Definition |
|---|---|
| Session-to-purchase conversion | Sessions containing at least one placed order, divided by all sessions, per day. A session ends after 30 minutes of inactivity. |
| Average order value (AOV) | Gross value of orders divided by number of orders. Cancelled orders are normally excluded; I would confirm this. |
| 7-day retention | Of the buyers who signed up on a given day, the share who purchase on days 1 to 7 afterward. Purchase versus visit must be agreed. |
| Cancellation rate by seller | Cancelled orders divided by placed orders for that seller, grouped by order date. |
| Daily GMV by category | Sum of quantity times unit price for placed items, in one reporting currency, by order date and category. |
Assumed scale: about 50M daily users, 2B page views per day (around 1 TB raw), 10M orders and 30M order lines per day, 5M sellers, 100M products, two or more years of history. Daily refresh is required, hourly is a bonus. All timestamps are stored in UTC and converted to the business timezone through the date dimension.
2. Architecture
- Ingestion: CDC from the operational database (for example Debezium into Kafka) for orders, items, payments, sellers, and products. Page views arrive as an event stream.
- Raw layer: append-only, immutable, partitioned by ingestion date so anything can be replayed.
- Staging layer: deduplicated, typed, and identity-resolved.
- Core layer: a star schema of facts and conformed dimensions.
- Marts: small aggregate tables, one per metric.
- Orchestration and quality: Airflow or dbt, with checks for row counts, null keys, referential integrity, grain uniqueness, and metric drift.
3. Dimensions
| Table | Grain | Keys | Notable attributes |
|---|---|---|---|
dim_date | One day | date_key INT (yyyymmdd) | date, week, month, holiday flag |
dim_buyer | One buyer (Type 1) | buyer_key BIGINT surrogate, buyer_id natural | signup timestamp, signup date key, country, acquisition channel; reserved row -1 for unknown |
dim_seller | One seller version (Type 2) | seller_key surrogate, seller_id natural | tier, region, status, commission rate DECIMAL(5,4), valid_from, valid_to, is_current |
dim_product | One product version (Type 2 on category) | product_key, product_id | title, brand, category levels 1 to 3, category_id, validity range |
bridge_identity | One anonymous-ID to buyer link | anonymous_id, buyer_id | first linked timestamp |
4. Facts
| Table | Grain | Keys | Measures | Partition / cluster |
|---|---|---|---|---|
fact_page_view | One unique event | event_id PK; buyer_key, anonymous_id, session_id, date_key | event_ts, ingest_ts, page type, product_key, device, referrer | Partition by event date; cluster by session_id, buyer_key |
fact_session | One session | session_id PK; buyer_key, date_key | start/end ts, page-view count, has_order, order count | Partition by start date; cluster by buyer_key |
fact_order | One order (accumulating snapshot) | order_id PK; buyer_key, seller_key, session_id, order_date_key | placed_ts, paid_ts, cancelled_ts, status, gross_amount_usd DECIMAL(18,2), currency, fx_rate, is_cancelled | Partition by order date; cluster by seller_key, buyer_key |
fact_order_item | One order line | (order_id, line_no) PK; product_key, seller_key, category_id | qty, unit price, line GMV, discount, line status | Partition by order date; cluster by category_id, seller_key |
fact_payment | One payment transaction | payment_id PK; order_id | amount, method, status, idempotency key | Partition by payment date |
Money is always DECIMAL, never floating point. I store both original currency and USD, converting at the rate for the order date.
Marts store numerators and denominators, not ratios, so they can be re-aggregated across days and segments:
agg_daily_conversion(date, sessions, converting_sessions)agg_daily_aov(date, orders, gross_value)agg_cohort_retention(signup_date, cohort_size, retained_d7)agg_seller_cancellation(order_date, seller_id, orders, cancelled)agg_daily_category_gmv(date, category_id, gmv)
5. Star versus snowflake
I choose a star schema with denormalized dimensions. Category levels are flattened into dim_product rather than split into their own tables. This avoids extra joins on the largest scans, simplifies BI usage, and costs little because dimensions are tiny compared with facts. Snowflaking is only worth it for a very large, frequently changing sub-hierarchy, which does not apply here.
Surrogate keys are used because they decouple the warehouse from source ID changes, allow multiple versions of a seller or product (Type 2), and give a clean home for the unknown member. Facts reference the surrogate key that was current at event time.
6. Operational concerns
Late-arriving events
- Keep
event_tsandingest_tsseparate and partition by event time so metrics land on the right day. - Each daily run reprocesses a sliding window (say, the last three days) using partition overwrite or MERGE, then recomputes affected marts. Older stragglers are caught by a weekly backfill, and recent days are labeled preliminary.
- Cancellations arrive later via CDC and are merged into
fact_order. They are attributed to the original order date, so the cancellation mart for that date is recomputed. - A retention cohort is only final after seven days plus the late window.
- If an order references a seller or product not yet loaded, insert an inferred stub dimension row with only the natural key and fill it in later.
Deduplication
- Events: clients send a UUID
event_idand reuse it on retry; keep the first occurrence. If missing, hash anonymous ID, timestamp, page, and payload. - CDC rows: keep the latest row per primary key ordered by update time and log position.
- Payments: dedupe on the idempotency key.
- Add uniqueness tests on every declared grain.
Anonymous and null users
- Sessions are built on
anonymous_id, so logged-out visitors stay in the conversion denominator. - When a user logs in,
bridge_identitylinks the anonymous ID to a buyer and the session is backfilled with that buyer key, optionally for earlier sessions within N days. - Unresolved sessions use
buyer_key = -1instead of NULL, keeping joins inner and counts explicit. - Retention cohorts include only registered buyers.
- Orders always have a buyer. Attach the order's session when known, otherwise the buyer's latest session within 30 minutes.
Slowly changing seller attributes
dim_selleris Type 2. When tier, region, or commission changes, close the current row (valid_to,is_current = false) and insert a new one.- Facts store the
seller_keyvalid at order time, so historical cancellation rates reflect the seller's status then. - For current-state reporting, join through
seller_idto theis_currentrow. - Keep seller-level aggregates keyed on the natural
seller_idso that rates survive attribute changes.
7. Common mistakes
- Averaging precomputed ratios across days instead of summing numerators and denominators.
- Using ingestion time for partitioning, which shifts metrics onto the wrong day.
- Dropping anonymous sessions and inflating conversion.
- Counting cancelled orders in AOV or GMV without stating the rule.
- Joining facts to the current seller row and silently rewriting history.
- Using floating point for money and ignoring currency conversion.