Back to explorer
Infrastructure & Messaging• 40 Min

Design a Distributed Notification System

MEDIUM

Design a Distributed Notification System

Modern consumer and enterprise products deliver billions of notifications daily across Apple Push Notification service (APNs), Firebase Cloud Messaging (FCM), SMS aggregators (Twilio), and Transactional Email (SendGrid / AWS SES).


1. System Requirements

Functional Requirements

  1. Multi-Channel Dispatch: Support iOS/Android Push Notifications, SMS, Email, and in-app bell notifications.
  2. Templating Engine: Dynamically render notifications with user-specific localization, names, and promotional copy.
  3. User Notification Preferences: Respect granular user opt-in/opt-out preferences (e.g., disable marketing emails while keeping security SMS enabled).
  4. Rate Limiting & Anti-Spam: Throttle non-critical alerts to prevent overwhelming users (e.g. max 3 marketing pushes per user per day).
  5. Priority Scheduling: Urgent notifications (Two-Factor OTP codes, fraud alerts) bypass marketing queues and deliver in $< 3\text{ seconds}$.

Non-Functional Requirements

  1. High Throughput: Capable of processing 1 Billion notifications/day with peak surges of 100,000 notifications/sec.
  2. Resilience: Zero lost mission-critical alerts; automatic failover between third-party SMS/Email vendors.
  3. Observability: Real-time delivery status tracking (sent, delivered, opened, bounced).

2. High-Level Architecture Blueprint

code
                                  +-----------------------+
                                  |  Microservices Engine |
                                  |  (Order, Auth, Fraud) |
                                  +-----------+-----------+
                                              |
                                              | POST /api/v1/notifications
                                              v
                                  +-----------------------+
                                  |  Notification Gateway |
                                  |  (Auth, Validation)   |
                                  +-----------+-----------+
                                              |
                                              v
                                  +-----------------------+
                                  |  Deduplication Filter |
                                  |  & User Opt-Out Check |
                                  +-----+-----------+-----+
                                        |           |
             +--------------------------+           +--------------------------+
             | High Priority (OTP)                  | Normal / Bulk Marketing
             v                                      v
+------------------------+             +------------------------+
|  Kafka Priority Queue  |             |  Kafka Standard Queue  |
+-----------+------------+             +-----------+------------+
            |                                      |
            +-------------------+------------------+
                                |
                                v
                    +-----------------------+
                    | Worker Dispatch Pools |
                    +-----+-----+-----+-----+
                          |     |     |     |
            +-------------+     |     |     +-------------+
            |                   |     |                   |
            v                   v     v                   v
     +------------+      +------------+      +------------+      +------------+
     | Apple APNs |      | Google FCM |      | Twilio SMS |      | AWS SES    |
     +------------+      +------------+      +------------+      +------------+

3. Deep Dives & Edge Case Handling

Deep Dive 1: Deduplication via In-Memory Hashes

To prevent accidental loops where a buggy payment service sends 50 duplicate SMS receipts:

  • Hash: SHA256(user_id + channel + event_type + timestamp_minute).
  • The gateway attempts to store this key in Redis with SET key 1 NX EX 300. If key already exists, the duplicate notification is silently discarded.

Deep Dive 2: Provider Failover & Circuit Breakers

If Twilio suffers an outage in Europe, SMS queues will back up.

  • Workers monitor vendor error rates via a Resilience4j / Envoy Circuit Breaker.
  • If error rate exceeds 10%, the system trips the breaker and routes European SMS traffic to a secondary vendor (e.g. Sinch or Infobip) within 500ms.

Key Architecture Trade-Offs & Decision Matrix

DimensionOption A: Asynchronous / DecoupledOption B: Synchronous RPCTrade-off & Recommendation
Communication PatternMessage Queues (Kafka / SQS)Direct gRPC / REST CallsMessage queues prevent cascading failures and provide backpressure buffering.
Failure HandlingDead Letter Queue (DLQ) with Exponential BackoffImmediate Circuit Breaker tripDLQ preserves unparseable messages for manual operational inspection.
Ordering GuaranteesPartition-level Strict FIFOUnordered Parallel ExecutionPartition-key routing guarantees chronological order per entity without global lock contention.