Design a Notification System

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2025-02-11

Problem Statement

Design the Low-Level Design (LLD) for a Notification System that can send notifications to users through multiple channels — such as Email, SMS, Push, and In-App notifications.


The system should be extensible, configurable, and capable of handling different message templates, priorities, and delivery mechanisms.


🎯 Core Requirements


1. Notification Creation


•A service or component should be able to trigger a notification event.
•Each notification contains:
•title, message, recipient(s), priority, and type (Email, SMS, Push, etc.).
•System should support templated notifications (e.g., "Welcome {{name}}!").


2. Notification Delivery


•Based on notification type, the system should delegate delivery to the corresponding channel handler:
•Email → uses EmailService
•SMS → uses SMSService
•Push → uses PushService
•In-App → uses InAppService
•Must handle failures (e.g., retry failed deliveries up to N times).


3. Multi-Channel Support


•A notification can be sent via multiple channels simultaneously.
•For example:
•PaymentSuccess → send Email + Push.
•PasswordReset → send Email only.


4. Scheduling (Optional Extension)


•Allow scheduling notifications for future delivery (e.g., birthday greetings).


5. Notification Preferences


•Each user can configure preferred channels (e.g., disable SMS, enable Email).
•The system must respect these preferences.


6. Status Tracking


•Maintain delivery status per notification:
•PENDING, SENT, FAILED, DELIVERED, READ.


⚙️ Key Operations (APIs)


APIDescription


sendNotification(Notification notification)


Sends a notification immediately.


scheduleNotification(Notification notification, Date time)


Schedules for future.


updatePreferences(User user, Preferences prefs)


Updates user notification preferences.


getNotificationStatus(String notificationId)


Retrieves the delivery status.


🧱 Suggested Class Design


ClassResponsibility


Notification


Represents the core notification entity (title, message, type, recipient, metadata).


NotificationService


Entry point for sending notifications. Delegates to correct channel(s).


NotificationChannel (Interface)


Abstract interface implemented by all channel types (Email, SMS, Push).


EmailNotificationChannel, SMSNotificationChannel, PushNotificationChannel


Implementations of notification channels.


NotificationScheduler


Handles delayed notifications using a queue or timer.


NotificationRepository


Stores sent notifications and their statuses.


UserPreferences


Stores user-specific channel preferences.


RetryHandler


Handles retries for failed sends.


🧠 Design Considerations


•Extensibility: Add new channels easily (e.g., WhatsApp, Slack).
•Reliability: Retry mechanism on failure.
•Scalability: Use async queues (e.g., Kafka, SQS) for high load.
•Template System: Replace placeholders dynamically (e.g., {{user_name}}).
•Persistence: Store notifications for audit/history.


🚀 Possible Extensions


•Integrate with external APIs (Twilio for SMS, SES for Email, Firebase for Push).
•Priority-based delivery (high → instant, low → batched).
•Event-driven architecture (Publisher–Subscriber model).
•Support bulk notifications (campaigns).


✅ Goal:


Design a modular, extensible, and maintainable notification system that supports multiple channels, user preferences, scheduling, and reliable delivery.

Solution

Comments