Design a Notification Service

Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2025-05-29

Problem Statement

Design a scalable, fault-tolerant Notification Service that can send notifications to users across multiple channels such as Email, SMS, Push notifications, and In-App alerts.


The system should allow other services or applications (e.g., e-commerce, banking, social media) to trigger notifications through an API and should handle high throughput, retries,

scheduling, and user preferences efficiently.


🎯 Core Requirements


Functional Requirements


Notification Triggers

•Other systems can trigger notifications via APIs or message queues.
•Support both event-driven and scheduled notifications.

Multi-Channel Support

•Send messages via:
•Email (e.g., order confirmation)
•SMS (e.g., OTP, alerts)
•Push notifications (e.g., mobile app)
•In-App messages or Web alerts
•Extendable design to add new channels (e.g., WhatsApp, Slack).

User Preferences

•Each user can configure:
•Which channels to receive notifications on.
•Notification types to subscribe/unsubscribe.
•Respect “Do Not Disturb” and scheduling preferences.

Retries & Failures

•Retry failed deliveries with exponential backoff.
•Log failures and allow manual or automatic reprocessing.

Delivery Tracking

•Track delivery status: Pending, Sent, Failed, Acknowledged.
•Support analytics and reporting.

Template Management

•Dynamic message templates for different use-cases.
•Allow placeholders like {{user_name}}, {{order_id}}.

Scalability

•Should handle millions of notifications per day.
•Should scale horizontally across microservices.

Extensibility

•Add new notification channels without affecting existing ones.


⚙️ Non-Functional Requirements


AttributeRequirement


Availability


99.9%


Latency


< 2 seconds per notification


Durability


Guaranteed delivery (eventual)


Scalability


Handle millions of events/day


Security


Encrypt sensitive data (email, phone), OAuth for APIs


Compliance


GDPR, DND, and audit logging


🧩 Core Components


Component with Description


Notification Producer (Trigger)

Other systems (Order Service, Payment Service) send notification events to the service via REST or message queues.


Notification Orchestrator

Central controller that receives requests, validates, enriches data, and routes them to the correct channel.


Message Queue (Kafka/SQS)

Decouples producers and consumers; ensures durability and fault-tolerance.


Channel Services

Dedicated microservices for Email, SMS, Push, etc. Handle integration with 3rd-party providers.


Template Service

Stores and renders message templates dynamically.


User Preference Service

Stores user preferences and DND settings.


Notification Store

Tracks delivery status, retries, and logs.


Scheduler / Retry Manager

Handles delayed or scheduled notifications.


API Gateway

Entry point for external services to trigger notifications.


🧱 High-Level Architecture

1 2 +------------------------+ 3 | External Service | 4 | (e.g., Order System) | 5 +----------+-------------+ 6 | 7 [1] API / Event 8 | 9 +-----------v------------+ 10 | Notification Gateway | 11 +-----------+------------+ 12 | 13 [2] Validation & Enrichment 14 | 15 +-----------v------------+ 16 | Notification Orchestrator | 17 +-----------+------------+ 18 | 19 [3] Publish to MQ 20 | 21 +--------------------------------------+ 22 | Message Queue | 23 | (Kafka / SQS / PubSub) | 24 +----+---------------+-----------------+ 25 | | 26 +--------v---+ +------v--------+ 27 | Email Svc | | SMS Service | 28 +--------+---+ +------^--------+ 29 | | 30 | | 31 +-----------v----+ +-------v----------+ 32 | Push Service | | In-App Service | 33 +----------------+ +------------------+ 34 35 | 36 v 37 +-------------------+ 38 | Notification DB | 39 | (Status, Logs) | 40 +-------------------+ 41 | 42 v 43 +-------------------+ 44 | Analytics Service | 45 +-------------------+


🗄️ Data Model (Simplified)


Notification

1 2{ 3 "id": "uuid", 4 "user_id": "u123", 5 "type": "ORDER_CONFIRMATION", 6 "channels": ["EMAIL", "SMS"], 7 "status": "SENT", 8 "created_at": "timestamp", 9 "updated_at": "timestamp" 10}


Template

1 2{ 3 "template_id": "t001", 4 "type": "ORDER_CONFIRMATION", 5 "subject": "Your order {{order_id}} is confirmed!", 6 "body": "Hi {{user_name}}, your order has been confirmed." 7}


UserPreference

1 2{ 3 "user_id": "u123", 4 "email_enabled": true, 5 "sms_enabled": false, 6 "dnd_start": "22:00", 7 "dnd_end": "07:00" 8}


⚡ Key Design Decisions


1. Decoupling


•Use message queues (Kafka, SQS) between the orchestrator and channel services for scalability.
•Ensures asynchronous, reliable delivery.


2. Extensibility


•Add new channels by implementing a common interface (send(Notification)).
•No change required in orchestrator logic.


3. Scalability


•Deploy each channel service independently with auto-scaling.
•Use load balancers for horizontal scaling.


4. Retries & Failures


•Retry failed notifications via Retry Queue or Dead Letter Queue (DLQ).
•Backoff and alerting system for repeated failures.


5. Template Rendering


•Templates stored in DB or cache.
•Rendered dynamically with context data before sending.


6. User Preferences


•Filter out muted channels or “Do Not Disturb” hours before enqueueing.


📈 Sample Flow


Trigger:

•Order service emits an event: OrderPlaced(user_id=123, order_id=456)

Orchestrator:

•Looks up user preferences and template for ORDER_CONFIRMATION

Enqueue:

•Pushes a message to Kafka topic: notifications.email

Channel Service:

•Consumes message, renders template, and calls Email Provider API

Status Update:

•Updates notification status in DB

Retry (if failed):

•Retry Manager picks it from DLQ and resends


💾 Storage Design


| Data Type | Storage Option | Reason |

| ----------------- | --------------------- | ---------------------------- |

| Notification Logs | PostgreSQL / DynamoDB | Consistent writes, queryable |

| Templates | Redis + S3 | Fast access + durability |

| User Preferences | PostgreSQL / Redis | Low latency reads |

| Message Queue | Kafka / SQS | Decoupled async processing |

| Metrics | Prometheus / ELK | Observability |



🧠 APIs


| Method | Endpoint | Description |

| ------ | --------------------------- | -------------------------- |

| `POST` | `/notify` | Trigger a new notification |

| `GET` | `/notifications/{user_id}` | Get user notifications |

| `GET` | `/status/{notification_id}` | Fetch delivery status |

| `POST` | `/preferences` | Update user preferences |

| `POST` | `/template` | Create/update a template |


🚀 Scalability & Optimization


•Use batch sending for similar notifications.
•Cache templates and preferences in Redis.
•Use asynchronous sends and parallelism per channel.
•Integrate multiple third-party providers for redundancy.
•Add rate limiting to prevent abuse.


✅ Goal:


Build a modular, reliable, and scalable notification platform that delivers messages across channels, supports retries, respects user preferences, and can be easily extended to handle new types of notifications or delivery mechanisms.

Solution

Comments