Design a notification system - when an author publishes a post, every follower who wants it is told once, also when the author has 2 million followers, a fanout task runs twice, or the push provider answers with a timeout. A push is only an attempt, so the system first records who must be told. The staff-level signal is one row per post and reader that makes every repeat harmless, and separate lanes so that a hot author, a busy reader or a slow provider delays only itself.
Written by a Staff+ Engineer and an Engineering Manager
12+ years of experience in distributed systems and real-time messaging. Tech lead for large-scale chat applications serving billions of users. Over 12 years of industry experience with 6 years in engineering leadership, building and scaling tech infrastructure teams that deliver end-to-end large-scale distributed systems.
Finished this lesson?
Sign in to save your progress across devices.
Tip: use ← and → to move between lessons.
Was this lesson helpful?
Rehearse this exact question with the author
45-minute mock with a Staff+ engineer who interviews at your target company. Premium members save $20.
What to Design?
Could you answer these today? Tick what you already know.
The happy path is quick to draw: a post comes in, look up the followers, send each one a push. The interview is decided by four questions about what happens when a task runs twice, an author is huge, a reader is busy, or a provider fails. Each one is a deep dive later on this page, with three designs side by side: the one that sounds right, a better one, and the one that holds.
Swipe to see all columns
| The four questions, and the answer that sounds right | |||
|---|---|---|---|
| # | The interviewer asks | The answer that sounds right | What it misses |
| DD1 | A fanout task dies after 6,000 of its 8,000 readers and runs again. Who gets two pushes? | Nobody, we remember who was done | Remembered where? A cache forgets, and check-then-send is not one step. Only a unique row per post and reader makes the second run harmless |
| DD2 | An author with 2 million followers posts. What happens to the post of an author with 300? | Nothing, we add more workers | |
The same three habits answer all four:
Do not spend your questions on the text of the banner. Spend them on what the system promises.
Swipe to see all columns
| Scope Decisions | ||
|---|---|---|
| Question | Agreed answer | Why it matters |
| Which events? | One: an author you follow published a post | One kind of notification. The hard part is the number of readers, not the number of types |
| Who gets it? | Every follower who wants it: notifications on, author not muted, not in quiet hours | Following is not enough. Preferences are checked for every reader of every post (Phase 2) |
| How big can one post be? | An author can have 2 million followers | |
Out of scope (say this out loud): the social app and its posts, the follow graph itself (another team owns it, we read a follower list), content moderation, email, the push providers themselves, and building a message queue.
FR1 — Accept the event. The post service reports "post published". The author never waits for the fanout.
FR2 — Tell every follower who wants it, once. A follower who wants the post is called a reader on this page. One notification per post and reader. Fanout means turning one post into one notification for each of its readers.
FR3 — Respect the reader's choices. A global switch, a mute per author, and quiet hours in the reader's own timezone.
FR4 — Push to the phone. On iOS through APNs, Apple's push service, and on Android through FCM, Google's. Several posts within a short time become one push.
FR5 — Show it in the app. The list in the app shows every notification, also when a push never arrived.
The key principle behind all five
Swipe to see all columns
| Five facts and the conclusions they do not supportRead each row as: the fact on the left does not, by itself, imply the claim in the middle. | ||
|---|---|---|
| Fact | Does not imply | Why the gap exists |
| The event was accepted | Somebody was notified | 202 means the job is stored. The fanout has not started |
NFR1 — Scale. 100 million notifications a day, and a single post that reaches 2 million followers.
NFR2 — Nothing lost, nothing twice. Every reader who wants a post is told once. Work may be repeated, the result may not.
NFR3 — Speed. 99% of pushes are handed to APNs or FCM within 5 minutes of the post, also for the largest author.
NFR4 — Fairness. A hot author never delays the posts of normal authors.
NFR5 — Availability. Accepting an event keeps working when the fanout or a provider is slow or down. In a healthy region the provider accepts 99.9% of the pushes.
| Capacity Thought Experiment | |
|---|---|
| Parameter | Value |
| Users | 10 million active a day, 5 million of them authors |
| Posts | 1% of the authors post on a day: 50,000 posts, about 0.6 a second |
| Notifications | 100 million a day: an average post reaches 2,000 readers |
| Average rate | About 1,160 notifications a second |
| One hot post | 2 million followers at once: as much as 29 minutes of average traffic |
The number that identifies the real system is the gap between the average and one post:
average 1,160 notifications a second
one hot post 2,000,000 followers in one moment = 29 minutes of average traffic
the target 5 minutes = 6,700 a second for this post alone
On average this system is nearly idle. It is sized for the moment a hot author posts, and for what the small authors experience in that moment. Worker speeds and fleet sizes are illustrative assumptions.
Interviewer signal: candidates who divide 100 million by 86,400 and call 1,160 a second "easy" have missed the system. Candidates who say "one post is work for 2 million followers that must be done in minutes, without delaying anybody else" have found it. The interviewer's looser number, 15 minutes until the user is reached, includes the provider's part, which we do not control.
Three ideas shape everything:
The durable story is who must be told about a post. It is one row per post and reader, called a notification intent, in our own database. The best-effort story is the push: a call to Apple or Google that can time out, be retried, or hit a token that no longer exists. The two are kept apart. A failed push never changes an intent, and the list in the app reads intents only.
Queues redeliver. Workers die halfway. The post service retries its call. So every step is built to give the same result the second time: a job is stored under a unique event ID, an intent under a unique pair of post and reader, a push under a unique pair of window and device.
A post with 2 million followers, a reader with twelve new posts, a provider that stops answering: each of them is normal. The design gives each its own lane. Hot posts have their own queue. Each reader has a 30-second window. Each provider has its own queue and workers.
The core recognition
This is a pipeline of small, repeatable steps around one table of intents. Accept, fan out, bundle, push: each step takes work from a queue, writes rows under a unique key, and only then deletes its message.
| Design Decisions | |
|---|---|
| Design choice | Reason |
| Store the job, then answer 202 | The author's side is done in 10 ms, and the event survives any crash behind it |
One intent per (post_id, recipient_id | |
Two tables carry the design:
notification_intentsandaggregation_windows. The intent says who must be told. The window says what happened to the push.
One row per post and reader. Written once by the fanout. Only read_at changes later.
Swipe to see all columns
| notification_intents | ||
|---|---|---|
| Field | Type | Comment |
intent_id | STRING | Key, for example int_a901 |
post_id | STRING | The post, for example post_901 |
author_id | ||
One row per reader and 30 seconds. All intents of that reader in those 30 seconds share it, and it becomes one push.
Swipe to see all columns
| aggregation_windows | ||
|---|---|---|
| Field | Type | Comment |
window_id | STRING | Key, for example win_a1 |
recipient_id | STRING | The reader. Unique together with window_start |
Swipe to see all columns
| Supporting records | ||
|---|---|---|
| Record | What it holds | Why it exists |
| Fanout job | job_id, event_id (unique), post_id, author_id, status ACCEPTED or QUEUED | The promise to fan out one post. Stored before the author's side gets its answer |
A device token is the address the provider gives an installed app on one phone. It changes when the app is reinstalled.
Swipe to see all columns
| Storage | ||
|---|---|---|
| Store | Data | Consistency |
| Notification store (PostgreSQL, 16 shards by user) | Jobs, preferences, devices, intents, windows, delivery attempts | The source of truth. Everything about one reader is on one shard, so intent and window are written in one transaction |
| Fanout queues (normal and hot) | Jobs and shard tasks | At least once. A message that is not deleted within 60 s is handed out again |
| Push queues (APNs and FCM) | One message per push | At least once, with a delay for retries |
Two groups of calls: one internal call for post events, and the calls of the app.
Swipe to see all columns
| The calls | ||
|---|---|---|
| Call | Who calls it | What it does |
POST /v1/notifications/ingest | Post service (internal) | Reports a published post: event_id, post_id, author_id, published_at. Answers 202 when the job is stored, 409 for an event_id it already has, 400 for a broken request |
The list call is the canonical read: it returns exactly what the intents say, whether or not any push was sent.
A visibility timeout is the rule that makes the queues safe: a message that a worker has taken is hidden, not deleted. If the worker does not delete it within 60 seconds, the queue hands it to the next worker that asks.
Present the design as four short stories, each in its own colour on one whiteboard: Phase 1 accept (black), Phase 2 fan out (blue), Phase 3 bundle and deliver (green), Phase 4 read in the app (purple). Phases 1 to 3 follow one post. The number on an arrow is the same number in the animation and in the step table.
Swipe to see all columns
| The four phases | |||
|---|---|---|---|
| Phase | Colour | Story | What it leaves behind |
| Phase 1: Accept the post event (FR1) | Black | The post service reports maya's post. The job is stored and queued, and the answer is 202 | One job row and one queue message |
| Phase 2: Fan out (FR2, FR3) | Blue | The post is split into shards of 10,000 followers. For each shard: who wants it, then one intent each | 8,000 intents for shard 7, each in its reader's window |
How to read the picture
post_901 at 12:00:00. alice, bob and carol follow her. Five seconds later leo, with 300 followers, publishes post_902.Each phase below is an interactive walkthrough of the same picture. It plays on its own. You can pause it, step with the arrow keys, and click any component to read its role.
Goal: take the event in a few milliseconds, exactly once, and never lose it.
/v1/notifications/ingestPost service → Ingest APINothing is written in this step.
Swipe to see all columns
| Phase 1 step by step (black arrows) | ||
|---|---|---|
| Step | What happens | Data written |
| 1 | The post service calls the Ingest API: event evt_901, post post_901, author maya, published at 12:00:00. | Nothing yet |
| 2 | The Ingest API inserts a fanout job. The event_id must be new. It is. | fanout_jobs row job_901: ACCEPTED |
Why it is built this way
event_id is the duplicate check. There is no "look first, insert second". The insert itself decides, so two calls at the same moment still create one job.ACCEPTED and no message exists. A small sweeper looks every 30 seconds for jobs that are still ACCEPTED and queues them again.Because a crash between the two loses the post's notifications, and nothing would ever notice. The post service has its 202 and will not send the event again. Storing first costs a few milliseconds, and after that the event can only be repeated, never lost.
Goal: turn one post into one intent for every reader who wants it, in tasks small enough to share and to repeat.
ReceiveMessage fanout-normalFanout workers → Fanout queues{ job_901, post_901, maya }job_901updateSwipe to see all columns
| Phase 2 step by step (blue arrows) | ||
|---|---|---|
| Step | What happens | Data written |
| 1 | A fanout worker takes the job of post_901 from the normal lane. | The message is hidden for 60 s |
| 2 | The worker asks the follower store how many followers maya has: 2,000,000. More than 10,000 makes her a hot author. | Nothing |
| 3 | The worker splits the post into 200 shard tasks of 10,000 followers each and puts them on the hot lane. Then it deletes the job message. | 200 shard tasks on the hot lane |
Why it is built this way
A normal author is simpler. leo has 300 followers, so at step 2 the same worker reads them, asks who wants the post and writes the intents at once: 0.15 seconds. At 12:00:05 his post adds a second intent, int_a902, to alice's window.
Because the Ingest API does not know how many followers an author has, and it should not have to ask: that would put the follower store into the publish path. The first worker that takes the job asks once. For a small author it simply does the work. For a hot author it only splits the post, which takes about a second, and moves the real work to the hot lane.
Goal: send one push per reader for everything that arrived within 30 seconds, and record what the provider answered.
SELECT windows WHERE status = OPEN AND flush_after is pastAggregator → Notification storewin_a1: alice, 2 intentswin_a1updateSwipe to see all columns
| Phase 3 step by step (green arrows) | ||
|---|---|---|
| Step | What happens | Data written |
| 1 | Every second the Aggregator looks for open windows whose 30 seconds are over. At 12:00:30 it finds win_a1: alice, two intents, maya's and leo's. | win_a1: OPEN to FLUSHED |
| 2 | The Aggregator reads alice's active devices: dev_a1, an iPhone. | delivery_attempts |
Why it is built this way
SENT means "the provider accepted it". It does not mean alice saw it. Nobody outside her phone knows that.The collapse ID is a label on the push. If the phone receives two pushes with the same label, the second replaces the first on the screen. It is the last guard against a push that is sent twice.
Yes, up to 30 seconds. The Aggregator cannot know whether a second post is about to arrive, so it waits for the window to end. A window with one intent is then pushed as itself: "maya posted", with the title of the post. The 30 seconds are well inside the 5-minute target.
Goal: show the reader every notification, without asking whether a push was sent.
/v1/notifications?limit=20Phone app → Notification APINothing is written in this step.
Swipe to see all columns
| Phase 4 step by step (purple arrows) | ||
|---|---|---|
| Step | What happens | Data written |
| 1 | alice opens the app. It asks the Notification API for her latest 20 notifications. | Nothing |
| 2 | The Notification API reads alice's intents, newest first: post_902 from leo and post_901 from maya, both unread. | Nothing |
| 3 | The app gets two items and has_more | |
Why it is built this way
Because pushes are the one thing we cannot count on. The phone was off, the banner was swiped away, the provider dropped an old push, the token had changed. A list built from received pushes has holes exactly where the reader needs it. The server knows who had to be told, so the server answers the question.
The deep dives are the four questions from the top of the page. Each one compares three designs for the same scenario: a Bad one, which is usually the answer that sounds right; a Good one, which still leaves a gap; and a Great one, which holds. The answers start covered. Read the scenario, decide what you would build, then click each card.
Swipe to see all columns
| Deep Dive Map | |||
|---|---|---|---|
| Deep Dive | Topic | Scenario | NFR |
Scenario: shard 7 of maya's post holds 10,000 followers. 2,000 of them do not want the post now, so the task has 8,000 readers to tell (Phase 2). Fanout worker F1 takes it and dies after 6,000 of them. It never deleted its message, so 60 seconds later the queue hands shard 7 to worker F2. Who gets two pushes?
Three ways to build it. Decide what you would do first. The answers are covered: click a card to reveal it.
The idea. The fanout worker walks through the readers and calls the push provider for each one. Nothing is written down.
Where it breaks. F2 knows nothing about F1's work, so it starts at reader 1 and pushes to all 8,000. The 6,000 readers that F1 had reached get the same push a second time. And if nobody had retried, 2,000 readers would have been told nothing. Without a record, the only choices are "twice" and "never".
The idea. Before each push the worker asks a cache: is this reader in the set sent:post_901? If not, it pushes and adds the reader.
What it fixes. Most of the repeat. F2 finds 6,000 names in the set, skips them and pushes to the last 2,000.
What it still misses. Check, push and add are three steps. F1 had pushed to reader 6,001 and died before adding the name, so F2 pushes to that reader again. If F1 was only slow and both workers run at once, both read "not there" for the same reader. And a cache forgets: after a restart the set is empty and all 6,000 get a second push. The guard fails exactly when things go wrong.
The key idea. Do not remember that a push was sent. Record that a reader must be told, under a key that can exist only once, and send pushes from that record.
How it works
event_id, so an event that arrives twice creates one job.The three designs side by side, in one picture:
Play the three options. Switch between them with the tabs at the top of the figure. A dashed box belongs to one design only.
ReceiveMessage fanout-hotF1 → Hot lane{ post_901, shard 7 }push to reader 1, 2, 3 ...F1 → APNs / FCMpost_901, shard 7updateSwipe to see all columns
| DD1 side by side | |||
|---|---|---|---|
| Bad: push as you go | Good: a set in a cache | Great: one row per reader | |
| Where "done" is recorded | Nowhere | In a set in a cache | In a unique row in the database |
| A shard that runs again | 8,000 pushes again | Skips most, a few doubles | 6,000 skipped, 2,000 added |
Check it against the scenario
| Shard 7 runs twice | |
|---|---|
| Situation | Outcome with the Great option |
| F1 dies after 6,000 intents | F2 repeats the shard: 6,000 inserts do nothing, 2,000 add rows. 8,000 intents, one per reader |
| F1 was only slow, and F2 starts the same shard | Both insert. For each reader one insert adds the row, the other does nothing |
| The post service sends evt_901 twice | The second job insert is refused. One job, 409 for the second call |
| The sweeper queues job_901 a second time | The post is split twice and every shard runs twice. All inserts of the second run do nothing |
| A push worker dies after APNs said 200, before it recorded the answer | |
The trade-off to name. A row for every reader of every post: 100 million inserts a day, each checked against a unique index. And the very last step cannot be made exact. A call to the provider and a write to our database are two systems, so a push can leave twice. The attempt row keeps that rare, and the collapse ID hides it on the phone.
That closes one gap of three. An atomic "add if missing" stops two workers from both passing the check. But the push is still a second step: the worker can add the name and die before it pushes, and then the reader is never told. And the cache can still lose the whole set. The row in the database solves both because nothing is sent at this point.
"Fanout tasks come from a queue with at-least-once delivery, so a shard can run twice. I make that harmless instead of trying to prevent it. The fanout worker does not push. It inserts a notification intent per recipient into a table with a unique key on post ID and recipient ID, using insert-or-do-nothing. A retried shard skips every row that exists and adds the rest. Pushes are created later from those rows, so no reader can be told twice by a repeat. The same idea protects the entry: the ingest call carries an event ID with a unique constraint, so a retried event creates one job."
Follow-up: "Can a push still go out twice?" — At the last step, yes: the provider call and my write are not one transaction. I key the attempt by window and device, and I set a collapse ID so the second copy replaces the first on the phone.
Scenario: at 12:00:00 maya publishes. She has 2,000,000 followers. Five seconds later leo publishes. He has 300. There are 40 fanout workers, and one worker expands 2,000 followers a second. When is each post fanned out?
Three ways to build it. Decide what you would do first. The answers are covered: click a card to reveal it.
The idea. Every post is one task: read all followers, write all intents. Workers take tasks from one queue.
Where it breaks. maya's task is 2,000,000 followers at 2,000 a second: 1,000 seconds, on one worker, while 39 others cannot help. Her last followers get their intent at 12:16:40, more than three times the 5-minute target. If that worker dies at minute 15, the task starts again at follower 1. leo is fine today, because another worker takes his task.
The idea. Split a big post into shard tasks of 10,000 followers and put them on the queue. Any worker can take any shard.
What it fixes. The big post. 40 workers take 40 shards at a time, 5 seconds each: all 200 are done in 25 seconds. A worker that dies costs one shard.
What it still misses. Everybody else. leo's task joins the queue at 12:00:05 behind the shard tasks that are still waiting, and starts at 12:00:25: 20 seconds late, for 0.15 seconds of work. But on an evening when 15 hot authors post within a minute, 3,000 shard tasks are 375 seconds of work for all 40 workers. Every small author behind them misses the 5 minutes, for something they had no part in.
The key idea. Split the big post so that many workers can share it. Then keep its pieces out of the way of everybody else.
How it works
The three designs side by side, in one picture:
Play the three options. Switch between them with the tabs at the top of the figure. A dashed box belongs to one design only.
ReceiveMessageone fanout worker → Fanout queue{ job_901, post_901, maya }job_901updateSwipe to see all columns
| DD2 side by side | |||
|---|---|---|---|
| Bad: one task per post | Good: shards, one queue | Great: shards, own lane | |
| maya's post as tasks | 1 task | 200 shard tasks | 200 shard tasks on the hot lane |
| maya is fanned out after | 16 min 40 s | 25 s | 35 s |
Check it against the scenario
| maya at 12:00:00, leo at 12:00:05 | |
|---|---|
| Situation | Outcome with the Great option |
| maya's post | Split in about a second, then 200 shards on 30 hot workers: the last intent is written about 36 seconds after the post |
| leo's post | A normal-lane worker takes it at once: 300 followers in 0.15 seconds |
| 15 hot authors in one minute | 3,000 shards need 500 seconds on 30 workers: too slow for the hot posts. The queue depth raises an alarm and more hot workers start: 50 bring it back to 300 seconds. Small authors do not notice |
| The whole way to the phone, for maya's last reader | Fan out 35 s, window at most 30 s, then 1,600,000 pushes at 20,000 a second: 80 s. About two and a half minutes |
The trade-off to name. Two lanes can waste workers: hot workers idle while the normal lane is busy, unless they are allowed to help. The split of 30 to 10 must follow the traffic.
It is a starting value, not a law. A task of 10,000 followers takes 5 seconds: short enough that no normal-lane worker is busy for long, and that a crash repeats little. A higher threshold makes normal tasks longer. A lower one sends more authors through the split, which costs a count and extra messages. Choose it from the data: if 99% of the authors have fewer than 10,000 followers, 99% of the posts are one short task. Then watch the fanout time and the depth of both queues, and move it.
"The average load is small. The design problem is one post with two million followers. I never fan that out as one task: it would take a single worker about 17 minutes and restart from zero on a crash. I split it into shard tasks of about ten thousand followers, so many workers share it and a retry repeats one shard, which is safe because intents are unique per post and recipient. And I keep those shards off the queue that small authors use: a hot lane with its own worker budget and a normal lane. A post with 300 followers is then done in a fraction of a second, whatever the big accounts are doing. The threshold and the budgets are tuned from follower distribution, fanout latency and queue depth."
Follow-up: "What if many hot authors post at once?" — The hot lane gets slower and the normal lane does not. I scale hot workers on queue depth, and I can cap the shards released per author so one celebrity does not block another.
Scenario: make the half minute busier. Between 12:00:00 and 12:00:30 twelve of the authors alice follows publish: maya, leo and ten more. How often does her phone buzz?
Three ways to build it. Decide what you would do first. The answers are covered: click a card to reveal it.
The idea. Every intent becomes a push at once. Twelve posts, twelve pushes.
Where it breaks. The system does exactly what was asked, and alice's phone buzzes twelve times in thirty seconds. She opens the app's settings and switches notifications off altogether. From now on she gets none. Every push was delivered, and the reader is lost.
The idea. At most 3 pushes per reader in 10 minutes. Whatever comes after that is not pushed.
What it fixes. The noise. Three buzzes are bearable.
What it still misses. The other nine posts get no push, and nobody says so. Which three alice hears about is decided by who posted first, not by what she cares about: the post of her favourite author was number nine. The nine are in her list in the app, but nothing tells her to look. A cap protects the reader by hiding things from her.
The key idea. Do not drop pushes. Wait a moment, and send one push that speaks for everything that arrived.
How it works
The three designs side by side, in one picture:
Play the three options. Switch between them with the tabs at the top of the figure. A dashed box belongs to one design only.
INSERT INTO notification_intents (post_901, alice)Fanout workers → Notification storeAPNs: send to the token of dev_a1Push workers → APNs / FCM200: acceptedint_a901new rowSwipe to see all columns
| DD3 side by side | |||
|---|---|---|---|
| Bad: one push per post | Good: a cap per reader | Great: a 30-second window | |
| Pushes alice gets | 12 | 3 | 1 |
| Posts a push tells her about | 12 | 3 | 12 |
| Posts in her list in the app | |||
Check it against the scenario
| Twelve posts between 12:00:00 and 12:00:30 | |
|---|---|
| Situation | Outcome with the Great option |
| All twelve posts | One window, one push at 12:00:30: "12 new posts from authors you follow". Twelve rows in the list |
| Only maya posts | One window with one intent. One push at 12:00:30: "maya posted" |
| A thirteenth post at 12:00:31 | It opens the next window, 12:00:30. Its push leaves at 12:01:00 |
| The Aggregator is down for two minutes | The windows stay open. When it returns it closes them all: late, but complete. The list had the intents all along |
The trade-off to name. Every push is up to 30 seconds late, also the one that would have been alone. That is a choice about the reader's experience, not a way to save work: the intents are written either way. If you cannot say why the window is there, do not add it.
That is a window that closes 30 seconds after the last post. It bundles better: a slow trickle ends up in one push. But it has no upper bound. A reader whose authors post every 20 seconds waits until the window is full, which can be minutes. With a fixed window the answer to "how late can a push be?" is one number: 30 seconds. In an interview that number is worth more than a slightly better bundle.
"A reader who follows many active authors can get a burst of notifications. Sending each one is correct and unusable, and a hard cap silently drops the rest. I aggregate per recipient instead: intents that arrive in the same 30-second window share one window row, written with an upsert in the same transaction as the intent. When the window closes, an aggregator queues one push: the post itself if there is one intent, a summary if there are several. The in-app list still shows every intent. The cost is up to 30 seconds of delay, which is a product decision I would confirm, and which is small against a 5-minute target."
Follow-up: "What if the aggregator is down?" — Windows stay open and are flushed when it returns. Nothing is lost, because the window and the intents are rows, not memory.
Scenario: at 12:00:30 two windows are closed. alice's push goes to her iPhone, and APNs does not answer in time. carol's push goes to the device row she has, and APNs answers 410: this token is gone. carol reinstalled the app yesterday. What does the push worker do with each?
Three ways to build it. Decide what you would do first. The answers are covered: click a card to reveal it.
The idea. A push that fails is put back on the queue and tried again, until it works.
Where it breaks. alice's second try works. carol's never will: the token is dead, and every try gets the same 410. Her push circles for ever, and every post of every author she follows adds one more. When APNs has a bad minute, all pushes fail together and are retried at once, with no pause: a storm against a provider that is already struggling.
The idea. Retry with a pause that doubles: 1, 2, 4 and 8 seconds. After the fifth try, give up.
What it fixes. The endless loop and the storm. alice's push works on the second try. carol's is dropped after five calls.
What it still misses. Nobody wrote down that carol's token is dead, so the next post makes five more calls to it. Five tries are over after 15 seconds: if APNs is down for two minutes, every push of those two minutes is dropped although it could have gone out a little later. And Android phones wait in the same queue behind the APNs retries.
The key idea. A failure is not one thing. Some answers mean "try again", one means "never again". Treat each as what it is, and retry the one push, never the fanout.
How it works
SENT, the window becomes SENT.attempt_count and a next_retry_at. The pauses double from 1 second up to 60, with a little randomness so retries do not arrive together.The three designs side by side, in one picture:
Play the three options. Switch between them with the tabs at the top of the figure. A dashed box belongs to one design only.
APNs: send to the token of dev_a1Push workers → APNsno answer in timeput the push backPush workers → Push queueatt_a1updateSwipe to see all columns
| DD4 side by side | |||
|---|---|---|---|
| Bad: back on the queue | Good: five tries | Great: read the answer | |
| A timeout | Retried at once, for ever | Retried 5 times | Retried with pauses until the push is 15 minutes old |
| A dead token | Retried for ever | 5 calls on every post | Marked INVALID |
Check it against the scenario
| A timeout for alice, 410 for carol | |
|---|---|
| Situation | Outcome with the Great option |
| alice: APNs does not answer | Transient. The same attempt is tried again one second later: 200. att_a1 is SENT |
| carol: APNs answers 410 | Permanent. dev_c1 becomes INVALID, att_c1 FAILED, win_c1 DROPPED. Her intent stays |
| maya posts again an hour later | carol's intent is written. At the flush she has no active device: the window is dropped without a call to APNs |
The trade-off to name. Somebody has to keep the list of answers up to date for each provider, and a wrong "permanent" removes a good token. That heals by itself, because the app registers its token on every start. And "retry until 15 minutes old" means some pushes arrive up to 15 minutes late.
Nobody knows. 200 means Apple took the push. The phone may be off, the banner may be hidden by a focus mode, or alice may have disabled notifications in the system settings. This is why SENT is the last state we can honestly record, and why the list in the app does not depend on any of it.
"The provider call is a side effect outside my system, so I record every attempt and I read the answer. Success marks the attempt and the window as sent, which only means handed over. Timeouts, server errors and rate limiting are transient: I retry the same delivery attempt with exponential backoff and jitter, and I stop when the push is too old to be useful. An unregistered token is permanent: I mark the device invalid immediately and never call it again. A retry never re-runs fanout or aggregation. Each provider has its own queue, workers, rate cap and kill switch, so an APNs brownout does not delay Android. And whatever happens to the push, the intent is still there, so the in-app list shows the notification."
Follow-up: "How does a user with a dead token get pushes again?" — The app registers its current token on every start. A reinstall produces a new device row, and the old one has already been marked invalid by the 410.
| If the interviewer keeps going | |
|---|---|
| Question | Short answer |
| Quiet hours: 22:00 to 08:00 in the reader's timezone | The simple rule: during quiet hours no intent is written, so nothing is pushed or listed. A kinder rule writes the intent and skips the push, so the post is in the list. A third holds everything for one summary at 08:00. Ask which one the product wants |
| Readers are tired of notifications | The window turns a burst into one push, and the mute removes single authors. A daily budget per reader can turn later pushes into summaries |
| A reader has not opened the app for 30 days | Keep writing intents, they are cheap. Push less often, for example one summary a day. Tokens of such phones are often dead anyway |
| The app was reinstalled | The new install gets a new token and registers it on its first start. The old token answers 410 once and is marked |
For the patterns behind this page, see Fan-Out and Feed Generation, Idempotency and Exactly-Once and the Message Queues building block. The Webhook question has the same last mile: deliver to somebody else's server, read the answer, retry with care.
The author then waits for Apple, and a timeout there fails the publish. Store a job, answer 202, and push in the background.
With no record, a repeated task tells readers twice and a lost task tells them never. Write who must be told, then push from that.
Check, send and mark are three steps, and a cache forgets when it restarts. A unique row in the database is one step and stays.
It takes one worker 17 minutes and starts again from zero after a crash. Split it into shards of 10,000.
200 shard tasks of one author stand in front of everybody. Give hot posts a lane and workers of their own.
Twelve buzzes in thirty seconds teach the reader to switch notifications off. Bundle per reader in a short window.
A 410 will be a 410 for ever. Mark the device invalid on the first one and stop calling it.
The provider confirms the handoff, nothing more. The list in the app, read from the intents, is what makes a lost push harmless.
The shape of a strong answer: store the event and answer at once. Fan out in the background, in shards, and write one intent per post and reader under a unique key. Give hot authors their own lane. Bundle each reader's intents in a 30-second window. Push through one queue per provider, read the answer, and retry only what can still work. Let the app read the intents, so a push is never the only copy of a notification.
Record first, push second
An intent says who must be told. It is written once and never changed by a push. Windows and attempts record what happened to the push, and the list in the app does not read them.
One row per post and reader
An event ID that exists once, an intent that exists once per post and reader, an attempt that exists once per window and device. Any step may run twice, and nobody is told twice.
Keep the lanes apart
A hot lane for authors with more than 10,000 followers, a window for each reader, a queue for each provider. One post is work for 2 million followers, and it delays only itself.
| What the interviewer expects at each level | |
|---|---|
| Level | A strong answer shows |
| Junior | A correct flow from post to push: a service, a database, a queue, a call to the provider. Explains why the event is stored before anything is sent |
| Mid-level | Explains the fanout problem and splits the hot author. Removes duplicates twice: by event ID at the entry and by post and reader in the fanout. Says why the push is best effort and the intent is the truth |
| Senior | Names the trade-offs unasked: window against delay, retries against duplicates, lanes against idle workers. Handles a provider that is down and a token that is dead. Justifies the thresholds, 10,000 followers and 30 seconds, from data |
These aren't toy problems — they reflect real infrastructure challenges at companies that notify millions of people at once. If you want to walk through this design in a mock interview or get feedback on your own approach, book a session with us. We're here to help you sharpen your thinking, build your confidence, and land your next offer.
Pick an answer for each question, and you see at once whether it is right. Show explanation opens the answer, why it is right, and why each of the other options fails.
Question 1 of 10
The post service reports a published post. When may the Ingest API answer 202 Accepted?
Question 2 of 10
The post service did not see the 202 and sends event evt_901 a second time. What happens?
Question 3 of 10
A fanout worker dies after writing 6,000 of the 8,000 intents of its shard. Another worker repeats the shard from the start. How many readers get two notifications?
Question 4 of 10
Why is a set of "already sent" readers in a cache not enough to stop duplicates?
Question 5 of 10
maya has 2,000,000 followers. A shard is 10,000 followers and takes one worker 5 seconds. How long do 40 workers need for her post?
Question 6 of 10
The hot post is already split into shards. Why does it still need a queue of its own?
Question 7 of 10
Twelve authors that alice follows post between 12:00:00 and 12:00:30. With a 30-second window, what does she get?
Question 8 of 10
What does the aggregation window cost?
Question 9 of 10
APNs answers a push with 410: the device token is no longer valid. What should the push worker do?
Question 10 of 10
carol's push was never delivered. How does she still find the post?
| One task of 2 million takes 17 minutes on one worker. Split into 200 tasks, it stands in front of everybody else, unless it has a lane of its own |
| DD3 | Twelve authors that alice follows post within thirty seconds. How often does her phone buzz? | Three times: we limit pushes per user | A limit drops the other nine without a word. A 30-second window turns twelve posts into one push that counts all twelve |
| DD4 | The provider answers one push with a timeout and another with 410. What now? | Retry both with backoff | A timeout may work next time. A dead token never will: mark it once and stop. And a retry repeats one push, not the fanout |
| One post is as much work as 29 minutes of average traffic (DD2) |
| May a notification be lost? Sent twice? | Neither. Missing one is worse, but a duplicate is not acceptable either | Work is repeated after a crash, so repeats must be harmless (DD1) |
| How fast? | 99% of pushes are handed to the provider within 5 minutes of the post | The author never waits. The fanout runs in the background, but not slowly |
| Which channels? | Push on iOS and Android, and a list inside the app | The list is the backup when a push is lost (Phase 4, DD4) |
| alice follows maya | alice gets a push | She may have switched notifications off, muted maya, or be in quiet hours |
| A task ran | It ran once | A queue that loses nothing will sometimes hand a task out twice |
| The provider accepted the push | alice saw it | Her phone may be off, or the banner may be hidden. The provider tells us nothing more |
| The push failed | The notification is lost | The notification is a row in our database. The list in the app reads that row |
| The target |
| 5 minutes are 300 seconds: about 6,700 a second for this one post |
| One fanout worker | Expands 2,000 followers a second. A task of 10,000 followers takes 5 seconds |
| Fanout workers | 40: together 80,000 followers a second |
| Push workers | 40, each hands over 500 pushes a second: 20,000 a second |
| Storage | About 1 KB per notification: 100 GB a day, 3 TB for 30 days |
| A fanout task that runs twice cannot tell a reader twice |
| Shards of 10,000 followers | A task takes 5 seconds, many workers share one post, and a crash repeats one shard |
| A hot lane and a normal lane | 200 shard tasks of one author never stand in front of a small author |
| A 30-second window per reader | Twelve posts become one push, at most 30 seconds late |
| The push state on the window, not on the intent | One push covers one window. An intent is written once and stays as it is |
| Read the provider's answer | A timeout is retried, a dead token is marked once and never called again |
| The in-app list reads intents | A lost push is not a lost notification |
STRING |
| Who published it |
recipient_id | STRING | Who must be told. Unique together with post_id |
window_id | STRING | The window this intent is pushed with |
created_at | TIMESTAMP | Orders the list in the app |
read_at | TIMESTAMP | Empty until the reader has seen it in the app |
window_start |
TIMESTAMP |
| The time rounded down to 30 seconds: 12:00:00, 12:00:30, and so on |
flush_after | TIMESTAMP | window_start plus 30 seconds. Never moved |
intent_count | INT | How many intents share this window |
status | ENUM | OPEN, then FLUSHED, then SENT or DROPPED |
| Preferences | Per user: notifications on or off, quiet hours, timezone. Per user and author: muted | Decides who wants a post. Read by the fanout for every reader |
| Device | device_id, user_id, platform ios or android, device_token, status ACTIVE or INVALID | Where a push can go. A user can have several |
| Delivery attempt | window_id, device_id (unique together), provider, status PENDING, SENT or FAILED, attempt_count, next_retry_at | One push to one phone, with its retries |
| Queue message | A job, a shard task such as post_901 shard 7, or a push job | Work waiting for a worker. Taken, hidden for 60 s, deleted when done |
| Follower store | Who follows whom | Owned by the social app. We only read it |
GET /v1/notifications?limit=20&cursor= |
| App |
The reader's notifications, newest first, with next_cursor and has_more. The reader comes from the login token |
POST /v1/notifications/read | App | Marks everything up to one notification as read |
POST /v1/devices | App | Registers a device token and its platform. Called on every start of the app |
PUT /v1/users/me/preferences | App | The global switch, quiet hours and timezone |
PUT /v1/users/me/authors/{author_id}/preferences | App | Mute or unmute one author |
| Phase 3: Bundle and deliver (FR4) | Green | alice's window closes after 30 seconds and becomes one push to APNs | One push for two posts, recorded as SENT |
| Phase 4: Read in the app (FR5) | Purple | alice opens the list. carol, whose push was lost, finds the notification there too | Read marks, and a new device for carol |
| 3 |
| The Ingest API puts the job on the normal lane of the fanout queues. |
One queue message. job_901: ACCEPTED to QUEUED |
| 4 | The Ingest API answers 202 Accepted, about 10 ms after the call. Nobody has been notified yet. | Nothing |
| 5 | The post service never saw the 202, because its connection dropped. It sends evt_901 again. The insert is refused: the event_id exists. The answer is 409 Conflict. | Nothing. Still one job |
| 4 | A worker takes shard 7 and reads its followers: number 60,001 to 70,000 of maya's list. alice, bob and carol are among them. | The shard message is hidden for 60 s |
| 5 | The worker reads the preferences of these 10,000 followers. 2,000 do not want the post now: notifications off, maya muted like bob, or quiet hours. 8,000 do. | Nothing |
| 6 | The worker inserts one intent per reader, each together with that reader's window. Then it deletes the shard message. | 8,000 intents. alice: int_a901 in window win_a1, count 1. carol: int_c901. bob: nothing |
PENDING| 3 | The Aggregator puts one push job on the APNs queue. | One queue message |
| 4 | A push worker takes the job and writes the text: "2 new posts from authors you follow". | The message is hidden for 60 s |
| 5 | The push worker calls APNs with the token of dev_a1 and the collapse ID win_a1. APNs answers 200: accepted. It shows the banner on alice's iPhone. | Nothing in our store yet |
| 6 | The push worker records the answer and deletes the queue message. | att_a1: PENDING to SENT. win_a1: FLUSHED to SENT |
| Nothing |
| 4 | alice looks at the list. The app marks everything up to int_a902 as read. | read_at on int_a901 and int_a902 |
| 5 | carol's push never arrived: her old device token was dead (DD4). At 12:10 she opens the app she reinstalled. It registers the new token. | devices row dev_c2: ACTIVE |
| 6 | carol's app asks for her notifications. maya's post is there, unread: int_c901. | Nothing |
| DD1 |
| A fanout task runs twice |
| A worker dies after 6,000 of 8,000 readers |
| NFR2 — Nothing lost, nothing twice |
| DD2 | An author with 2 million followers posts | A hot post and a small post, five seconds apart | NFR1, NFR3, NFR4 — Scale, speed, fairness |
| DD3 | Twelve posts in thirty seconds for one reader | One phone, twelve authors | NFR2 — and a reader who keeps notifications on |
| DD4 | The provider answers with a timeout, or with 410 | One slow answer, one dead token | NFR5 — Availability |
| The cache or a worker restarts |
| Already wrong |
| Everything is sent again |
| Nothing changes |
| Readers with two pushes | 6,000 | 1 to 6,000 | 0 |
| The push job returns and is sent again with the same collapse ID. The phone shows one banner |
| 0.15 s |
| 20 s |
| 0.15 s |
| A worker dies | Start again at follower 1 | One shard again | One shard again |
| 15 hot authors in one minute | 15 workers busy for 17 minutes | Every post waits up to 375 s | Hot posts wait, small authors do not |
| 12 |
| 12 |
| 12 |
| Delay of a push | None | None | Up to 30 seconds |
| What she does next | Switches notifications off | Misses nine posts | Opens the app |
DROPPEDINVALID at once. No retry. No later push is sent to it.DROPPED. The intent stays, so the list in the app has the post.| APNs is down for two minutes | A retry storm | Those pushes are dropped | They wait and go out afterwards |
| Android during an APNs problem | Stuck behind it | Stuck behind it | Its own queue: not affected |
| The reader still finds the post | Only if there is a list | In the list | In the list |
| carol opens the app at 12:10 |
| The new token is registered as dev_c2. The list shows maya's post. Later pushes reach her again |
| APNs is down from 12:00:30 to 12:02:30 | The attempts are retried after 1, 2, 4, 8, 16, 32 and 60 seconds, and go out when APNs is back |
INVALID| APNs has a brownout | Its queue grows, its workers slow down under the rate cap, and its kill switch can stop pushes for that provider, one platform or one hot author. Ingest, fanout and the list keep running |
| More than one region | Run the fanout in the region that holds the follower list. Replicate the intents, because the list reads them. Delivery attempts stay local |
| The post is stored, but the ingest call fails | The post service keeps an outbox: the event is written in the same transaction as the post and sent until it gets 202 or 409 |
| The unread badge | read_at on the intent, and a counter per reader that goes up with every intent and back to zero when the list is read |
| A push worker sends the same push twice | The attempt row is unique per window and device, so a second worker finds SENT and stops. For the rare gap after the provider's 200, the collapse ID makes the second banner replace the first |
| A queue loses its messages | Rebuild it from the rows: jobs that are not QUEUED, open windows whose time is over, attempts whose next_retry_at has passed |
| Email as a second channel | The same intent, another delivery worker with its own queue. The fanout does not change |