Design a subscription push service
Design a Subscription Push Service
Design an object-oriented publish/subscribe notification service that manages user subscriptions to various topics and pushes notifications to them.
API Endpoints
The service must expose the following APIs:
addSubscription(userId, topicId): Allows a user to subscribe to a specific topic.unsubscribe(userId, topicId): Allows a user to unsubscribe from a specific topic.publishNews(topicId, newsId, payload): Publishes a new item of news to a given topic.newsIdis unique per topic and serves as an idempotency key.onNewsReceived(userId, newsId): A client-side acknowledgement indicating that a user has successfully received a specific news item.
Core Requirements
-
Delivery Semantics:
- Each published news item must be delivered at most once.
- News items should only be delivered to users who are actively subscribed to the topic at the time of delivery.
- An unsubscribe action must immediately prevent any future deliveries for that user-topic pair, including items that were published earlier but not yet delivered.
- For any given user and topic, news items must be delivered in the exact order they were published to that topic (per-topic, per-user ordering).
-
Design Deliverables:
- Provide a clear class and interface design for the service and its constituent components.
- Detail the in-memory and persistence data models required.
- Address concurrency control mechanisms, specifically focusing on maintaining ordering, ensuring idempotency, and handling race conditions.
- Outline strategies for failure handling, including system restarts, partial component failures, and a robust retry policy.
- Present a scalability plan capable of supporting millions of users.
- Analyze the time and space complexity of core operations.
Assumptions
newsIdis unique within the scope of a single topic and can be used as an idempotency key.- The per-topic ordering of news items is defined by a monotonically increasing sequence (offset) assigned at the time of publication.
- If a user unsubscribes from a topic after a news item has been published but before it has been delivered, that specific news item must not be delivered to the unsubscribed user.
- New subscriptions should only receive news items published after their subscription time; there is no requirement for historical replay of past news.
AI interview prep
Core dilemma
Balancing strict delivery guarantees (at-most-once, per-user-per-topic ordering, immediate unsubscribe effect) with high scalability and fault tolerance in a push-based system.
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 subscription push service
Design a Subscription Push Service
Design an object-oriented publish/subscribe notification service that manages user subscriptions to various topics and pushes notifications to them.
API Endpoints
The service must expose the following APIs:
addSubscription(userId, topicId): Allows a user to subscribe to a specific topic.unsubscribe(userId, topicId): Allows a user to unsubscribe from a specific topic.publishNews(topicId, newsId, payload): Publishes a new item of news to a given topic.newsIdis unique per topic and serves as an idempotency key.onNewsReceived(userId, newsId): A client-side acknowledgement indicating that a user has successfully received a specific news item.
Core Requirements
-
Delivery Semantics:
- Each published news item must be delivered at most once.
- News items should only be delivered to users who are actively subscribed to the topic at the time of delivery.
- An unsubscribe action must immediately prevent any future deliveries for that user-topic pair, including items that were published earlier but not yet delivered.
- For any given user and topic, news items must be delivered in the exact order they were published to that topic (per-topic, per-user ordering).
-
Design Deliverables:
- Provide a clear class and interface design for the service and its constituent components.
- Detail the in-memory and persistence data models required.
- Address concurrency control mechanisms, specifically focusing on maintaining ordering, ensuring idempotency, and handling race conditions.
- Outline strategies for failure handling, including system restarts, partial component failures, and a robust retry policy.
- Present a scalability plan capable of supporting millions of users.
- Analyze the time and space complexity of core operations.
Assumptions
newsIdis unique within the scope of a single topic and can be used as an idempotency key.- The per-topic ordering of news items is defined by a monotonically increasing sequence (offset) assigned at the time of publication.
- If a user unsubscribes from a topic after a news item has been published but before it has been delivered, that specific news item must not be delivered to the unsubscribed user.
- New subscriptions should only receive news items published after their subscription time; there is no requirement for historical replay of past news.