KatInterview

Design a Real-Time Content Feed Delivery System

System DesignModel answer
HardModel answerSystem Designsystem-designdata-structuresrate-limiting

Last updated

Design an in-memory topic subscription platform that handles fast ingestion, interest filtering, sliding-window rate limiting, de-duplication, and sorted feed distribution.

You are tasked with designing the core in-memory engine for a real-time topic-based content aggregation service. The platform ingests items continuously and publishes customized feeds to subscribers based on topic interests, quality filters, and delivery speed caps.

Implement a class ContentStreamManager supporting the following API:

API Requirements

  1. add_subscription(subscriber_id: int, min_score: float, max_items_per_sec: int, topics: list[str]) -> bool

    • Creates or updates a subscriber's delivery policy.
    • Returns True if inputs are valid (max_items_per_sec >= 1, non-empty topics, valid numeric min_score).
    • Returns False on any invalid argument or constraint violation.
    • Note: Updating an existing subscription must preserve previously delivered item histories and rate-limiting window state.
  2. remove_subscription(subscriber_id: int) -> bool

    • Removes the subscriber's configuration, window state, and delivery history.
    • Returns True if found and deleted, or False if subscriber_id does not exist.
  3. ingest_item(item_id: int, timestamp: float, score: float, topics: list[str]) -> bool

    • Ingests a new content item into the engine.
    • Returns False if item_id has already been ingested or if topics is empty.
    • Returns True upon successful registration.
  4. dispatch(current_time: float, max_age: float) -> dict[int, list[int]]

    • Generates and returns a dictionary mapping every registered subscriber_id to a list of eligible item_ids to deliver at current_time.

Dispatch Rules & Order Constraints

For a given subscriber, an item is eligible for delivery if and only if:

  • Score: item.score >= subscriber.min_score.
  • Topic Match: At least one topic in item.topics exists in subscriber.topics.
  • Freshness Window: current_time - max_age <= item.timestamp <= current_time.
  • De-duplication: The item_id has never been delivered to this subscriber in any past or current dispatch call.
  • Rate Limit: The number of items delivered to the subscriber within the last rolling 1-second window (current_time - 1.0, current_time] (including current deliveries) cannot exceed max_items_per_sec.

Ordering: Eligible items delivered to a subscriber in a single dispatch call must be sorted by:

  1. Higher score first (score descending).
  2. If scores tie, older timestamp first (timestamp ascending).
  3. If scores and timestamps tie, smaller item_id first (item_id ascending).

Deliverables

  1. In-Memory Architecture: Detail the exact data structures chosen for fast lookup, sorting, duplicate detection, and sliding-window rate tracking.
  2. Algorithm & Pseudo-code: Detail the implementation of dispatch and management operations.
  3. Complexity Analysis: Provide time and space complexity for every operation in terms of total items $N$, total subscribers $S$, fresh items $F$, and topics per item/subscriber $T$.
  4. Distributed System & Scale-Out Strategy: Explain how to scale this architecture to handle millions of subscribers, high-throughput item ingestion, stream partitioning, distributed rate limiting, and fault tolerance.

Hints

Hint 1

Because all subscribers demand the exact same total order (score desc, timestamp asc, item_id asc), you can compute a single global sort of the fresh candidate items during dispatch instead of sorting per subscriber.

Hint 2

Using a sliding window algorithm with a deque per subscriber allows O(1) eviction of timestamps older than current_time - 1.0.

Hint 3

To clean up memory over long-running processes, prune delivered item IDs and cached items that fall outside the maximum allowed window max_age.