Bird
Raised Fist0
LLDsystem_design~7 mins

Notification system in LLD - System Design Guide

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Problem Statement
When users perform actions or when system events occur, failing to inform users promptly leads to poor user experience and missed opportunities for engagement. Without a structured notification system, messages can be delayed, lost, or sent in an inconsistent manner, causing confusion and dissatisfaction.
Solution
A notification system centralizes the creation, management, and delivery of messages to users through various channels like email, SMS, or push notifications. It queues notifications, applies user preferences, and retries failed deliveries to ensure timely and reliable communication.
Architecture
Event Source
(User Action)
Notification
Notification
Queue/Store

This diagram shows how events trigger notifications that are queued and processed by delivery handlers respecting user preferences.

Trade-offs
✓ Pros
Ensures reliable delivery by queuing and retrying failed notifications.
Supports multiple channels (email, SMS, push) through modular handlers.
Respects user preferences to avoid spamming and improve engagement.
Decouples event generation from notification delivery for scalability.
✗ Cons
Adds complexity with the need to manage queues and delivery status.
Requires handling of failures and retries which can increase latency.
Needs storage for queued notifications and user preferences.
Use when your system needs to notify thousands or more users asynchronously with multiple delivery channels and user-specific preferences.
Avoid if your application only requires simple, synchronous alerts for a small user base under 100 users.
Real World Examples
Uber
Uber uses a notification system to inform riders and drivers about trip status updates, cancellations, and promotions reliably across SMS and app push notifications.
Airbnb
Airbnb sends booking confirmations, reminders, and review requests through email and push notifications, managing user preferences and delivery retries.
LinkedIn
LinkedIn delivers notifications about connection requests, messages, and job alerts through multiple channels, ensuring timely and personalized delivery.
Code Example
Before, notifications were sent directly inside business logic, mixing concerns and risking delays or failures. After applying the notification system pattern, notifications are queued and sent asynchronously respecting user preferences, improving reliability and scalability.
LLD
### Before: No notification system, direct calls
class OrderService:
    def place_order(self, user, order):
        # process order
        self.send_email(user.email, "Order placed")

    def send_email(self, email, message):
        # direct email sending
        print(f"Sending email to {email}: {message}")


### After: Using Notification System
from enum import Enum

class Channel(Enum):
    EMAIL = 'email'
    SMS = 'sms'

class Notification:
    def __init__(self, user_id, message, channel):
        self.user_id = user_id
        self.message = message
        self.channel = channel

class NotificationQueue:
    def __init__(self):
        self.queue = []

    def enqueue(self, notification):
        self.queue.append(notification)

    def dequeue(self):
        if self.queue:
            return self.queue.pop(0)
        return None

class NotificationService:
    def __init__(self, queue, user_prefs):
        self.queue = queue
        self.user_prefs = user_prefs

    def notify(self, user_id, message):
        prefs = self.user_prefs.get(user_id, [Channel.EMAIL])
        for channel in prefs:
            notification = Notification(user_id, message, channel)
            self.queue.enqueue(notification)

    def process_notifications(self):
        notification = self.queue.dequeue()
        while notification:
            self.send(notification)
            notification = self.queue.dequeue()

    def send(self, notification):
        print(f"Sending {notification.channel.value} to user {notification.user_id}: {notification.message}")


class OrderService:
    def __init__(self, notification_service):
        self.notification_service = notification_service

    def place_order(self, user_id, order):
        # process order
        self.notification_service.notify(user_id, "Your order has been placed.")


# Usage
user_prefs = {1: [Channel.EMAIL, Channel.SMS], 2: [Channel.EMAIL]}
queue = NotificationQueue()
notif_service = NotificationService(queue, user_prefs)
order_service = OrderService(notif_service)

order_service.place_order(1, "Order123")
notif_service.process_notifications()
OutputSuccess
Alternatives
Polling-based notification
Clients periodically request updates instead of receiving push notifications.
Use when: Choose when real-time delivery is not critical and system simplicity is preferred.
Webhook-based notification
External systems receive notifications via HTTP callbacks rather than internal delivery.
Use when: Choose when integrating with third-party services that require event callbacks.
Summary
A notification system ensures reliable, timely delivery of messages to users across multiple channels.
It decouples event generation from message delivery using queues and respects user preferences.
This pattern improves user engagement and system scalability by handling retries and failures gracefully.

Practice

(1/5)
1. Which component in a notification system is responsible for generating events that trigger notifications?
easy
A. Delivery Channel
B. Notification Service
C. User Preferences Store
D. Event Producer

