Bird
Raised Fist0
HLDsystem_design~15 mins

Pub/sub pattern in HLD - Deep Dive

Choose your learning style10 modes available
Overview - Pub/sub pattern
What is it?
The Pub/sub pattern is a way for different parts of a system to communicate by sending and receiving messages without knowing about each other directly. Publishers send messages to a topic or channel, and subscribers listen to those topics to get the messages. This helps systems stay loosely connected and scalable.
Why it matters
Without the Pub/sub pattern, systems would need to know exactly who to talk to, making them tightly linked and hard to change or grow. Pub/sub allows many parts to work independently and react to events in real time, which is essential for modern apps like chat, notifications, or live updates.
Where it fits
Before learning Pub/sub, you should understand basic messaging and client-server communication. After this, you can explore event-driven architectures, message queues, and real-time data streaming systems.
Mental Model
Core Idea
Pub/sub lets senders broadcast messages to many receivers without knowing who they are, enabling flexible and scalable communication.
Think of it like...
Imagine a radio station (publisher) broadcasting music over the airwaves (topic). Anyone with a radio (subscriber) tuned to that station can listen, but the station doesn't know who is listening or how many people are tuned in.
Publisher(s) ──▶ Topic/Channel ──▶ Subscriber(s)

  [Publisher]       [Message Broker]       [Subscriber]
      │                   │                    │
      └── Sends message ──▶│                    │
                          │  Distributes message ──▶
                          │                    │
                          └────────────────────▶
Build-Up - 7 Steps
1
FoundationUnderstanding basic messaging
🤔
Concept: Learn what messages are and how systems send and receive them.
A message is a piece of information sent from one part of a system to another. In simple systems, one sender talks directly to one receiver. For example, a chat app sends your message directly to your friend's device.
Result
You understand that messages carry data and that communication can be direct between two parties.
Knowing what a message is and how it flows is the foundation for understanding more complex communication patterns like Pub/sub.
2
FoundationDirect communication limits
🤔
Concept: Explore why direct sender-to-receiver communication can be limiting.
When a sender must know exactly who to send messages to, the system becomes tightly linked. If you add more receivers or change them, the sender must also change. This makes the system hard to grow or maintain.
Result
You see that direct communication creates dependencies that reduce flexibility.
Understanding these limits shows why a pattern like Pub/sub is needed to decouple parts of a system.
3
IntermediateIntroducing the Pub/sub pattern
🤔Before reading on: do you think publishers know who their subscribers are in Pub/sub? Commit to your answer.
Concept: Learn how Pub/sub decouples senders and receivers using topics or channels.
In Pub/sub, publishers send messages to a topic without knowing who listens. Subscribers express interest in topics and receive messages sent to them. A message broker or event bus manages this distribution.
Result
You understand that publishers and subscribers do not know about each other, enabling loose coupling.
Knowing that Pub/sub decouples communication helps you design systems that are easier to scale and change.
4
IntermediateMessage brokers and delivery
🤔Before reading on: do you think messages in Pub/sub are always delivered instantly and reliably? Commit to your answer.
Concept: Explore the role of message brokers and how messages are delivered to subscribers.
A message broker receives messages from publishers and forwards them to subscribers. Brokers can store messages temporarily, retry delivery, and handle many subscribers. Delivery can be at-most-once, at-least-once, or exactly-once depending on the system.
Result
You see that brokers manage message flow and reliability, which affects system behavior.
Understanding broker roles and delivery guarantees helps you choose the right Pub/sub system for your needs.
5
IntermediateFiltering and topic hierarchies
🤔
Concept: Learn how subscribers can choose which messages to receive using filters and topic structures.
Topics can be organized hierarchically, like sports/football or sports/basketball. Subscribers can listen to specific topics or use patterns to get groups of messages. Filters help reduce unnecessary message processing.
Result
You understand how to control message flow to subscribers efficiently.
Knowing filtering and topic hierarchies improves system performance and subscriber experience.
6
AdvancedScaling Pub/sub systems
🤔Before reading on: do you think a single message broker can handle millions of messages and subscribers easily? Commit to your answer.
Concept: Explore how Pub/sub systems scale to handle large loads and many clients.
To scale, Pub/sub systems use multiple brokers, partition topics, and replicate data. Load balancing and fault tolerance ensure messages reach subscribers even if parts fail. Systems like Kafka or Google Pub/Sub use these techniques.
Result
You see how Pub/sub can support large, distributed systems with high message volumes.
Understanding scaling techniques prepares you to design robust, high-performance Pub/sub architectures.
7
ExpertHandling ordering and consistency
🤔Before reading on: do you think Pub/sub guarantees message order and consistency by default? Commit to your answer.
Concept: Learn about challenges with message order, duplication, and consistency in Pub/sub systems.
Pub/sub systems may deliver messages out of order or more than once. Handling this requires design choices like partition keys, sequence numbers, or idempotent processing. Tradeoffs exist between performance and strict ordering or exactly-once delivery.
Result
You understand the complexity behind reliable and ordered message delivery in Pub/sub.
Knowing these challenges helps you build systems that handle real-world data consistency needs.
Under the Hood
Underneath, a Pub/sub system uses a message broker that acts as a middleman. Publishers send messages to the broker, which stores them temporarily and forwards them to all subscribers interested in the topic. The broker manages connections, message queues, and delivery retries. Internally, it may partition topics and replicate data for scalability and fault tolerance.
Why designed this way?
Pub/sub was designed to decouple senders and receivers to allow independent scaling and evolution. Early systems were tightly coupled and hard to maintain. Using a broker centralizes message management, enabling features like filtering, retries, and load balancing. Alternatives like direct messaging were less flexible and scalable.
┌─────────────┐       ┌───────────────┐       ┌─────────────┐
│  Publisher  │──────▶│ Message Broker │──────▶│ Subscriber  │
└─────────────┘       └───────────────┘       └─────────────┘
       │                     │                      │
       │  Sends message       │  Stores & forwards   │  Receives message
       │────────────────────▶│─────────────────────▶│
       │                     │                      │
