Skip to content

How to design a notification system (frontend)

The first sections are free. The deep dives, level expectations and quiz are part of Pro.

On this page
Asked at
Practice this question

The problem

Notifications for a web app: a list of past items that loads more as you scroll, new ones landing while the list is open, an unread badge, and short pop-ups that any screen or background task can raise. The answer turns on one client store keyed by notification id, fed by both cursor pages and a live stream, with the server as the owner of read state. The pop-ups are a second, smaller system: a single host at the app root fed by a capped queue and announced through a live region.

Clarifying questions

AskWhy it mattersA typical answer

Does the server push new notifications to the open app, or does the client ask on a timer?

It decides what the live path looks like: a connection to hold and reconnect, or a timer whose interval sets how stale the list and the badge can get.

Assume a live stream per person exists and carries three events: a notification created, a notification read, and the unread count changed. Checks on a timer are the fallback, with at least 60 seconds between checks.

Where do notification ids come from, and do they sort by time?

The list merges items from pages and from the stream by id, and both the cursor for older pages and the catch-up request lean on an order the client can trust.

The server assigns ids and they sort in creation order, so the client can use an id as a cursor in either direction. Ids from several servers that don't sort cleanly are a follow-up, not the base case.

How long is the history, and how big is a page?

It sizes the first paint, how often the scroll asks for more, and how many requests a long catch-up takes.

Pages of 50. Assume the history is long enough that the list has to load more as the person scrolls, and that it holds no total.

Does one person have the app open in several tabs and on several devices at once?

Read state has to agree everywhere; several tabs and devices are what turn it from a single request into a synchronization problem.

Yes: a phone and a laptop, with two tabs open on the laptop. The unread count must agree across them within seconds.

What does a pop-up carry, and who can raise one?

A plain text toast and one with an action button have different timing and accessibility needs, and the set of callers decides where the host has to live.

A severity level, the message text, an optional action button and a duration, with a close after 5 seconds as the default. Any screen or background task in the app can raise one through a single function.

What marks a notification read: opening the list, or an explicit action?

A badge that clears when the panel opens is a different state (seen) from one that clears per item (read), and the server has to model whichever we pick.

An explicit action: one item, or all of them up to a point. A separate seen state that clears the badge when the panel opens is a variant worth naming, not building today.

The requirements menu

RequirementScopeWhy

Open a list of past notifications that loads more as the person scrolls

Core

The list is the center of the product, and infinite scroll over a history that keeps growing is what makes paging a real question.

Show new notifications as they arrive, in the list and in a count of unread ones

Core

Live arrival is what breaks a plain paged list, and the badge is the piece that sits on each page of the app, so it has to be right.

Mark one notification or all of them as read, on every device the person uses

Core

Read state is the one piece that has to agree everywhere, which forces the server to own it.

Let any part of the app show a short pop-up message that closes by itself

Core

A second, smaller system: a host at the root with a queue, plus the accessibility rules for a message that goes away on its own.

Hold new items behind a banner when the person has scrolled down the list

Optional

Take it: it keeps the reading position when items land. Leave it if the interviewer wants the time spent on read state or pop-ups; inserting at the top is a defensible first cut.

Undo after marking everything read

Optional

Take it if you show the read at once and send it in the background; the undo is how a refused request gets explained. Leave it for a design that waits for the server before changing the screen.

A cap on how many pop-ups show at once, with the rest waiting their turn

Optional

Take it when several parts of the app can raise pop-ups in the same second; without it a burst covers the screen. Leave it in a product with a single source of pop-ups.

One summary line for notifications missed while the tab was away

Optional

Take it so a return after a weekend doesn't fire dozens of pop-ups; the list and the badge already carry the detail.

Keeping the whole notification history on the device for offline reading

Out of scope

Storage, eviction and conflict rules for a list that may be years long. On return, painting the newest page plus the badge is enough, and the rest loads on scroll.

Email and phone push notifications

Out of scope

Other channels with their own delivery rules; the in-app list is the source of truth when the app is open.

Notification settings and preferences

Out of scope

Preferences decide which notifications get created and over which channels; the list, the badge and the pop-ups work the same way whatever the preferences say.

How the server decides who gets a notification

Out of scope

Backend fan-out. The client sees items with ids and a count and doesn't care how they were produced.

Grouping similar notifications together

Out of scope

Grouping changes what an item is and how read state applies to it; it's a product question on top of this design.

Non-functional requirements

RequirementWhat it forces

The unread count agrees across open tabs and devices within seconds

Read state lives on the server, and each client displays the number the server sent, not a sum it keeps locally. A read on one device goes to the server first and then out on the live stream to the others; tabs of the same browser can also pass it to each other directly so none of them waits on the stream.

No notification shows twice or goes missing after a reconnect

One store keyed by notification id, so an item that arrives on both the stream and a page lands in the same slot; and after any gap, a request for items newer than the latest id the client holds before the stream is trusted again.

