Design a Scalable Notification System using Redis & Spark
Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2024-09-19
Problem Statement
Designing a scalable, fault-tolerant notification system that can send millions of notifications (push, email, SMS, in-app) to users in real-time or batch mode. The system should leverage Redis for fast queuing/caching and Apache Spark for large-scale data processing and personalization.
✅ Functional Requirements:
1.<span></span>Multi-Channel Notifications
2.<span></span>Support push notifications (mobile/web), email, SMS, and in-app messages.
3.<span></span>Support user preferences (e.g., email only, no SMS at night).
4.<span></span>Notification Triggers
5.<span></span>Real-time triggers (e.g., “order placed”, “payment successful”).
6.<span></span>Scheduled/batch campaigns (e.g., daily offers, reminders).
7.<span></span>Delivery Pipeline
8.<span></span>Queue notifications for reliable delivery.
9.<span></span>Deduplicate messages (avoid sending multiple alerts for same event).
10.<span></span>Retry on failure (e.g., SMS gateway downtime).
11.<span></span>Personalization
12.<span></span>Tailor messages using user history, preferences, and behavior.
13.<span></span>Support A/B testing for campaigns.
14.<span></span>Analytics & Monitoring
15.<span></span>Track delivery, open rates, click-through rates.
16.<span></span>Dashboards for monitoring failures, retries, latency.
⚙️ Non-Functional Requirements:
1.<span></span>Scalability: Handle millions of notifications per hour.
2.<span></span>Low Latency: Real-time notifications must be delivered in <1s.
3.<span></span>Fault Tolerance: Ensure reliable delivery even if downstream gateways fail.
4.<span></span>Durability: Notifications should not be lost (use Redis persistence).
5.<span></span>Cost Efficiency: Optimize infra for both burst traffic and daily campaigns.
🔐 Constraints:
1.<span></span>Peak load: 5M notifications/hour.
2.<span></span>10M+ active users with varying notification preferences.
3.<span></span>Redis should not become a bottleneck (must support clustering/sharding).
4.<span></span>Spark jobs must handle batch personalization & campaign distribution efficiently.
🧱 Key Components to Design:
1.<span></span>Event Producer → generates events (e.g., order placed).
2.<span></span>Notification Orchestrator → decides notification type & channel.
3.<span></span>Redis Queue →
4.<span></span>Stores pending notifications.
5.<span></span>Supports Pub/Sub for real-time pushes.
6.<span></span>Acts as a buffer for downstream delivery services.
7.<span></span>Spark Engine →
8.<span></span>Processes large datasets for campaigns & personalization.
9.<span></span>Runs batch jobs (nightly offers, behavioral analysis).
10.<span></span>Channel Delivery Services →
11.<span></span>Push Service (FCM/APNs).
12.<span></span>Email Service (SMTP, SendGrid).
13.<span></span>SMS Service (Twilio, Karix, etc.).
14.<span></span>In-app messaging.
15.<span></span>Retry & DLQ (Dead Letter Queue) → Redis lists for failed messages.
16.<span></span>User Preferences Store → filters channels before sending.
17.<span></span>Monitoring & Analytics Layer → logs events, stores metrics, powers dashboards.
🧪 Example Scenario:
<div><div><br /></div><div>1. User places an order → Event Producer pushes "Order Confirmed" event.</div><div>2. Notification Orchestrator checks preferences → decides to send push + email.</div><div>3. Notification is enqueued in Redis → delivery workers consume it.</div><div>4. Push notification sent via FCM → success logged.</div><div>5. Email sent via SMTP → fails → retried from Redis → succeeds on 2nd attempt.</div><div>6. Spark batch job runs at night → selects users for a "Diwali Offer" campaign.</div><div>7. Spark job pushes campaign messages into Redis queue → delivery workers send them.</div><div>8. Monitoring system tracks delivery rate and shows failures in dashboard.</div></div>