What if your system could tell everyone exactly when something important changes, all by itself?
Why Notification on state change in LLD? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have a big team working on a project, and everyone needs to know when a task changes status. You try to call or message each person every time something updates.
It quickly becomes overwhelming and confusing.
Manually informing everyone is slow and easy to forget. People miss updates or get too many messages at once. It wastes time and causes mistakes.
Notification on state change automates this. When a task changes, the system sends alerts only to the right people instantly. No one is left out or spammed.
if task.status_changed: for user in team: send_message(user, 'Task updated')
task.on_status_change(lambda new_status: notify_subscribers(new_status))
This lets teams stay updated in real time without extra effort, improving coordination and speed.
In a delivery app, when a package status changes from 'shipped' to 'out for delivery', the customer and driver get notified immediately.
Manual updates are slow and error-prone.
Automated notifications send timely alerts to the right people.
This improves communication and efficiency in any system.
Practice
Solution
Step 1: Understand the role of notifications
Notifications alert parts of a system or users when something important changes.Step 2: Identify the purpose of state change notifications
They ensure components get updates immediately without waiting or manual refresh.Final Answer:
To inform interested components immediately when data changes -> Option AQuick Check:
Notification = Immediate update [OK]
- Confusing notification with data storage
- Thinking notifications delay updates
- Assuming notifications increase app size
Solution
Step 1: Recall observer pattern methods
Common methods include subscribe, unsubscribe, and notifyObservers.Step 2: Identify the method that sends updates
notifyObservers() is used to alert all subscribed observers about changes.Final Answer:
notifyObservers() -> Option DQuick Check:
Notify method = notifyObservers() [OK]
- Confusing subscribe with notify
- Using updateState() which changes state, not notify
- Mixing unsubscribe with notification
class Subject:
def __init__(self):
self.observers = []
def subscribe(self, observer):
self.observers.append(observer)
def notify(self, message):
for obs in self.observers:
obs.update(message)
class Observer:
def update(self, message):
print(f"Received: {message}")
subject = Subject()
obs1 = Observer()
subject.subscribe(obs1)
subject.notify("State changed")
What will be the output when subject.notify("State changed") is called?Solution
Step 1: Trace subscription and notification
Observer obs1 is subscribed to subject, so it is in the observers list.Step 2: Check notify method behavior
notify calls update on each observer with the message "State changed".Final Answer:
Received: State changed -> Option CQuick Check:
Observer prints message on notify [OK]
- Assuming notify does nothing without explicit call
- Thinking update method is missing
- Expecting no output if observers list is empty
class Subject:
def __init__(self):
self.observers = set()
def subscribe(self, observer):
self.observers.add(observer)
def notify(self, message):
for obs in self.observers:
obs.receive(message)
class Observer:
def update(self, message):
print(f"Got: {message}")
subject = Subject()
obs1 = Observer()
subject.subscribe(obs1)
subject.notify("Update")Solution
Step 1: Check method called in notify
notify() calls obs.receive(message).Step 2: Check Observer class methods
Observer defines update(message), but no receive() method.Final Answer:
Method notify calls obs.receive but Observer has update method -> Option BQuick Check:
Method name mismatch causes AttributeError [OK]
- Confusing set vs list for storing observers
- Ignoring method name mismatches
- Assuming missing class when it exists
Solution
Step 1: Understand scalability needs
Thousands of subscribers require non-blocking, efficient notification delivery.Step 2: Evaluate design options
Synchronous loops block main process; polling adds delay; restart notifications are impractical.Step 3: Identify best practice
Asynchronous message queues decouple notification sending, allowing scalable, fast delivery.Final Answer:
Use asynchronous message queues to dispatch notifications -> Option AQuick Check:
Async queues = scalable notifications [OK]
- Using synchronous loops causing delays
- Relying on polling which is slow
- Sending notifications only on restart
