Design an Alert/Notification System

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

Problem Statement

Design a scalable, reliable, and configurable Notification System that can send alerts or notifications to users through multiple channels such as Email, SMS, Push Notifications, Slack, or Webhooks.


The system should support high throughput, low latency delivery, configurable templates, user preferences, and retry mechanisms for failed notifications.


🎯 Core Objective


To build a centralized Notification Platform that:


•Receives events (e.g., “Order Placed”, “Payment Failed”, “System Down”).
•Determines the recipients and channels.
•Formats the message content dynamically.
•Sends notifications through appropriate delivery channels.
•Tracks status, failures, and retries.


✅ Functional Requirements


Event Ingestion


•Receive events from other services (e.g., Order Service, Monitoring System).
•Support both synchronous API calls and asynchronous message ingestion (Kafka, RabbitMQ, etc.).


Notification Rules


•Define rules mapping event types to notification channels.
•Example:
•ORDER_PLACED → Email + Push
•SERVER_DOWN → SMS + Slack


User Preferences


•Users can choose preferred channels (Email, SMS, Push, etc.).
•Option to opt out from specific notification categories.


Template Management


•Store and manage notification templates.
•Support variables and placeholders (e.g., {{user_name}}, {{order_id}}).


Notification Delivery


•Send notifications via multiple providers:

Email: SMTP, SendGrid

SMS: Twilio

Push: Firebase, APNs

Webhooks/Slack: HTTP POST

•Ensure reliable delivery with retry on transient failures.

Failure Handling & Retries

•Implement exponential backoff for failed notifications.
•Log and alert on repeated failures.

Tracking & Reporting


•Track delivery status: PENDING → SENT → DELIVERED → FAILED.
•Maintain audit logs for each notification.
•Expose metrics (e.g., success rate, latency, retries).


Admin Console (Optional)


•View notification logs, retry failed ones manually.
•Configure new templates, rules, or channels dynamically.


⚙️ Non-Functional Requirements


Attribute with Goal


Scalability

Handle 1M+ notifications/day


Latency

<200 ms for high-priority alerts


Reliability

At least once delivery guarantee


Durability

Store event and delivery logs safely


Availability

99.99% uptime


Extensibility

Easily add new channels or templates


Security

Encrypt user data and credentials


🧩 Core Components

Component with Description


API Gateway

Entry point for event producers and admin clients


Event Processor

Processes incoming events and routes them


Notification Orchestrator

Determines recipients, templates, and channels


Channel Workers

Handle delivery via Email, SMS, Push, Webhooks


Template Service

Renders messages using templates and variables


User Preference Service

Manages user-specific notification settings


Retry & Queue System

Manages failed notifications with exponential backoff


Storage Layer

Stores events, templates, user preferences, and logs


Monitoring & Analytics

Tracks metrics like latency, delivery rate, and failures


🧱 High-Level Architecture

1 2 +-----------------------+ 3 | Event Producer | 4 | (App / Service) | 5 +-----------+-------------+ 6 | 7 (HTTP / Kafka) 8 | 9 +-----------v-------------+ 10 | API Gateway | 11 +-----------+-------------+ 12 | 13 +-----------v-------------+ 14 | Event Processor Svc | 15 +-----------+-------------+ 16 | 17 +-----------v-------------+ 18 | Notification Orchestrator| 19 +-----------+-------------+ 20 / | \ 21 / | \ 22 +-----------v+ +-----v-----+ +-----v------+ 23 | Template Svc| | Pref Svc | | Retry Queue | 24 +-----+-------+ +-----------+ +-------------+ 25 | | 26 +---------v--------+ +--------------v-------------+ 27 | Channel Workers |--> Email, SMS, Push, Slack, Webhook | 28 +---------+--------+ +--------------+-------------+ 29 | | 30 +-------------v-----------------+ 31 | 32 +-------+--------+ 33 | Storage Layer | 34 | (DB + Logs) | 35 +----------------+ 36 | 37 +-------v--------+ 38 | Monitoring & | 39 | Admin Console | 40 +----------------+


