Design WhatsApp 1:1 Messaging
Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2025-04-30
Problem Statement
Design the 1:1 messaging component of WhatsApp that allows two users to exchange messages in real time with high reliability, low latency, and strong security guarantees like end-to-end encryption.
🎯 Functional Requirements:
1.Send and Receive Messages
•Text messages (initial scope)
•Support emojis, links, etc.
2.Real-Time Delivery
•Instant delivery when both users are online
•Queue & retry for offline users
3.Message Acknowledgements
•Sent ✓, Delivered ✓✓, Read ✓✓ blue
4.End-to-End Encryption
•Messages are encrypted before leaving the sender's device and decrypted only on the receiver's device
5.Message Sync
•Deliver same message across multiple devices (if multi-device support exists)
6.Message Persistence
•Store messages for a limited duration (e.g., till delivered)
•Support for optional cloud backups (future scope)
7.Delete for Me / Delete for Everyone (optional)
⚙️ Non-Functional Requirements:
•Latency: ~100 ms or lower
•High Availability (99.99%)
•Horizontally Scalable
•Reliable Message Delivery (even across spotty networks)
•Secure (E2E encryption, auth, replay protection)
•Idempotency (avoid duplicate messages)
🧱 Key Components:
1.Client (Mobile/Web):
•Encrypt/decrypt messages (using Signal Protocol)
•Maintain local chat history
•Show message status (✓, ✓✓)
2.API Gateway:
•Auth, rate-limiting, load balancing
3.Message Service:
•Receive, route, and queue messages
•Store undelivered messages (message store)
•Handle retries and acknowledgments
4.Delivery Queue / Broker:
•Kafka / Redis pub-sub to decouple sender and receiver
•Per-user message topic or queue
5.Presence Service:
•Track user online/offline state
•Optimize delivery paths
6.Acknowledgement Service:
•Track sent/delivered/read receipts
•Push updates back to sender
7.Encryption Service:
•On client side using Signal Protocol
•Server stores encrypted blobs only
8.Storage:
•Temporary store (Redis/Cassandra) for undelivered messages
•Persistent DB for delivery logs, status, metadata
🗂️ Data Models (Simplified):
Message
1json
2CopyEdit
3{
4 "message_id": "uuid",
5 "sender_id": "user123",
6 "receiver_id": "user456",
7 "timestamp": 1723453200,
8 "status": "SENT | DELIVERED | READ",
9 "content": "<encrypted_blob>"
10}🔁 Message Flow Sequence:
1.User A sends a message from client:
•Message is encrypted locally
•Sent to server with metadata
2.Server receives message:
•Stores it in a delivery queue
•If User B is online, pushes via WebSocket
•If offline, stores in undelivered store (e.g., Redis)
3.User B receives the message:
•Decrypts on device
•Sends "DELIVERED" and then "READ" ack back
4.Server updates status:
•Notifies User A of delivery/read receipts
🔐 Security and Encryption:
•Signal Protocol for E2E encryption (X3DH + Double Ratchet)
•Server cannot read message contents
•Use message IDs + timestamps to prevent replay
🧠 Scalability Considerations:
•Use sharding by user ID to distribute message queues
•Horizontal scaling for message brokers (Kafka/Redis Streams)
•Stateless services wherever possible
•WebSocket fanout layer with device mapping