Design a Chat App (WhatsApp)
Design one-to-one and group messaging with persistent connections, message storage, delivery receipts, presence and offline delivery.
Advanced · about 5 hours · difficulty 4/5
Learn first: HTTP, Connections and Real-Time Protocols, NoSQL Data Models and When to Use Them, Design a Notification System
WhatsApp serves billions of messages a day with famously small teams. Designing it tests real-time connections, storage at scale and delivery guarantees.
Requirements
- One-to-one and group chats (groups up to a few hundred or thousand members), text and media.
- Sent, delivered and read receipts; online and last seen presence.
- Offline users receive messages when they reconnect; message order preserved per chat.
- Low latency, very high availability, end-to-end encryption.
Connections
Clients keep a persistent WebSocket (or MQTT-style) connection to a chat server. A session service (Redis) maps user_id to the chat server holding their connection. Millions of connections are spread across many servers; servers are stateful, so use consistent routing and handle reconnects gracefully.
Message flow (one-to-one)
- Sender's chat server receives the message, assigns a message ID (time-ordered per conversation) and persists it.
- It acks the sender (single tick).
- It looks up the recipient's chat server and forwards the message; if the recipient is offline, it stays in their pending inbox and a push notification is sent.
- The recipient's client acks delivery (double tick) and later read (blue ticks); these acks flow back as small events.
Storage
Messages are write-heavy and read by conversation and time, a natural fit for a wide-column store partitioned by conversation_id and clustered by message ID (Cassandra, ScyllaDB, HBase). Media goes to object storage via pre-signed URLs and a CDN; the message carries only a reference.
Groups and presence
Small groups: fan out a copy (or a pointer) to each member's inbox on write. Large groups and channels: store once and let members pull. Presence uses heartbeats with a TTL in Redis; broadcast changes only to contacts who are online, and batch them.
Pitfalls
- Using HTTP polling for chat at this scale.
- Global ordering across all chats; you only need order per conversation.
- Forgetting idempotent message IDs, so client retries create duplicates.
You are ready to move on when you can trace a message from sender to an offline recipient and back to blue ticks, naming every component and store it touches.
Key concepts
- WebSocket connection servers
- Session registry (user to server)
- Message IDs and per-chat ordering
- Delivery and read receipts
- Offline inbox and push
- Wide-column message storage
- Group fan-out
- Presence with heartbeats
- End-to-end encryption basics
Mini project
Build a chat prototype: two WebSocket servers, Redis for the session registry and pub/sub between servers, Postgres or Cassandra for messages, and sent/delivered/read receipts. Show a message reaching a user connected to the other server and an offline user receiving it on reconnect.
Check yourself
User A is connected to chat server 3 and user B to chat server 7. How does A's message reach B, and what happens if B's server crashes mid-delivery?
Free resources
- How Discord stores trillions of messages (Discord)
- The WebSocket API (MDN)
- High Scalability (High Scalability)