| Scale | Users/Clients | Messages per Second | System Changes |
|---|---|---|---|
| Small | 100 users | 100-500 msg/s | Single broker, simple topic management, direct message delivery |
| Medium | 10,000 users | 10,000-50,000 msg/s | Multiple brokers, partitioned topics, message persistence, basic load balancing |
| Large | 1,000,000 users | 1M+ msg/s | Clustered brokers, topic sharding, advanced load balancing, message replication, durable storage |
| Very Large | 100,000,000 users | 100M+ msg/s | Geo-distributed clusters, multi-region replication, CDN integration, hierarchical topic routing |
Pub/sub pattern in HLD - Scalability & System Analysis
At small to medium scale, the message broker is the first bottleneck. It handles all message routing and delivery. As users and message rates grow, a single broker's CPU, memory, and network limits are reached quickly.
Also, the network bandwidth between publishers, brokers, and subscribers becomes a bottleneck as message volume increases.
- Horizontal scaling: Add more broker instances and distribute topics among them (partitioning/sharding).
- Load balancing: Use load balancers to distribute client connections evenly across brokers.
- Caching: Use subscriber-side caching or edge caches for frequently requested messages.
- Message persistence: Store messages durably to allow replay and reduce load spikes.
- Geo-distribution: Deploy brokers in multiple regions to reduce latency and network load.
- CDN integration: For static or large messages, use CDNs to offload delivery.
Assuming 10,000 messages per second at medium scale:
- Each message ~1 KB → 10 MB/s bandwidth needed.
- Broker CPU: 1 server can handle ~5,000 msg/s → need 2+ brokers.
- Storage: For message persistence, 10,000 msg/s × 1 KB × 3600 s = ~36 GB/hour.
- Network: 1 Gbps link (~125 MB/s) sufficient for this scale.
Start by explaining the pub/sub components: publishers, subscribers, and brokers.
Discuss how message volume and user count affect broker load and network.
Identify the bottleneck (broker capacity) and propose scaling with partitioning and horizontal scaling.
Mention persistence and geo-distribution for reliability and latency.
Your message broker handles 1,000 messages per second. Traffic grows 10x to 10,000 msg/s. What do you do first?
Answer: Add more broker instances and partition topics to distribute load horizontally, because a single broker cannot handle 10x the load.
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
