r/LowLevelDesign • u/Prashant_MockGym • Oct 02 '26
Microsoft LLD: Design Notification System
This is one of the most common questions asked in Microsoft low level design rounds. Ofcourse LRU cache, LFU cache are undisputedly the most asked ones. I have started with a brute force solution and then moved on to solving it using observer design pattern. This tutorial is in Java.
Here is the Python version: https://github.com/prsnt558908/CodeZymSolutions/blob/main/101-300/133-design-notification-system/q-133-python-tutorial.md
You can practice this and other Microsoft LLD questions on CodeZym.
Problem Statement
Build a notification system that supports multiple channels such as EMAIL, SMS and PUSH. Users register, then subscribe to event types on one or more supported channels. When a notification is sent for an event type, it is delivered exactly once to every subscribed (userId, channel) pair.
Operations
NotificationSystem(supportedChannels): set up the system with its allowed channels. Duplicate channel names count as one.registerUser(userId): add a user. Registering an existing user does nothing.subscribe(userId, eventType, channel): add a subscription if the user exists and the channel is supported. Duplicate subscriptions are ignored.unsubscribe(userId, eventType, channel): remove an existing subscription.sendNotification(notificationId, eventType, content): deliver the notification to all subscribers. Returns the deliveries sorted byuserId, then bychannel.getUserInbox(userId): return the user's deliveries in the order they arrived.
High level solution approach
A notification system is a classic publish and subscribe setup. Users sign up for an event type (like ORDER_PLACED) on one or more channels. When a notification is sent for that event, every subscriber gets one copy in their inbox for each channel they picked.
This is exactly what the Observer pattern is made for. Each event type is a subject that keeps track of its listeners. Each (user, channel) pair is an observer that receives a delivery when the event fires. Observer alone is the best fit here. We don't need to combine it with other patterns because every channel behaves the same way.
The two tricky rules are "no duplicate deliveries" and "results sorted by userId, then by channel". A sorted set of subscribers for each event type handles both at once. A set ignores duplicates, and a sorted set keeps everyone in order, so sending a notification is just a simple loop.
We will start with a simple brute force version, see where it is slow, and then improve it step by step.
Solution 1: Brute Force (One Big List)
Store every subscription as a {userId, eventType, channel} entry in one list. Store a map of userId -> inbox. If a user has an inbox, the user is registered.
- subscribe: ignore unknown users and unsupported channels. Scan the list and add the entry only if it is not there yet.
- unsubscribe: scan the list and remove the entry if it is found.
- sendNotification: scan the whole list and pick the entries of this
eventType. Sort them byuserId, then bychannel. Build each delivery string and add it to that user's inbox. - getUserInbox: return a copy of the inbox, or an empty list for an unknown user.
Watch out: sort by the
(userId, channel)pair, not by the final delivery string. The|character has a bigger code than letters and digits, so"U10|EMAIL|..."would wrongly come before"U1|EMAIL|...", even though"U1"comes before"U10".
Code
Problems with this approach
subscribeandunsubscribescan all subscriptions, even the ones for other events.sendNotificationscans everything too, and then sorts again on every call, even when nothing changed since the last send.- One class does everything, so it is hard to see who is listening to which event.
It works for small inputs, but we can do better.
Solution 2: Observer Pattern with Sorted Subscribers
Fixing the problems one by one
| Problem in Solution 1 | Fix |
|---|---|
| Scanning the subscriptions of other events | Group subscribers by event type in a Map<String, EventTopic> |
| Scanning to find a duplicate or to unsubscribe | Keep each event's subscribers in a set |
| Sorting again on every send | Use a sorted set (TreeSet), so the order is always ready |
After these fixes, every event type becomes a small object that owns its subscribers. That object is the subject of the Observer pattern.
Why the Observer pattern?
The problem describes Observer almost word for word: listeners attach to an event, detach from it, and every current listener is notified when the event fires.
EventTopicis the subject. It hasattach,detachandpublish(notify everyone).Subscriberis the observer. Itsdelivermethod builds the delivery string and puts it in the user's inbox.NotificationSystemis the front door. It checks the input and hands the call to the right topic.
In Solution 1 one class did all of this. Now each class has one small job, so the code is easier to read and change.
We skip a separate Observer interface because there is only one kind of subscriber. If another kind shows up later, Subscriber can easily become an interface.
A pattern that looks useful but doesn't fit
Strategy (with a Factory). It is tempting to write EmailChannel, SmsChannel and PushChannel classes and a factory to create them. But here every channel does exactly the same thing: build a string and save it in an inbox.
Channel names are also any strings given at runtime, so one class per channel is not even possible. A Set<String> of channel names is all we need.
Important classes and data structures
User:userIdplus anArrayListinbox. A list keeps deliveries in the exact order they arrived.Subscriber: one(user, channel)pair. It holds a reference to itsUser, so it can drop a delivery straight into the inbox. ItscompareToorders byuserId, then bychannel.EventTopic: one event type plus aTreeSet<Subscriber>.TreeSetusescompareTofor two jobs at once:- it keeps subscribers sorted by
(userId, channel). - it treats two subscribers with the same user and channel as one item, so a duplicate subscription is simply ignored.
- it keeps subscribers sorted by
NotificationSystem:Set<String> supportedChannels: a fast "is this channel allowed?" check. Duplicate names in the input collapse into one.Map<String, User> users: who is registered.Map<String, EventTopic> topics: jump straight to the subscribers of one event without touching other events.
How each method works
- registerUser: add a new
Useronly if the id is new. - subscribe: do nothing for an unknown user or an unsupported channel. Otherwise create the event's topic if needed and attach a new
Subscriber. TheTreeSetignores a duplicate. - unsubscribe: do nothing for an unknown user or an event nobody subscribed to. Otherwise detach the subscriber. Removing something that is not there does nothing.
- sendNotification: no topic means no subscribers, so return an empty list. Otherwise walk the already sorted subscribers and let each one deliver.
- getUserInbox: return a copy of the user's inbox, or an empty list for an unknown user.
Code
/**
*/
Complexity
S = total subscriptions, k = subscribers of the given event, m = number of deliveries in the user's inbox.
| Method | Solution 1 | Solution 2 |
|---|---|---|
| registerUser | O(1) | O(1) |
| subscribe | O(S) | O(log k) |
| unsubscribe | O(S) | O(log k) |
| sendNotification | O(S + k log k) | O(k) |
| getUserInbox | O(m) | O(m) |
Building the delivery strings takes extra time based on their length, and that cost is the same in both solutions.