Myth Busters - 4 Common Misconceptions
Quick: Do publishers know who their subscribers are in Pub/sub? Commit to yes or no.
Common Belief:Publishers must know exactly who their subscribers are to send messages.
Tap to reveal reality
Reality:Publishers send messages to topics without knowing any subscribers; the broker handles delivery.
Why it matters:Believing otherwise leads to tightly coupled systems that lose the flexibility and scalability benefits of Pub/sub.
Quick: Does Pub/sub guarantee messages arrive in the order they were sent? Commit to yes or no.
Common Belief:Pub/sub always delivers messages in the exact order they were published.
Tap to reveal reality
Reality:Message order is not guaranteed by default; it depends on the system and configuration.
Why it matters:Assuming order can cause bugs in systems that rely on strict sequencing, leading to inconsistent states.
Quick: Are messages in Pub/sub always delivered exactly once? Commit to yes or no.
Common Belief:Pub/sub systems guarantee exactly-once message delivery.
Tap to reveal reality
Reality:Many Pub/sub systems provide at-least-once or at-most-once delivery; exactly-once is hard and requires extra design.
Why it matters:Ignoring this can cause duplicate processing or lost messages, affecting data accuracy.
Quick: Can a single message broker easily handle millions of subscribers and messages? Commit to yes or no.
Common Belief:One message broker can handle unlimited scale without issues.
Tap to reveal reality
Reality:Single brokers have limits; scaling requires partitioning, replication, and multiple brokers.
Why it matters:Overloading a single broker causes delays, message loss, or system crashes.
Expert Zone
1
Some Pub/sub systems allow subscribers to replay past messages, enabling event sourcing and debugging.
2
The choice between push and pull delivery models affects latency, load, and complexity in Pub/sub.
3
Message filtering can be done at the broker or subscriber side, impacting performance and network usage.
When NOT to use
Pub/sub is not ideal when strict request-response communication is needed or when message order and exactly-once delivery are critical without complex handling. Alternatives include direct RPC calls, REST APIs, or transactional message queues.
Production Patterns
In production, Pub/sub is used for event-driven microservices, real-time notifications, log aggregation, and IoT data streaming. Systems often combine Pub/sub with message queues and databases to ensure reliability and consistency.
Connections
Event-driven architecture
Pub/sub is a core building block of event-driven systems.
Understanding Pub/sub helps grasp how events trigger actions across loosely coupled services.
Observer pattern (software design)
Pub/sub is a distributed, scalable form of the Observer pattern.
Knowing the Observer pattern clarifies how Pub/sub manages subscriptions and notifications.
Broadcasting in wireless networks
Both involve sending information to many receivers without direct connections.
Recognizing this connection shows how communication principles apply across technology and physical systems.
Common Pitfalls
#1Assuming message order is guaranteed and designing logic based on that.
Wrong approach:Process messages as they arrive without checking sequence numbers or timestamps.
Correct approach:Include sequence checks or use ordered partitions to ensure correct processing order.
Root cause:Misunderstanding that Pub/sub systems do not always preserve message order.
#2Ignoring message duplication and processing messages multiple times.
Wrong approach:Treat every received message as unique without idempotency.
Correct approach:Design subscribers to handle duplicate messages safely using idempotent operations or deduplication.
Root cause:Belief that Pub/sub guarantees exactly-once delivery.
#3Overloading a single message broker without scaling strategies.
Wrong approach:Deploy one broker instance for all topics and subscribers regardless of load.
Correct approach:Partition topics, replicate brokers, and use load balancing to distribute traffic.
Root cause:Underestimating the scale and resource needs of Pub/sub systems.
Key Takeaways
Pub/sub decouples senders and receivers by using topics and a message broker, enabling scalable and flexible communication.
Publishers do not know who subscribes; subscribers choose which topics to listen to, allowing independent evolution.
Message brokers manage delivery, retries, and filtering but do not always guarantee order or exactly-once delivery.
Scaling Pub/sub requires partitioning, replication, and careful design to handle large volumes and many clients.
Understanding the limits and tradeoffs of Pub/sub helps design reliable, maintainable, and efficient distributed systems.

