| Users / Messages | 100 users | 10K users | 1M users | 100M users |
|---|---|---|---|---|
| Message volume per second | ~100 msg/s | ~10K msg/s | ~1M msg/s | ~100M msg/s |
| System components | Single broker, single DB | Multiple brokers, DB replicas | Partitioned brokers, sharded DB | Global distributed brokers, multi-region DB shards |
| Delivery guarantee complexity | Simple ack, retry | Idempotency, deduplication | Exactly-once semantics, ordering | Cross-region consistency, geo-replication |
| Latency | Low, <100ms | Moderate, 100-200ms | Higher, 200-500ms | Variable, 500ms+ |
| Failure handling | Basic retry | Dead-letter queues, monitoring | Advanced retry policies, transactional logs | Multi-region failover, disaster recovery |
Message delivery guarantees in HLD - Scalability & System Analysis
Start learning this pattern below
Jump into concepts and practice - no test required
The first bottleneck is the message broker's throughput and storage capacity. At low scale, a single broker handles message delivery with simple acknowledgments. As users grow, the broker's ability to process and persist messages reliably becomes limited. The database or storage system backing the broker also becomes a bottleneck due to write throughput and consistency requirements for delivery guarantees.
- Horizontal scaling: Add more message brokers and partition topics to distribute load.
- Replication: Use broker clusters with replication for fault tolerance and availability.
- Caching: Use in-memory caches for quick message state checks to reduce DB load.
- Sharding: Partition databases by user or topic to scale storage and throughput.
- Idempotency and deduplication: Implement to ensure exactly-once delivery despite retries.
- Dead-letter queues: Handle undeliverable messages separately to avoid blocking.
- Geo-distribution: Deploy brokers and storage in multiple regions for latency and disaster recovery.
- At 10K messages/sec, a single broker can handle ~5K-10K msg/sec, so 2 brokers needed.
- Database write throughput must support message persistence; ~10K writes/sec at 10K msg/sec.
- Storage grows with message retention; 10K msg/sec * 86,400 sec/day = ~864M messages/day.
- Network bandwidth: 10K msg/sec * average message size (e.g., 1KB) = ~10MB/s (~80Mbps).
- At 1M msg/sec, partitioning and sharding are mandatory; storage and network scale accordingly.
Start by clarifying the delivery guarantees needed: at-most-once, at-least-once, or exactly-once. Discuss how these affect system design and complexity. Then, outline scaling steps from single broker to distributed clusters, focusing on bottlenecks and solutions. Use real numbers to justify design choices and show understanding of trade-offs.
Your database handles 1000 QPS. Traffic grows 10x to 10,000 QPS. What do you do first?
Answer: Add read replicas and implement caching to reduce load on the primary database. Also, consider sharding the database to distribute writes and scale horizontally.
Practice
Solution
Step 1: Understand 'At most once' guarantee
This guarantee means messages can be lost but never duplicated.Step 2: Compare with other guarantees
'At least once' may duplicate messages, 'Exactly once' avoids loss and duplication but is complex.Final Answer:
At most once -> Option BQuick Check:
At most once = no duplicates, possible loss [OK]
- Confusing 'At most once' with 'At least once'
- Thinking 'Exactly once' is simple
- Assuming 'At most once' never loses messages
Solution
Step 1: Define 'Exactly once' guarantee
This guarantee ensures each message is delivered once, no loss, no duplication.Step 2: Eliminate other options
'Messages may be lost but never duplicated' is 'At most once', 'Messages may be duplicated but never lost' and 'delivered at least once, possibly duplicated' describe 'At least once'.Final Answer:
Messages are delivered once without loss or duplication -> Option AQuick Check:
Exactly once = no loss, no duplicates [OK]
- Mixing 'Exactly once' with 'At least once'
- Believing 'Exactly once' allows duplicates
- Confusing 'At most once' with 'Exactly once'
Solution
Step 1: Understand 'At least once' behavior on receiver crash
If receiver crashes before ack, sender retries, causing possible duplicates.Step 2: Analyze options
The message is not lost but sender retries on no ack, so may be delivered multiple times to ensure at least once.Final Answer:
The message may be delivered multiple times -> Option DQuick Check:
At least once = possible duplicates [OK]
- Assuming message is lost on crash
- Confusing 'At least once' with 'At most once'
- Ignoring possibility of duplicates
Solution
Step 1: Identify cause of duplicates in 'Exactly once'
Duplicates usually happen if 'At least once' is used without deduplication.Step 2: Evaluate other options
'At most once' causes losses not duplicates, improper ack handling or dropping messages cause losses or retries but exactly once needs deduplication atop at least once.Final Answer:
The system uses 'At least once' delivery without deduplication -> Option AQuick Check:
Duplicates mean missing deduplication in 'At least once' [OK]
- Assuming 'At most once' causes duplicates
- Ignoring deduplication step
- Confusing message loss with duplication
Solution
Step 1: Identify system requirements
Payment processing must avoid losing or duplicating transactions.Step 2: Match guarantee to requirements
'Exactly once' ensures messages are delivered once without loss or duplication, fitting the need.Final Answer:
Exactly once, because it guarantees no loss and no duplicates -> Option CQuick Check:
Payment systems need exactly once delivery [OK]
- Choosing 'At most once' and risking loss
- Choosing 'At least once' and risking duplicates
- Ignoring delivery guarantees in critical systems
