System Design Problems
Design WhatsApp
WhatsApp serves 2B+ users with 100B+ messages daily. This design explores building a globally distributed messaging platform with end-to-end encryption, media delivery, and real-time presence.
- Scale â 2B users, 100B messages/day, 50M messages/second peak
- Latency â Message delivery under 100ms for 95% of messages
- Encryption â End-to-end encryption using Signal Protocol
Designing for WhatsApp means solving the hardest problems in distributed messaging at planetary scale.
Requirements Clarification
Functional Requirements
- One-to-one text messaging with delivery/read receipts
- Group messaging (up to 1024 members)
- Media sharing (images, videos, documents up to 2GB)
- Online/offline presence indicators
- Message history and synchronization across devices
- End-to-end encryption (E2EE)
Non-Functional Requirements
- Availability: 99.99% uptime
- Latency: < 100ms for message delivery (P99)
- Durability: Messages must not be lost once sent
- Consistency: Causal ordering within conversations
- Scale: 2B registered users, 500M daily active users
Back-of-the-Envelope Estimation
High-Level Architecture
Core Components Deep Dive
1. Connection Manager
Maintains persistent WebSocket connections with clients:
class ConnectionManager:
def __init__(self):
self.user_connections = {} # user_id -> {device_id: server_id}
self.server_connections = {} # server_id -> {connection_id: user_id}
def register(self, user_id, device_id, server_id):
self.user_connections[user_id][device_id] = server_id
self.server_connections[server_id][connection_id] = user_id
def get_servers(self, user_id):
return set(self.user_connections.get(user_id, {}).values())
2. Message Router
Routes messages between senders and receivers:
3. Message Flow (1:1 Chat)
4. End-to-End Encryption
5. Group Messaging
6. Presence System
Data Model
Scaling Strategies
Message Storage Partitioning
Messages are partitioned by chat_id using consistent hashing:
Push vs Pull for Delivery
Practice Exercises
-
Design: How would you implement message synchronization across multiple devices for the same user? Consider ordering and conflict resolution.
-
Scale: WhatsApp has 50M concurrent users. Estimate the number of WebSocket connections needed and the memory overhead per connection.
-
Reliability: Design a mechanism to ensure messages are never lost, even if the sender's device crashes after sending but before receiving acknowledgment.
-
Optimization: How would you reduce bandwidth usage for users on slow networks? Propose a compression strategy for text and media.
What to Learn Next
-> Design Instagram Photo sharing, feeds, and media delivery at scale.
-> Design Twitter Real-time feeds, fan-out, and timeline generation.
-> Design YouTube Video streaming, transcoding, and CDN delivery.
-> Design Netflix Content delivery, recommendation, and adaptive streaming.
-> Circuit Breaker Pattern Preventing cascade failures in distributed systems.
-> Back Pressure Managing load in message-driven architectures.