System Design: How to Design a Notification System (Outbox Pattern, Kafka Fan-Out & Idempotency)
How to send 10 million push notifications, emails, and SMS messages reliably without blocking your checkout API or double-charging users on retries.
Calling third-party providers like Twilio (SMS), SendGrid (Email), and APNs/FCM (Push) synchronously inside a user’s HTTP request is a classic production anti-pattern: one slow provider stalls your API threads, and a network timeout mid-request leaves you guessing which notifications actually sent.
1. Transactional Outbox Pattern
Instead of calling external APIs inside the request thread, write the order record and an outbox_events row inside the same ACID database transaction (COMMIT in <5ms). A background publisher relays outbox rows into Kafka so no notification is ever lost if a broker restarts.
2. Independent Per-Channel Topic Queues
Fan out events into isolated topic queues by channel (push_topic, email_topic, sms_topic) with dedicated worker pools:
- If Twilio SMS experiences a 30-second outage, only the
sms_topicqueue buffers and retries—Push and Email workers continue running at full speed (0mscross-channel head-of-line blocking).
3. Idempotency Keys + Exponential Backoff & DLQ
Because message queues guarantee at-least-once delivery, every worker checks SETNX notif:{event_id}:{channel} in Redis before calling the provider, retries transient 503 errors with exponential backoff + jitter (1s → 2s → 4s → 8s), and routes permanently poisoned payloads to a Dead-Letter Queue (DLQ).
Test Your Understanding
Quick architectural scenario challenge based on this breakdown.
A 3rd-party payment gateway starts timing out with 504 errors during peak traffic.
Q1.How should a downstream dependency failure be handled to protect upstream service availability?
Comments –
Sign in to share your thoughts
Nothing yet. Be the first.