A pop-up never takes focus and is still announced to a screen reader

The host renders into a live region that assistive technology reads without moving focus. Focus stays where the person is working, and an action on a pop-up needs a second home in the UI because the pop-up closes on a timer.

A tap on a notification answers within 200 milliseconds

Marking read is drawn from client state right away and sent afterward; nothing on the tap path waits for the network. web.dev's good threshold for interaction to next paint is 200 milliseconds or less, so the paint that follows the tap has to show the item as read without waiting on a response.

A tab left in the background does almost no work

Listen for visibilitychange; a hidden tab closes its stream once it has been hidden for a short while (2,000 milliseconds in the scale we assumed) and stops its timers. On return it reconnects and fetches what it missed instead of having listened the whole time.

Core entities

EntityWhat it holds

Notification

One thing that happened to the person, as the server recorded it.

Page of notifications

One answer to a request for older items.

Unread count

The badge number, as the server last worked it out.

Pop-up

A short message raised by some part of the app, queued and drawn by the host.

Client notification state

Everything the open tab knows about notifications right now.

The API

EndpointNotes

GET /notifications?before={cursor}&limit=50 → { items, next_cursor, has_more }

The older-pages call. before is the id of the oldest item the client holds, so a page is defined by its position in the id order rather than by a page number that shifts as items land. Knock's feed API has the same shape: before and after cursors with a page_size between one and 50, defaulting to 50.

GET /notifications?after={newest_id} → { items, unread_count }

The catch-up call, used on reconnect, on return from hidden, and as the timer fallback. The server returns what the client missed and the count as of now, so one response fixes both the list and the badge.

POST /notifications/read { up_to_id } → { unread_count }

Marks everything read up to and including an id. Sending the newest id the client has, rather than a list of ids, keeps the request small and leaves anything that landed after it unread. The returned count replaces the local one.

Live stream per person: notification created, notification read, unread count changed

One connection per visible tab. In this design we define each event to carry the item or the count it changes, plus an event id the client hands back on reconnect; with server-sent events the browser sends the last event id it saw in a Last-Event-ID header on its own.

showPopup({ level, text, action, duration }) → popup id, callable from any part of the app

The single entry point into the pop-up system. It appends to a queue the host owns and returns an id, so a caller can close its pop-up later (an upload that fails and then succeeds on retry). Where the host renders is not the caller's concern.

The high-level design

pagescountServer APINotification listUnread badge

Step 1 of 4

A list that asks the server for pages

Start with the plainest version: the list component fetches a page of notifications from the server, the badge fetches the count, and scrolling to the bottom asks for the next page. It shows the data flow end to end, and it already carries the fault the rest of the design fixes: with numbered pages, each new item that lands at the top pushes the older ones down a slot, so the next page overlaps the one before it and the list repeats items. Nothing here hears about new items at all.

pageseventsbroadcastordercountscrolledtapreadundoerrorcappedannounceServer APINotification storeLive streamOther tabsNotification listUnread badgeNew items bannershowPopupBackground uploadPop-up queuePop-up hostScreen reader
The whole design

What breaks first

The deep dives

New notifications while the list is open. Someone is halfway down the list, with older pages still loading, when three new notifications land. What does your list do with them, and what stops any item from appearing twice?

A concept that helps here: Pagination and infinite scrollA concept that helps here: Client caching and normalized stores

Read and unread across devices. Someone clears all their notifications on their phone while the app sits open in two laptop tabs. What does each screen show, when does it change, and which copy of the count is right?

A concept that helps here: Coordinating across tabsA concept that helps here: Optimistic updates and rollbackA concept that helps here: Where state lives

Pop-up messages from anywhere in the app. A save on the settings page, a failed background upload and a mention from a teammate all want to tell the person something in the same second. How does each message reach the screen, and what does a screen reader user hear?

A concept that helps here: Accessible widgetsA concept that helps here: Designing a component API

Catching up after being away. Someone leaves the tab in the background on Friday and comes back on Monday, after dozens of new notifications. How do the list and the badge catch up, and how many requests does it take?

A concept that helps here: Reconnect and resumeA concept that helps here: Pagination and infinite scroll

Concepts this guide uses

Sources

  1. GitHub Docs: REST API endpoints for notifications
  2. Knock docs: in-app feeds overview
  3. Knock API reference: overview and pagination
  4. Knock docs: JavaScript SDK quick start
  5. notistack: API reference
  6. notistack: Basic features
  7. W3C: Understanding SC 4.1.3 Status Messages
  8. W3C: Understanding SC 2.2.1 Timing Adjustable
  9. MDN: Broadcast Channel API
  10. MDN: Page Visibility API
  11. MDN: ARIA aria-live attribute
  12. MDN: ARIA live regions
  13. HTML Standard: Server-sent events
  14. javascript.info: Server Sent Events
  15. web.dev: Web Vitals
Sign in to report a mistake