KatInterview

Streaming Analytics: Building a Live Product Popularity Service

System DesignModel answer
HardModel answerSystem Designstreamingdistributed-systemsanalytics

Last updated

Design a distributed system that processes purchase event streams in real time and continuously exposes the most popular products across multiple time horizons.

You're building a service that processes a continuous feed of purchase transactions and maintains up-to-date rankings of the most frequently purchased products. Each transaction arrives as a record containing customer_id, item_id, and timestamp.

Your system must simultaneously track and serve the most popular K items across different observation periods:

  • Short-term trend windows (such as the past 5 minutes or past hour)
  • Medium-term windows (such as the trailing 24 hours)
  • Calendar-aligned aggregations (today's totals so far, plus completed historical days)

Design the complete architecture and explain your approach to:

  1. Event schema and output format — what fields belong in the incoming events and in the popularity rankings you expose
  2. Ingestion layer design — durable messaging infrastructure, how you partition the stream, and strategies for load balancing when certain products dominate traffic
  3. Time window implementation — techniques for computing overlapping time ranges efficiently, managing bucket granularity, and handling calendar boundaries
  4. Ranking computation — data structures and algorithms for maintaining sorted popularity lists, including both precise and space-efficient approximation methods
  5. Timestamp semantics — dealing with events that arrive delayed, out of sequence, or duplicated; defining when a time window closes
  6. Persistent state strategy — where aggregation state lives, how it's organized, checkpointing, and automatic cleanup
  7. Reliability guarantees — recovery from failures and whether you provide exactly-once or at-least-once processing
  8. Horizontal scaling approach — distributing work across machines, handling imbalanced data, and elasticity
  9. Read path architecture — how clients query current rankings, and whether you support real-time subscriptions
  10. Performance tuning — trade-offs between result freshness, computational cost, and ranking precision

Assume the system handles tens of thousands of transactions per second normally, with spikes reaching hundreds of thousands during promotional events. The product catalog contains tens of millions of items with highly skewed popularity. The ranking depth K is around 100, and read traffic is in the thousands of queries per second with tight latency requirements.

Hints

Hint 1

Think about how to avoid recomputing every overlapping window from scratch—can you incrementally update by adding new data and removing expired data?

Hint 2

A single extremely popular product can create a hotspot. Consider local aggregation before shuffling data across the network.

Hint 3

For very large time windows, storing exact counts for every product may not be feasible. Investigate probabilistic data structures that can estimate heavy hitters with bounded error.