Subscription-based notification service

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2025-03-12

Problem Statement

Design a notification service where users can subscribe to specific topics, channels, or events and receive real-time notifications. Focus is on OOP design, modularity, and scalable message delivery.


Functional Requirements


1.Subscription Management
•Users can subscribe or unsubscribe from topics or channels.
•Support multiple topics per user and multiple users per topic.
2.Notification Publishing
•Applications or services can publish notifications to a topic.
•Notifications are delivered to all subscribed users.
3.Delivery Mechanism
•Deliver notifications in real-time (push-based) or batch mode.
•Optionally support different channels: email, SMS, push notifications, webhooks.
4.Optional Features
•Prioritize notifications by importance or type.
•Support persistence of undelivered notifications.
•Retry mechanism for failed deliveries.


Non-Functional Requirements


•Scalability: Handle a large number of subscribers and high-frequency notifications.
•Reliability: Ensure notifications are delivered at least once.
•Extensibility: Easy to add new channels or notification formats.
•Low Latency: Deliver notifications with minimal delay.


Edge Cases & Constraints


•No subscribers for a topic → handle gracefully.
•High load → ensure queueing or throttling of notifications.
•Failed delivery → implement retry/backoff strategy.
•Concurrent subscription/unsubscription → ensure thread safety.


Design Hints


•Key Classes / Interfaces:
•User → contains user info and subscription list.
•Topic → represents a topic or channel with subscriber list.
•Notification → encapsulates content, type, timestamp, and metadata.
•SubscriptionService → manages subscribing/unsubscribing users to topics.
•NotificationService → manages publishing and delivery of notifications.
•DeliveryChannel → interface for different delivery mechanisms (Email, SMS, Push, Webhook).
•Data Structures:
•Map<TopicId, Set<UserId>> → to track subscribers per topic efficiently.
•Queue<Notification> → for asynchronous notification delivery.
•PriorityQueue (Optional) → for prioritizing notifications.
•Design Patterns:
•Observer Pattern → notify subscribers when a topic receives a new notification.
•Strategy Pattern → pluggable delivery channels (Email, SMS, Push).
•Singleton (Optional) → centralized service manager instance.


Example Flow


1.User subscribes to topic "Sports".
2.Application publishes Notification("Match starts at 7 PM") to "Sports" topic.
3.NotificationService fetches all subscribers of "Sports".
4.Notifications are pushed asynchronously via each user’s preferred DeliveryChannel.
5.User receives notification in real-time.


Evaluation Areas


•Clean class and interface design for subscriptions and notifications.
•Efficient subscriber management and notification delivery.
•Extensibility to support multiple channels and prioritization.
•Thread safety and handling concurrent subscriptions/unsubscriptions.


Solution

Comments