Day 25 Exercises
Real-Time Messaging Design
Apply WebSocket connection management, message delivery guarantees, and end-to-end encryption architecture to chat systems.
Message Delivery State Machine
WhatsApp shows single tick (✓), double tick (✓✓), and blue tick for message delivery status. Design the state machine and the events that trigger each transition.
Tasks
- Define the 4 states a message goes through from send to read.
- What network event triggers each state transition?
- How does the sender learn about delivery/read status? (Push vs. poll?)
- How do delivery receipts work when the recipient is offline for 2 hours?
WebSocket Connection Management at Scale
WhatsApp has 2 billion users with 500M daily active. Peak: 100M concurrent WebSocket connections. Each connection uses ~50KB RAM. How do you manage this?
Tasks
- How much RAM do 100M connections require? Is a single server sufficient?
- Design the connection routing — how does Server A know a message should go to Server B?
- How do you handle WebSocket connection keepalives at scale?
- What happens to queued messages when a user's phone battery dies?
End-to-End Encryption Key Exchange
WhatsApp uses Signal Protocol for E2E encryption. Design the key exchange that allows Alice and Bob to establish a shared secret without the server seeing the plaintext.
Tasks
- What is the "Double Ratchet" algorithm and why is it better than a static shared key?
- What are "prekeys" and why does WhatsApp pre-upload them to the server?
- If Bob is offline when Alice sends a message, how is the message delivered securely?
- What does "forward secrecy" mean, and why does it matter if Alice's device is stolen later?
Group Messaging at Scale
WhatsApp has groups of up to 1,024 members. A message sent to a group with 500 members must be delivered to all 500 within 2 seconds. Design the fan-out.
Tasks
- Is server-side fan-out (server sends to all 500) or client-side fan-out better for E2E encryption in groups?
- Design the fan-out architecture for a 500-member group message.
- How do you handle members who are offline? How do you limit the offline queue?
- Why is "sender key" used in group E2E encryption instead of individual keys per recipient?