Sub topic 4
Accepting notification events
This chapter assumes you know HTTP APIs, basic message queues, and short Python snippets. It covers the entry point of the system: the API that producers call to request a notification.
What the ingestion API does
Producers are services such as orders, billing, or social features. They should not know about push, SMS, or email providers. They send one small event, and the notification system decides how to deliver it.
The ingestion API has three jobs:
- Check that the event is well formed.
- Store it durably, usually by writing it to a queue.
- Reply quickly, without waiting for delivery.
Delivery can take seconds or fail and retry. Doing it inside the request would tie the producer's latency to the slowest provider.
The event shape
An event carries only what later stages need. It has a unique id for deduplication, the target user, a type for preference lookup, a priority, and the data a template needs.
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(
event_id="evt-1001",
user_id="user-42",
type="order_shipped",
payload={"order_id": "A-77"},
)The event does not name a channel. Channel choice depends on user preferences, which are applied later.
Validation at the boundary
Reject bad input here, because errors are cheaper to fix before an event enters the queue. Check these points:
- Required fields are present and have the right types.
typeis a known notification type, so a template and preference entry exist for it.priorityis one of the allowed values, such asnormalorcritical.- The payload is within a size limit.
Return 400 for a malformed event and 401 or 403 for a caller that is not allowed to send. Do not look up the user's preferences at this point. That work belongs to a later stage and would slow the response.
Acknowledge, then process
After validation, write the event to a queue and return 202 Accepted. This status means the event was accepted for processing, not that anything was delivered.
Handling producer retries
Producers retry when a response is lost, so the same event_id can arrive twice. The API should treat event_id as an idempotency key. It should store the id on acceptance and return the original result if it sees the id again. Later chapters cover deduplication per channel in more detail.
Common errors
- Sending inline. Calling a provider inside the request makes producers wait and fail when a provider is down.
- Trusting the producer's channel choice. This bypasses user opt-outs.
- No event id. Without it, you cannot safely deduplicate.
- Acknowledging before the write is durable. If the API replies
202and then crashes before the enqueue completes, the event is lost.
Exercise
A producer's request times out, so it sends the same event again with the same event_id. What should the ingestion API do, and why?
Show answer
It should recognise the event_id as already accepted and return the original success response without enqueuing a second copy. This stops the producer's retry from creating duplicate notifications.