KatInterview

Build a Clickstream and Orders Warehouse for an Online Shop

System DesignModel answer
HardModel answerSystem Designdata-warehouseevent-schemaidentity-stitching

Last updated

Design a warehouse for a retail platform's clickstream and orders that serves two fixed queries: monthly unique Asian viewers of an item, and the current top three orders by value.

You are building the analytics layer for an online retail platform. The product exposes these user-facing actions: searching for items, viewing an item page, logging in, adding to cart, viewing the cart, checking out, and viewing a past order.

The warehouse has to answer two pre-defined questions:

  1. During the current calendar month, how many distinct users located in Asia viewed a particular item, A?
  2. At this moment, which three orders have the largest total amount?

Describe your design. A strong answer covers:

  • Event schema: fields, data types, primary and foreign keys.
  • Sessions: how a session or tracking ID is created and renewed.
  • Identity stitching: linking anonymous activity to a user after login.
  • Objects and quantities: how to represent cart contents and order totals.
  • Storage format: semi-structured JSON versus normalized columns, and the trade-offs.
  • Physical layout: partitioning and indexing or clustering.
  • Correctness: deduplication, idempotent writes, and late or out-of-order events.
  • Dimensional model: users, items, and geography, including slowly changing dimensions.
  • Pipeline: batch versus streaming, scaling, and cost.
  • Optional: example SQL for the two questions.

State your assumptions (time zone, currency, what counts as a valid order, how anonymous traffic is counted) and sketch rough volumes.

Hints

Hint 1

Pin down ambiguous terms first: 'in Asia' (location at event time vs. profile country), which time zone defines the month, whether anonymous visitors count as users, and whether cancelled or refunded orders can appear in the top three.

Hint 2

Treat clickstream and money differently: behavioral events arrive from client SDKs and need dedup and sessionization, while order totals should come from the order database (via change data capture) as the source of truth, stored as decimals.

Hint 3

Keep identity stitching in a separate mapping table instead of rewriting facts, and partition the event table by event date with clustering on event type and item so Query 1 touches only about one month of one event type.

Hint 4

Real-time top-K is awkward in a columnar warehouse; consider a small serving store or streaming aggregate for Query 2, and use the warehouse for the batch/historical view.