Design real-time top-K products service
AmazonSoftware Engineer · Technical ScreenLast updated
An Amazon software-engineer system-design screen: design a real-time service that ingests a stream of purchase events (customer_id, item_id, timestamp) and continuously serves the top-K most purchased items over multiple rolling windows (e.g. last 5 minutes, last 1 hour, 24 hours, and daily totals). The answer covers ingestion and partitioning, event-time windowing with watermarks, exact min-heap vs. approximate Count-Min-Sketch / Space-Saving top-K, late/duplicate/out-of-order handling, state and TTL, exactly-once fault tolerance, horizontal scaling, serving APIs, and latency-vs-accuracy trade-offs.
Question
Design a real-time service that ingests a continuous stream of purchase events with the schema (customer_id, item_id, timestamp) and continuously computes and serves the top-K most purchased items. The service must support multiple rolling windows at once, for example:
- A short sliding window (e.g. last 5 minutes, or a configurable window
Tsuch as the last 1 hour), and - A long window and daily rolling totals (e.g. last 24 hours, plus per-calendar-day totals with both a running "today" view and finalized prior days).
Work through the full design and address each of the following:
- Data model for the ingested event and for the materialized top-K result records.
- Ingestion pipeline and partitioning — durable log, partition key choice, and hot-key mitigation.
- Windowing and aggregation strategy — sliding windows vs. tumbling micro-buckets with add/subtract, keyed windows, slide cadence, and how daily totals are finalized.
- Algorithms and data structures for maintaining top-K at scale (e.g. count map + min-heap for exact, or approximate heavy-hitters such as Space-Saving / Misra-Gries and Count-Min Sketch + heap), including the two-stage local-then-global merge pattern.
- Event-time processing with watermarks: handling late, out-of-order, and duplicate events; allowed-lateness budgets per window.
- State management and TTL — embedded state store, checkpointing, per-window namespaces, and eviction.
- Fault tolerance and exactly-once vs. at-least-once semantics.
- Horizontal scalability — partitioning, key-splitting for skew, autoscaling.
- Serving and query APIs — how clients fetch the current top-K per window/day, plus optional streaming push.
- Latency vs. accuracy trade-offs and how you would tune them.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.