Solution

  1. Step 1: Understand the role of event producers

    Event producers create or detect events that require notifying users, such as a new message or alert.
  2. Step 2: Differentiate from other components

    Notification service processes events, delivery channels send notifications, and user preferences store user settings.
  3. Final Answer:

    Event Producer -> Option D
  4. Quick Check:

    Event source = Event Producer [OK]
Hint: Event creators are called producers in notification systems [OK]
Common Mistakes:
  • Confusing notification service with event producer
  • Thinking delivery channel generates events
  • Assuming user preferences create events
2. Which of the following is the correct sequence of components for sending a notification after an event occurs?
easy
A. Delivery Channel -> Notification Service -> Event Producer
B. Event Producer -> Notification Service -> Delivery Channel
C. Notification Service -> Event Producer -> Delivery Channel
D. User Preferences Store -> Event Producer -> Delivery Channel

Solution

  1. Step 1: Identify the logical flow of notification

    First, an event is generated by the event producer, then processed by the notification service, and finally sent via the delivery channel.
  2. Step 2: Eliminate incorrect sequences

    Delivery channel cannot start the process; user preferences store is not part of the sending sequence.
  3. Final Answer:

    Event Producer -> Notification Service -> Delivery Channel -> Option B
  4. Quick Check:

    Event -> Process -> Send = A [OK]
Hint: Notifications flow from event to service to delivery [OK]
Common Mistakes:
  • Reversing the order of components
  • Including user preferences in the sending chain
  • Confusing delivery channel as event source
3. Consider a notification system where users can choose email or SMS as delivery channels. If a user prefers both, what is the expected behavior when an event triggers a notification?
medium
A. Send notification via both email and SMS
B. Send notification via email only
C. Send notification via SMS only
D. Do not send any notification

Solution

  1. Step 1: Understand user preference handling

    If a user selects multiple delivery channels, the system should send notifications through all preferred channels to ensure delivery.
  2. Step 2: Confirm expected multi-channel delivery

    Sending via both email and SMS respects user choice and increases notification reach.
  3. Final Answer:

    Send notification via both email and SMS -> Option A
  4. Quick Check:

    Multiple preferences = multiple channels [OK]
Hint: Send notifications on all user-selected channels [OK]
Common Mistakes:
  • Sending notification on only one channel
  • Ignoring user preferences
  • Not sending notification at all
4. A notification system uses a queue to handle event processing but notifications are delayed significantly. Which is the most likely cause?
medium
A. Queue is overloaded with too many events
B. User preferences are not stored
C. Delivery channel is sending notifications instantly
D. Event producer is generating too few events

Solution

  1. Step 1: Analyze queue role in notification system

    Queue buffers events to handle load. If overloaded, it causes delays in processing notifications.
  2. Step 2: Evaluate other options

    Missing user preferences or instant delivery does not cause delay; too few events would reduce load, not increase delay.
  3. Final Answer:

    Queue is overloaded with too many events -> Option A
  4. Quick Check:

    Queue overload = delay [OK]
Hint: Delays often mean queue overload, not missing data [OK]
Common Mistakes:
  • Blaming delivery channel for delays
  • Assuming missing preferences cause delay
  • Thinking fewer events cause delays
5. You need to design a notification system that supports millions of users with personalized preferences and multiple delivery channels. Which design choice best ensures scalability and user customization?
hard
A. Use a centralized notification service with a single queue and fixed delivery channels
B. Store all user preferences in a local file on the notification server
C. Implement distributed notification services with sharded queues and dynamic delivery channel selection per user
D. Send notifications synchronously from event producers directly to users

Solution

  1. Step 1: Consider scalability requirements

    Millions of users require distributed services and sharded queues to handle load without bottlenecks.
  2. Step 2: Address user customization needs

    Dynamic delivery channel selection per user allows personalized notifications respecting preferences.
  3. Step 3: Evaluate other options

    Centralized service and single queue create bottlenecks; synchronous sending blocks processing; local files do not scale or support dynamic preferences.
  4. Final Answer:

    Implement distributed notification services with sharded queues and dynamic delivery channel selection per user -> Option C
  5. Quick Check:

    Distributed + dynamic preferences = scalable & customizable [OK]
Hint: Distribute services and shard queues for scale and flexibility [OK]
Common Mistakes:
  • Choosing centralized design causing bottlenecks
  • Using synchronous sending blocking system
  • Storing preferences in non-scalable local files