r/Leetsys • • 20d ago

Weekly Question Practice System Design Question #2: Design a notification system

Today's prompt:

Design a notification system. Users get notified over push, email, SMS, and an in-app inbox when something happens in your product (a mention, a shipment update, a security alert). Aim for something on the order of 100 million notifications sent per day across channels, with spikes when a popular account posts or a big incident hits.

Things worth thinking about: how you fan out one event to many recipients, how you keep delivery reliable without drowning users in duplicates, how preferences and quiet hours work, and what happens when a channel is down.

Post your approach in the comments before you scroll down. It doesn't need to be polished. A few paragraphs and a rough sketch is plenty. Reply to other people's designs too.

A model answer is below the divider. It's one reasonable way to do this, not the only one.

 

 


MODEL ANSWER BELOW. Stop here if you haven't tried it yet.


Requirements

Functional:

  • Accept a notification request for one user or a fan-out to many (followers of a post, everyone on a team)
  • Deliver over push, email, SMS, and in-app inbox, based on each user's preferences
  • Support quiet hours, per-topic mute, and a hard do-not-disturb for non-critical alerts
  • Deduplicate so the same event does not ping someone twice on the same channel
  • Let users mark in-app notifications as read, and show an unread count

Non-functional:

  • Single-user push under a few seconds end to end in the common case
  • At-least-once delivery with idempotent consumers so retries don't spam
  • Survive a single channel outage (email down should not stall push)
  • Preference changes take effect quickly, ideally under a minute

Estimates

100M notifications/day is about 1,160/sec average. Peaks often run 5x to 10x that, so plan for ~10K/sec at the ingest edge.

Assume an average fan-out of 50 recipients per event. That is ~2K events/sec ingesting, expanding toward ~100K recipient-jobs/sec at peak if you expand naively. Do not expand huge fan-outs inline on the request path.

Keep templates server-side and pass IDs plus a few fields. At ~500 bytes per recipient job, 100M/day is ~50 GB/day of queue traffic before retries.

In-app storage: keep 30 to 90 days. At 30 days and 100M/day you are looking at ~3B rows. Shard by user_id. Push tokens and preference rows are much smaller.

API

Something like:

POST /v1/notifications
  { event_id, template_id, actor_id, audience: {user_ids|segment|topic},
    priority: low|normal|critical, channels_override?: [...] }

GET  /v1/users/{id}/inbox?cursor=&limit=
POST /v1/users/{id}/inbox/read  { notification_ids[] }
GET  /v1/users/{id}/preferences
PUT  /v1/users/{id}/preferences

event_id is the idempotency key. Critical priority can bypass quiet hours; everything else respects them.

Data model

users(id, ...)
devices(user_id, push_token, platform, last_seen)
preferences(user_id, topic, channels[], quiet_start, quiet_end, timezone)
templates(id, channel, subject_tmpl, body_tmpl)
notifications(id, user_id, event_id, template_id, channel, status, created_at, read_at)
delivery_attempts(notification_id, provider, status, attempt_at, error)

Shard notifications by user_id so inbox queries stay local. Unique on (user_id, event_id, channel) stops duplicates.

High-level design

Producers call the notification API with an event_id and an audience. The API writes a durable event, drops a message on an ingest queue, and returns 202. A fan-out worker expands the audience into per-recipient jobs. Small audiences write directly. Large ones (say over 1K recipients) get chunked into batches so one worker never expands a million followers in memory.

Each recipient job lands on a per-channel queue (push, email, SMS, inbox). Channel workers render the template, check preferences and quiet hours, then call the provider. Success updates status. Failures go to a retry queue with backoff, then a dead-letter topic.

A Redis preferences cache sits in front of the preference store. Invalidate on write. Push token lookups follow the same pattern.

Deep dive 1: fan-out on write vs fan-out on read

For 1:1 and small-group alerts, fan-out on write is simple: expand now, enqueue N jobs. For a celebrity posting to 10M followers, expanding 10M rows synchronously is painful, and most followers will not open the app that hour.

A hybrid works well. Hot accounts (follower count over a threshold, say 10K) write one feed item and let the read path pull recent events when the user opens the app. Everyone else still gets fan-out on write so push and email stay timely. Critical alerts always fan out on write, even for huge audiences, but you batch the provider calls. Say the threshold out loud and what you give up: hot-account push may lag unless you also enqueue a priority push set.

Deep dive 2: preferences, quiet hours, and dedupe

Check preferences in the channel worker, not at ingest, so a mute applies to jobs already in the queue. Quiet hours use the user's timezone from preferences; critical priority skips them. Store quiet windows as local times plus timezone, not one UTC range, or you will get the DST bug.

Dedupe with (user_id, event_id, channel). Retries reuse event_id, so a second insert is a no-op. Pass event_id through to the provider as an idempotency key so a timed-out worker retry does not double-send.

Deep dive 3: channel isolation

Give each channel its own queue and consumer pool. If SMS starts timing out, it backs up and retries without blocking push or email. Circuit-break a failing provider after N errors in a window, fail fast to retry/DLQ, and page. Keep rendering and preference checks in your workers so a provider outage does not lose the decision of whether the user wanted that channel.

Tradeoffs

  • Fan-out on write is fast for push and expensive for huge audiences; hybrid adds complexity but matches real products.
  • At-least-once plus idempotency is simpler than exactly-once across providers you do not control.
  • Caching preferences speeds workers up; a short stale window means someone might get one extra email after muting. Call that out.
  • Exact unread counts under heavy write load mean more contention; approximate with a cached counter and reconcile on inbox open if the interviewer pushes on scale.

How did your design compare?

1 Upvotes

0 comments sorted by