Design a Top-K trending service
Design a Top-K Trending Service
Scenario
Building upon your understanding of algorithms for computing "top K" (e.g., most frequent items), design a scalable backend service that provides similar functionality at a distributed scale.
Requirements
Design a service that continuously computes and serves the top K most frequent items (e.g., hashtags, products, search queries).
Functional Requirements
- Event Ingestion: Ingest a high-volume stream of events, where each event includes at least
(item_id, timestamp). - Query API: Support an API endpoint,
GET /topk?k=K&window=..., which returns the top-K items by frequency. - Time Window Support: The service must support querying for top-K items based on a specified time window. At a minimum, this includes:
- All-time counts, or
- A sliding window (e.g., last 5 minutes / last 1 hour). You must state your chosen approach and any associated assumptions for implementing the sliding window.
Non-Functional Requirements
- Low Latency Queries: Achieve low latency for query responses (e.g., p95 < 100 ms) at high Queries Per Second (QPS).
- High Write Throughput: The system must handle a high throughput of incoming event data.
- Horizontal Scalability: The service should be designed to scale horizontally by adding more machines.
- Fault Tolerance & Correctness: Ensure fault tolerance and provide reasonable correctness guarantees. Clearly state the consistency model your design adheres to.
Clarifications to Address
During your design, you should explicitly address or state assumptions for the following:
- The expected event rate (events per second), the total number of distinct items, and the anticipated query QPS.
- Whether exact results are strictly required, or if approximate results are acceptable for the top-K computation.
AI interview prep
Core dilemma
Balancing real-time, high-volume data ingestion and processing with low-latency, aggregated query responses, particularly challenging with dynamic time windows and potential exactness requirements, all while ensuring horizontal scalability and fault tolerance.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Design a Top-K trending service
Design a Top-K Trending Service
Scenario
Building upon your understanding of algorithms for computing "top K" (e.g., most frequent items), design a scalable backend service that provides similar functionality at a distributed scale.
Requirements
Design a service that continuously computes and serves the top K most frequent items (e.g., hashtags, products, search queries).
Functional Requirements
- Event Ingestion: Ingest a high-volume stream of events, where each event includes at least
(item_id, timestamp). - Query API: Support an API endpoint,
GET /topk?k=K&window=..., which returns the top-K items by frequency. - Time Window Support: The service must support querying for top-K items based on a specified time window. At a minimum, this includes:
- All-time counts, or
- A sliding window (e.g., last 5 minutes / last 1 hour). You must state your chosen approach and any associated assumptions for implementing the sliding window.
Non-Functional Requirements
- Low Latency Queries: Achieve low latency for query responses (e.g., p95 < 100 ms) at high Queries Per Second (QPS).
- High Write Throughput: The system must handle a high throughput of incoming event data.
- Horizontal Scalability: The service should be designed to scale horizontally by adding more machines.
- Fault Tolerance & Correctness: Ensure fault tolerance and provide reasonable correctness guarantees. Clearly state the consistency model your design adheres to.
Clarifications to Address
During your design, you should explicitly address or state assumptions for the following:
- The expected event rate (events per second), the total number of distinct items, and the anticipated query QPS.
- Whether exact results are strictly required, or if approximate results are acceptable for the top-K computation.