What if you could send a message once and have it instantly reach only those who care, without lifting the phone?
Why Pub/sub pattern in HLD? - Purpose & Use Cases
Imagine a busy office where every employee must call each other individually to share updates. If one person has news, they must dial every colleague's number and explain it one by one.
This manual calling is slow, tiring, and easy to forget someone. Important messages get delayed or lost, and the office becomes chaotic with repeated calls and confusion.
The Pub/sub pattern acts like a central bulletin board where anyone can post messages, and those interested can read them instantly. No need to call everyone; messages reach the right people automatically.
for subscriber in subscribers: subscriber.receive(message)
subscribers = broker.get_subscribers(topic) publisher.publish(topic, message)
It enables efficient, scalable communication where senders and receivers stay loosely connected, making systems faster and easier to manage.
News apps use pub/sub to send breaking news alerts only to users interested in specific topics, without bothering everyone with all updates.
Manual direct messaging is slow and error-prone.
Pub/sub centralizes message delivery for efficiency.
It supports scalable and flexible communication.
Practice
Pub/Sub pattern in system design?Solution
Step 1: Understand the role of Pub/Sub
Pub/Sub pattern separates the sender (publisher) and receiver (subscriber) so they do not depend on each other directly.Step 2: Identify the main benefit
This separation allows flexible communication and easier scaling of systems.Final Answer:
To decouple message senders from receivers for flexible communication -> Option AQuick Check:
Decoupling = A [OK]
- Thinking Pub/Sub stores data permanently
- Confusing Pub/Sub with encryption
- Assuming tight coupling between components
Solution
Step 1: Recall Pub/Sub components
Pub/Sub systems have publishers (senders), subscribers (receivers), and a message broker that routes messages.Step 2: Match components to options
Only Publisher, Subscriber, Message Broker lists these three correctly.Final Answer:
Publisher, Subscriber, Message Broker -> Option CQuick Check:
Pub/Sub components = D [OK]
- Confusing Pub/Sub with client-server terms
- Mixing cache or firewall as core components
- Using generic terms like producer/consumer without broker
Solution
Step 1: Understand message delivery in Pub/Sub
Each subscriber receives its own copy of the message from the broker to ensure independent processing.Step 2: Calculate copies sent
With three subscribers, the broker sends three copies, one per subscriber.Final Answer:
Three copies, one for each subscriber -> Option BQuick Check:
Copies = Subscribers count = B [OK]
- Assuming one copy shared by all subscribers
- Thinking subscribers pull messages without broker sending
- Believing message size affects copy count
Solution
Step 1: Analyze why subscribers get no messages
If subscribers never receive messages, a common cause is they are not subscribed to the topic where messages are published.Step 2: Evaluate other options
Sending frequency or encryption does not prevent delivery; wrong database is unrelated.Final Answer:
Subscribers are not subscribed to the correct topic -> Option DQuick Check:
Subscription mismatch = A [OK]
- Blaming message frequency for delivery failure
- Assuming encryption blocks delivery
- Confusing database usage with Pub/Sub messaging
Solution
Step 1: Identify requirements for offline subscriber support
To handle offline subscribers, messages must be stored until subscribers can receive them.Step 2: Choose the right Pub/Sub feature
Persistent storage and durable subscriptions in the broker ensure messages are saved and delivered later.Step 3: Evaluate other options
Direct sending or immediate deletion loses messages; polling is inefficient and not scalable.Final Answer:
Use a message broker with persistent storage and durable subscriptions -> Option AQuick Check:
Durable subscriptions = reliable delivery = C [OK]
- Skipping message persistence in broker
- Trying direct publisher-to-subscriber without broker
- Relying on polling instead of push delivery
Ready to prove it?
Pass the test and this pattern moves unknown
