Sub topic 1
Clarifying Requirements and Scope
Prerequisites
Assume you understand HTTP APIs, databases, key-value stores, message queues, asynchronous processing, and short Python snippets.
Clarifying requirements and scope
Start by defining what the system must do and how well it must do it.
Functional requirements include:
- Accept events such as
order_shipped. - Choose one or more channels.
- Respect opt-outs, preferences, and quiet hours.
- Retry temporary provider failures.
- Record delivery status.
Non-functional requirements describe quality constraints. Define expected volume, latency, availability, retention, privacy, and cost. For example, an urgent security alert might require delivery within 30 seconds, while a weekly summary can wait several hours.
Also define scope boundaries. Decide whether the first version supports marketing messages, attachments, replies, scheduled delivery, or only transactional notifications.
Choosing channels: push, SMS, email and in-app
| Channel | Strength | Common limitation |
|---|---|---|
| Push | Fast and inexpensive | Requires a valid device token |
| SMS | Reaches users without an app | Costs more and has regional limits |
| Supports detail and history | Delivery may be delayed | |
| In-app | Reliable while the user is active | Does not reach offline users |
Use channel fallback deliberately. For example, an urgent alert might use push followed by SMS. Do not send every message through every channel, because that increases cost and notification fatigue.
A high-level architecture
Event producers should not call providers directly. An ingestion API validates events and places them on a queue. Channel workers consume queued jobs, apply routing rules, render templates, and call providers. A status store records outcomes and feedback.
Accepting notification events
An event needs a stable identifier, recipient, type, priority, and template data.
from dataclasses import dataclass, field
@dataclass(frozen=True)
class NotificationEvent:
event_id: str
user_id: str
type: str
priority: str = "normal"
payload: dict = field(default_factory=dict)
event = NotificationEvent(
"evt-1001", "user-42", "order_shipped", payload={"order_id": "A-77"}
)
print(event)Respecting user preferences and quiet hours
Store preferences by user and notification type. Check opt-outs before rendering or sending. During quiet hours, keep only non-intrusive channels unless the event is critical.
def choose_channels(event, preferences, quiet_hours):
channels = list(preferences.get(event.type, []))
if quiet_hours and event.priority != "critical":
channels = [channel for channel in channels if channel == "in_app"]
return channelsExercise
Why should a producer enqueue an event instead of calling an SMS provider directly?
Show answer
A queue isolates the producer from provider latency and failures, and lets workers retry or rate-limit delivery.