Module 35: System Design — Notification System

Comprehensive architectural breakdown of a global multi-channel Notification System (supporting iOS APNs, Android FCM, SMS, and Email), covering priority queues, rate limiting, deduplication windows, and third-party provider failovers.


Prerequisites

Before tackling the notification system, review these core modules:


ELI5 (30-Second Summary)

Imagine a massive emergency siren system for a city. The city needs to send out tornado warnings immediately, but also sends out parking ticket reminders and library book alerts.

If the city sends parking ticket reminders over the emergency siren, people will go deaf and mute the alerts. Furthermore, if a sensor glitches and detects a storm 10 times in 1 second, you don't want 10 sirens blasting at the same time (Deduplication). And if the siren wire to Apple or Google breaks, you need to store the message safely, wait a few seconds, and try again without crashing your servers (Retry & Dead-Letter Queues).

A modern Notification System prioritizes critical alerts (2FA codes, fraud detection), drops spammy duplicates, respects user quiet hours, and routes each message to the right delivery highway (Apple APNs, Google FCM, Twilio SMS, SendGrid Email).


Physical Analogy

Think of an Airport Central Dispatch and Paging Desk:

Airport Analogy Notification System Concept Technical Description
Urgent Air Traffic Control Alert High-Priority Queue 2FA codes, account takeover warnings (instant dispatch).
Flight Boarding Call Medium-Priority Queue Order delivery status, ride arrival notifications.
Duty-Free Shopping Discount Flyer Low-Priority Queue Promotional emails, weekly digest newsletters.
Gate Agent Paging Once, Not 5 Times Deduplication Window (Redis) Suppresses identical alerts sent within 5 minutes.
Airport Public Address Speakers Mobile Push Gateways Apple APNs and Google Firebase Cloud Messaging (FCM).
Postal Mail Delivery Email Gateways SendGrid, Amazon SES, Mailgun.

The Problem It Solves

A notification service must process billions of outbound events daily while solving critical distributed systems challenges:

  1. Unreliable External Vendors: Apple APNs, Google FCM, Twilio, and SendGrid can experience latency spikes, rate limit exhaustion, or regional outages. Internal systems must not lock or fail when an external vendor is down.
  2. Notification Fatigue & Spam: Bombarding a user with 50 push notifications an hour leads to users disabling app notifications entirely. Systems must enforce per-user rate limits and quiet-hour schedules.
  3. Race Conditions & Duplication: Network retries across microservices can cause customers to receive three identical 2FA SMS messages or duplicate credit card debit alerts.

Visual Architecture

High-Throughput Multi-Channel Notification Platform

flowchart TD
    subgraph Ingestion["1. Ingestion Layer"]
        ServiceA["Billing Service"] -->|"POST /v1/notifications"| Gateway["Notification API Gateway"]
        ServiceB["Auth Service (2FA)"] -->|"POST /v1/notifications"| Gateway
        ServiceC["Marketing Engine"] -->|"POST /v1/notifications"| Gateway
    end

    subgraph Validation["2. Validation & Deduplication"]
        Gateway --> DedupCheck{"Redis Dedup Window (5m)"}
        DedupCheck -->|"Duplicate Found"| Drop["Drop Duplicate (Log Metric)"]
        DedupCheck -->|"Unique"| UserPref["User Preference & Rate Limit Check"]
        UserPref --> Prioritize{"Priority Routing"}
    end

    subgraph Queues["3. Partitioned Message Queues"]
        Prioritize -->|"Critical (2FA, Fraud)"| HighQ["Kafka: High-Priority Queue"]
        Prioritize -->|"Transactional (Orders)"| MedQ["Kafka: Medium-Priority Queue"]
        Prioritize -->|"Marketing (Promo)"| LowQ["Kafka: Low-Priority Queue"]
    end

    subgraph Workers["4. Worker Dispatch & Vendor Routing"]
        HighQ --> WorkerHigh["High-Priority Workers"]
        MedQ --> WorkerMed["Standard Workers"]
        LowQ --> WorkerLow["Batch Workers"]
        
        WorkerHigh --> APNS["Apple APNs (iOS)"]
        WorkerHigh --> FCM["Google FCM (Android)"]
        WorkerMed --> Twilio["Twilio / MessageBird (SMS)"]
        WorkerLow --> SendGrid["SendGrid / SES (Email)"]
    end

    subgraph DeadLetter["5. Failure Handling"]
        WorkerHigh -.->|"Failure after 3 Retries"| DLQ[("Dead Letter Queue (DLQ)")]
    end

Real Numbers & System Sizing

Consider a system sending notifications for an application with 100 Million Active Users:


Deep Dive: Under the Hood

1. Delivery Channel Providers

2. Deduplication Algorithm

To prevent burst duplicates (e.g., three network retries from an upstream microservice):

  1. Compute fingerprint: hash = sha256(user_id + channel + content_fingerprint).
  2. Execute Redis atomic lock: SET dedup:{hash} 1 EX 300 NX.
  3. If SETNX returns 0, the message is a duplicate within the 5-minute window; drop immediately.

3. Rate Limiting & User Preferences


Trade-offs & Decision Matrix

