Design a Notification Service (Supports SMS, WhatsApp, Push Notification)

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2025-05-28

Problem Statement

Design and implement a Notification Service that can send messages via SMS, WhatsApp, and Push Notifications. The focus is on object-oriented design, modularity, and extensibility, allowing the system to easily support new notification channels in the future.


✅ Functional Requirements


1.Message Sending
•Support sending messages via:
•SMS
•WhatsApp
•Push Notification
•API:
1 2sendNotification(User user, String message, Channel channel)
1.User Management
•Each user can have multiple contact methods: phone number, WhatsApp ID, device token.
•The system should select the correct channel based on input.
2.Queueing & Retry
•Messages should be queued for delivery.
•Failed messages should be retried after a configurable interval.
3.Extensibility
•Easy to add new channels (e.g., Email, Slack).
•Ability to broadcast to multiple channels simultaneously.


🔐 Constraints & Assumptions


•Focus on LLD in-memory design; persistence or distributed delivery is optional.
•Assume external services for sending SMS, WhatsApp, and Push are available.
•Messages may be sent asynchronously to avoid blocking the main thread.
•Thread-safety should be considered if multiple messages are sent concurrently.


🧱 Key Components & Classes


1.NotificationService (Facade)
1 2class NotificationService { 3public: 4 void sendNotification(User user, String message, Channel channel); 5};
1.Channel Interface
1 2class INotificationChannel { 3public: 4 virtual bool send(User user, String message) = 0; 5};
1.Concrete Channels
1 2class SMSChannel : public INotificationChannel { ... }; 3class WhatsAppChannel : public INotificationChannel { ... }; 4class PushNotificationChannel : public INotificationChannel { ... };
1.User Class
1 2class User { 3private: 4 string phoneNumber; 5 string whatsappId; 6 string deviceToken; 7public: 8 // getters for each contact method 9};
1.Queue / Scheduler (Optional)
•Maintain a queue of pending notifications.
•Retry failed deliveries after configurable interval.


🔄 Example Workflow


1.User A wants to receive an SMS → NotificationService.sendNotification(userA, "Hello", SMS).
2.Service selects SMSChannel → attempts delivery → success/failure logged.
3.If delivery fails → queued → retried after interval.
4.Extending for multiple channels: NotificationService.sendNotification(userA, "Hello", [SMS, Push]).


🧪 Expected Deliverables


•Class diagram showing NotificationService, INotificationChannel, concrete channels, and User.
•Implementation of core methods: sendNotification, send for each channel.
•Test cases:
•Single-channel notification (SMS, WhatsApp, Push)
•Multi-channel broadcast
•Failed delivery retry simulation


💡 Discussion Points


•Trade-offs between synchronous vs asynchronous sending.
•Extensibility for new channels.
•Retry strategy (fixed interval vs exponential backoff).
•Scalability considerations if the service needs to handle millions of users.


Solution

Comments