Object-Oriented Design: Publish/Subscribe Notification Service
Design an object-oriented publish/subscribe notification service for user–topic subscriptions.
Provide the following APIs:
-
addSubscription(userId, topicId)
-
unsubscribe(userId, topicId)
-
publishNews(topicId, newsId, payload)
-
onNewsReceived(userId, newsId) // client acknowledgement
Requirements:
-
Delivery semantics
-
Each published item is delivered at most once to users who are subscribed at the time of delivery.
-
Unsubscribes prevent any future deliveries for that user–topic pair, including items published earlier but not yet delivered.
-
Per-topic per-user ordering: for a given user and topic, deliver items in the order they were published to that topic.
-
Design deliverables
-
Class/interface design for the service and its components.
-
In-memory and persistence data models.
-
Concurrency control (ordering, idempotency, race handling).
-
Failure handling (restarts, partial failures, retries policy).
-
Scalability plan to millions of users.
-
Time and space complexity analysis of core operations.
Assume:
-
newsId is unique per topic (idempotency key).
-
Per-topic ordering is defined by a monotonically increasing sequence (offset) assigned at publish time.
-
If a user unsubscribes after a publish but before delivery, do not deliver that item.
-
New subscriptions start from the current end of the topic (no historical replay).