🧠 Detailed Flow


1. Event Generation


•An upstream service (e.g., Order Service) emits an event:
1 2{ 3 "event_type": "ORDER_PLACED", 4 "user_id": "U123", 5 "metadata": { 6 "order_id": "O456", 7 "amount": "499.00" 8 } 9}
•Sent via HTTP POST or Kafka topic to the Notification System.


2. Event Processing


•Event Processor validates the event and pushes it to a processing queue.
•The Notification Orchestrator:
•Fetches applicable rules for this event.
•Fetches user preferences.
•Fetches relevant templates.


3. Message Rendering


•Template Service fills in dynamic placeholders:
1 2Hello {{user_name}}, your order {{order_id}} has been placed successfully!
•→
1 2Hello Sahil, your order O456 has been placed successfully!


4. Channel Dispatch


•Orchestrator sends rendered messages to channel workers.
•Channel Workers:
•Use third-party APIs (Twilio, SendGrid, Firebase) for actual delivery.
•Receive response (success/failure).


5. Retry and Logging


•If delivery fails:
•Push notification to a Retry Queue (e.g., Kafka / Redis Streams).
•Retry using exponential backoff.
•Log attempts in Notification Log DB.


6. Monitoring & Analytics


•Admins can:
•View dashboard of sent/failed notifications.
•Filter by channel or event type.
•Trigger manual retries.


🧱 Data Model (Simplified)


NotificationEvent

1 2{ 3 "event_id": "EVT12345", 4 "event_type": "ORDER_PLACED", 5 "user_id": "U123", 6 "payload": { "order_id": "O456", "amount": 499.0 }, 7 "status": "PENDING" 8}


Template

1 2{ 3 "template_id": "TMP01", 4 "event_type": "ORDER_PLACED", 5 "channel": "EMAIL", 6 "body": "Hello {{user_name}}, your order {{order_id}} is confirmed!" 7}


UserPreference

1 2{ 3 "user_id": "U123", 4 "channels": ["EMAIL", "PUSH"], 5 "opt_out_events": ["PROMOTIONS"] 6}


⚡ Scalability & Reliability Considerations


| Area | Approach |

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

| **Event Load** | Use Kafka/RabbitMQ for async processing |

| **Delivery Workers** | Auto-scale based on queue depth |

| **Template Rendering** | Cache frequently used templates in Redis |

| **Failure Handling** | Retry Queue + Dead Letter Queue (DLQ) |

| **Storage** | Use Postgres for config, MongoDB for logs |

| **Monitoring** | Prometheus + Grafana + Alerts |

| **Geo Redundancy** | Multi-region deployment for failover |


🔒 Security & Privacy

•Encrypt PII (user data, phone numbers, emails).
•Use API keys and OAuth for third-party providers.
•Role-based access for admins.
•Audit logs for compliance (GDPR-ready).


🧩 Tech Stack (Example)

| Layer | Tech Options |

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

| **Frontend (Admin)** | React / Angular |

| **Backend** | Java Spring Boot / Go / Node.js |

| **Queue** | Kafka / RabbitMQ / AWS SQS |

| **DB** | PostgreSQL + MongoDB + Redis |

| **Email Provider** | SendGrid / SES |

| **SMS Provider** | Twilio |

| **Push** | Firebase / APNs |

| **Monitoring** | Prometheus / Grafana |

| **Deployment** | Docker + Kubernetes |


🚀 Key Challenges


| Challenge | Approach |

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

| Multi-channel reliability | Use channel prioritization + fallback mechanisms |

| Delivery at scale | Asynchronous queue-based worker pool |

| Template flexibility | Parameterized dynamic templates |

| User preference sync | Cache user settings in Redis |

| Failures and retries | Exponential backoff + DLQ |

| Ordering | Use unique event IDs and idempotent delivery |



✅ Goal:

Design a highly reliable, scalable, and extensible notification system that supports multi-channel delivery, configurable templates, user preferences, and robust retry mechanisms, suitable for enterprise-scale alerting or customer engagement.

Solution

Comments