Dimension Synchronous Direct Dispatch Asynchronous Queue-Based Dispatch Multi-Tier Priority Queuing
Latency for 2FA Lowest (< 50 ms) Low (100 - 300 ms) Ultra-low for High-Q (< 100 ms)
System Resilience Terrible (vendor outage crashes caller) High (queues buffer traffic) Highest (marketing cannot starve 2FA)
Infrastructure Cost Zero queues needed Moderate (Kafka/RabbitMQ cluster) High (multiple queue partitions and worker pools)
Burst Protection None (downstream vendors throttle) Full (workers drain at vendor rate limit) Full with SLA guarantees per tier

Common Pitfalls & Anti-Patterns

  1. Mixing 2FA and Promotional Campaigns in a Single Queue: If a marketing campaign pushes 10 million email blasts to the queue, a customer logging in cannot receive their 2FA password reset SMS for 45 minutes! Rule: Strict separation into independent High, Medium, and Low priority queues.
  2. Missing Dead-Letter Queues (DLQ): If a user uninstalls the app or changes their phone number, APNs/Twilio returns an unrecoverable 400/404 error. Retrying endlessly consumes resources. After 3 attempts, push failing items to a DLQ and mark the device token as invalid.
  3. Hardcoding Vendor Integrations in Application Code: Coupling services directly to Twilio or SendGrid makes switching vendors during an outage impossible. Always abstract vendors behind a generic Notification Gateway.

Try It Yourself

A runnable, zero-dependency Python simulation demonstrating multi-channel notification dispatch, priority queue scheduling, Redis-style deduplication window suppression, and exponential retry is included in this directory.

Run the simulation:

python demo.py

Production References & Further Reading


Top 5 Interview Questions with Model Answers

Q1: How do you prevent promotional notifications from delaying time-sensitive 2FA verification codes?

Model Answer: We enforce Strict Queue Partitioning by Priority Level:

Q2: How do you guarantee exactly-once notification delivery to users?

Model Answer: In distributed systems, guaranteed exactly-once delivery across unreliable third-party networks (telecom carriers, APNs, FCM) is impossible due to network splits. Instead, we implement At-Least-Once Delivery with Aggressive Deduplication:

  1. Client/Producer assigns an idempotency_key (or we compute a SHA-256 fingerprint of user_id + channel + message_digest).
  2. Workers use Redis SETNX key "LOCKED" EX <window> to suppress duplicate submissions within a 5-10 minute window.
  3. Mobile client apps implement client-side deduplication using notification IDs to ensure identical messages arriving twice only render a single notification in the OS tray.

Q3: What happens when an external push vendor (e.g., Apple APNs) experiences a major outage?

Model Answer:

  1. Circuit Breaking: The worker pool detects consecutive HTTP 5xx errors from APNs and trips the circuit breaker to avoid wasting connection pools.
  2. Queue Buffering: Messages accumulate safely in Kafka/RabbitMQ without impacting upstream services.
  3. Fallback Channels: If configured by business policy, critical alerts can fall back to alternative channels (e.g., if iOS push fails, fall back to SMS).
  4. Exponential Backoff with Jitter: Once APNs recovers, workers drain the queue gradually to avoid overwhelming the vendor.

Q4: How do you handle invalid device tokens when users uninstall the app?

Model Answer: When APNs or FCM attempts delivery to an uninstalled app, they return a specific status code (e.g., APNs 410 BadDeviceToken or FCM UNREGISTERED). When the worker receives this code:

Q5: How do you track delivery status and open rates across millions of notifications?

Model Answer: We build an asynchronous analytics feedback loop:


Check Yourself Quiz

1. Why are message queues mandatory between the API Gateway and notification vendors? A) Because databases cannot store phone numbers.
B) To decouple internal systems from slow, rate-limited, and potentially unavailable third-party APIs.
C) Because APNs only accepts connections via Kafka.
D) To encrypt messages with AES-256.

Answer: B. External vendors can be slow or experience outages. Queues buffer requests and protect system availability.
2. How does a deduplication window prevent duplicate notifications? A) By deleting the user's account.
B) By checking an atomic Redis SETNX key of the message fingerprint within a rolling time window (e.g. 5 minutes).
C) By rebooting the phone.
D) By slowing down the network.

Answer: B. Hashing message parameters and checking an atomic Redis key ensures duplicate triggers within the window are rejected.
3. What should a worker do when receiving HTTP 410 BadDeviceToken from Apple APNs? A) Retry 50 times.
B) Deactivate the device token in the database and cease further dispatches to it.
C) Crash the worker pod.
D) Switch to email.

Answer: B. 410 indicates the token is permanently invalid (app uninstalled); retrying wastes bandwidth and risks vendor throttling.
4. Why should 2FA alerts use a completely isolated queue from marketing campaigns? A) Because 2FA uses a different programming language.
B) To prevent large marketing bursts from blocking the queue and delaying time-sensitive authentication codes.
C) Because marketing emails are illegal.
D) Because marketing queues do not support SMS.

Answer: B. Priority isolation guarantees zero latency degradation for critical authentication and fraud alerts.
5. Which mobile push service is used natively for Google Android devices? A) APNs
B) Firebase Cloud Messaging (FCM)
C) SendGrid
D) Twilio

Answer: B. FCM is Google's native cloud messaging service for Android.