Designing a Scalable Notification System
Instruction: Given the task to design a scalable notification system for a social media platform, outline the database schema and the data warehouse model you would propose. Explain your choice of database (SQL vs. NoSQL) and how your design supports both real-time and analytical processing needs.
Updated
Official answer
Start with delivery latency, supported channels, preference rules and an acceptable retry policy. A relational operational model is a reasonable starting point when transactional consistency matters:
- notifications(notification_id, event_id, recipient_id, template_version, created_at), with a unique logical deduplication key appropriate to the event/recipient/channel contract.
- preferences(user_id, channel, enabled, updated_at), unique by user/channel.
- deliveries(delivery_id, notification_id, channel, state, next_attempt_at), with one logical delivery per notification/channel.
- delivery_attempts(delivery_id, attempt_number, provider_request_id, started_at, finished_at, outcome), unique by delivery/attempt.
- an outbox row committed with creation of the logical delivery.
Workers relay durable outbox work to a queue and retry boundedly. An ambiguous provider timeout needs a status lookup or provider-supported idempotency key; a queue alone cannot prevent duplicate notifications. Recheck applicable preferences/consent near dispatch and handle stale caches deliberately. Restrict payload access and retention.
For analytics, keep a notification fact at logical-recipient grain, a delivery fact at notification/channel grain, and a separate attempt fact at delivery-attempt grain. The delivery fact includes delivery_id, eligibility, status and relevant dates, including eligible deliveries with zero attempts. Use dimensions only at compatible grains: channel belongs to the delivery/attempt facts, while template and permitted recipient segment can describe the notification. Calculate delivery success per eligible logical delivery, not per retry attempt, and distinguish provider acceptance from user receipt or opening. For example, one successful delivery after three attempts plus one eligible delivery with no attempts gives delivery success 1/2; a successful-attempt rate of 1/3 answers a different question.
A NoSQL log store may suit measured write volume/access patterns, but delivery status is not inherently unstructured and SQL is not inherently unscalable. Preserve event IDs, late corrections and reconciliation across stores before splitting the architecture.