Practice

(1/5)
1. What is the main purpose of the Pub/Sub pattern in system design?
easy
A. To decouple message senders from receivers for flexible communication
B. To tightly couple components for faster data exchange
C. To store data permanently in a database
D. To encrypt messages for security

Solution

  1. 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.
  2. Step 2: Identify the main benefit

    This separation allows flexible communication and easier scaling of systems.
  3. Final Answer:

    To decouple message senders from receivers for flexible communication -> Option A
  4. Quick Check:

    Decoupling = A [OK]
Hint: Pub/Sub means senders and receivers don't know each other [OK]
Common Mistakes:
  • Thinking Pub/Sub stores data permanently
  • Confusing Pub/Sub with encryption
  • Assuming tight coupling between components
2. Which of the following is the correct component in a Pub/Sub system?
easy
A. Sender, Receiver, Firewall
B. Client, Server, Database
C. Publisher, Subscriber, Message Broker
D. Producer, Consumer, Cache

Solution

  1. Step 1: Recall Pub/Sub components

    Pub/Sub systems have publishers (senders), subscribers (receivers), and a message broker that routes messages.
  2. Step 2: Match components to options

    Only Publisher, Subscriber, Message Broker lists these three correctly.
  3. Final Answer:

    Publisher, Subscriber, Message Broker -> Option C
  4. Quick Check:

    Pub/Sub components = D [OK]
Hint: Remember the broker routes messages between publishers and subscribers [OK]
Common Mistakes:
  • Confusing Pub/Sub with client-server terms
  • Mixing cache or firewall as core components
  • Using generic terms like producer/consumer without broker
3. Consider a Pub/Sub system where a publisher sends messages to a topic. If three subscribers are subscribed to that topic, how many copies of each message does the broker send?
medium
A. One copy shared among all subscribers
B. Three copies, one for each subscriber
C. Zero copies, subscribers pull messages themselves
D. Depends on the message size

Solution

  1. Step 1: Understand message delivery in Pub/Sub

    Each subscriber receives its own copy of the message from the broker to ensure independent processing.
  2. Step 2: Calculate copies sent

    With three subscribers, the broker sends three copies, one per subscriber.
  3. Final Answer:

    Three copies, one for each subscriber -> Option B
  4. Quick Check:

    Copies = Subscribers count = B [OK]
Hint: Broker sends one message per subscriber, not shared [OK]
Common Mistakes:
  • Assuming one copy shared by all subscribers
  • Thinking subscribers pull messages without broker sending
  • Believing message size affects copy count
4. A developer implemented a Pub/Sub system but notices subscribers never receive messages. Which is the most likely cause?
medium
A. Subscribers are using the wrong database
B. Publisher is sending messages too frequently
C. Message broker is encrypting messages
D. Subscribers are not subscribed to the correct topic

Solution

  1. 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.
  2. Step 2: Evaluate other options

    Sending frequency or encryption does not prevent delivery; wrong database is unrelated.
  3. Final Answer:

    Subscribers are not subscribed to the correct topic -> Option D
  4. Quick Check:

    Subscription mismatch = A [OK]
Hint: Check subscriber topic matches publisher topic first [OK]
Common Mistakes:
  • Blaming message frequency for delivery failure
  • Assuming encryption blocks delivery
  • Confusing database usage with Pub/Sub messaging
5. You need to design a scalable notification system using Pub/Sub. Which approach best ensures messages are delivered even if some subscribers are temporarily offline?
hard
A. Use a message broker with persistent storage and durable subscriptions
B. Send messages directly from publisher to subscribers without broker
C. Use a broker that deletes messages immediately after sending
D. Require subscribers to poll the publisher continuously

Solution

  1. Step 1: Identify requirements for offline subscriber support

    To handle offline subscribers, messages must be stored until subscribers can receive them.
  2. Step 2: Choose the right Pub/Sub feature

    Persistent storage and durable subscriptions in the broker ensure messages are saved and delivered later.
  3. Step 3: Evaluate other options

    Direct sending or immediate deletion loses messages; polling is inefficient and not scalable.
  4. Final Answer:

    Use a message broker with persistent storage and durable subscriptions -> Option A
  5. Quick Check:

    Durable subscriptions = reliable delivery = C [OK]
Hint: Durable subscriptions keep messages for offline subscribers [OK]
Common Mistakes:
  • Skipping message persistence in broker
  • Trying direct publisher-to-subscriber without broker
  • Relying on polling instead of push delivery
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard