Design WhatsApp: System Design Interview Guide
Master WhatsApp system design for interviews.
20+ years shipping production code across the stack, with years spent interviewing engineers. Notes here come from systems that actually shipped.
- ✓Basic understanding of distributed systems (CAP theorem, consistency models).
- ✓Familiarity with WebSocket, REST, and message queues (Kafka).
- ✓Knowledge of NoSQL databases (Cassandra) and caching (Redis).
- ✓Understanding of load balancing and horizontal scaling.
- WhatsApp is a real-time messaging app requiring high availability, low latency, and end-to-end encryption.
- Core components: chat servers, presence service, message store, media storage, and notification system.
- Key challenges: handling millions of concurrent connections, message ordering, and delivery guarantees.
- Use WebSocket for real-time communication, Cassandra for message storage, and CDN for media.
- Scale with horizontal sharding, load balancing, and asynchronous processing.
Imagine a postal system where letters are delivered instantly. WhatsApp is like a super-efficient post office that handles billions of letters every day. Each letter is encrypted so only the sender and receiver can read it. The system has many post offices (servers) around the world to ensure fast delivery. If one post office is busy, another takes over. Messages are stored temporarily until delivered, and you can see when someone is typing or online, like a live tracking system for letters.
| Chrome | Firefox | Safari | Edge |
|---|---|---|---|
| ✓ | ✓ | ✓ | ✓ |
WhatsApp is one of the most popular messaging apps globally, handling over 100 billion messages daily. Designing a system like WhatsApp is a classic system design interview question that tests your ability to build a real-time, scalable, and fault-tolerant application. In this guide, we'll walk through the key components, trade-offs, and interview strategies to ace this question.
Imagine you're asked: 'Design WhatsApp' in a 45-minute interview. The interviewer expects you to cover functional requirements (one-on-one chat, group chat, media sharing, online presence) and non-functional requirements (low latency, high availability, consistency, security). You'll need to propose a high-level architecture, dive into data models, discuss scaling strategies, and handle edge cases like offline messages or message ordering.
We'll start with requirements, then move to a deep dive on each component: WebSocket servers for real-time communication, Cassandra for message storage, Redis for presence, and CDN for media. We'll also cover end-to-end encryption, message delivery guarantees (at-least-once), and how to handle billions of users. By the end, you'll have a solid blueprint to impress any interviewer.
1. Requirements and Goals
Start by clarifying functional and non-functional requirements. Functional: send/receive messages (text, image, video), group chat (up to 256 users), online presence, delivery receipts (sent, delivered, read), media sharing, end-to-end encryption. Non-functional: low latency (<100ms for message delivery), high availability (99.999%), scalability (billions of users), consistency (messages ordered per conversation), security (encryption). Also consider offline messaging, multi-device support, and voice/video calls (optional).
2. High-Level Architecture
The architecture consists of clients (mobile/web), load balancers, chat servers (WebSocket), presence servers (Redis), message queues (Kafka), databases (Cassandra for messages, MySQL for user metadata), media storage (S3/CDN), and notification servers (APNS/FCM). Clients connect via WebSocket to chat servers for real-time messaging. Messages are persisted in Cassandra for durability. Presence is tracked in Redis with TTL. Media is uploaded to S3 and served via CDN. Notifications are sent via push when app is backgrounded.
3. Data Model
Key entities: User (userId, phoneNumber, displayName, avatarUrl), Message (messageId, conversationId, senderId, content, timestamp, type), Conversation (conversationId, participants, lastMessage, lastTimestamp), Media (mediaId, url, type, size). For one-on-one chat, conversationId = sorted(userId1, userId2) to ensure uniqueness. For group chat, conversationId is a UUID. Messages are stored in Cassandra with partition key = conversationId and clustering key = timestamp (for ordering). Example schema:
CREATE TABLE messages ( conversation_id text, message_id timeuuid, sender_id text, content text, media_url text, created_at timestamp, PRIMARY KEY (conversation_id, created_at, message_id) ) WITH CLUSTERING ORDER BY (created_at DESC);
4. Real-Time Messaging Flow
When user A sends a message to user B: 1) Client A sends message via WebSocket to chat server. 2) Chat server assigns a unique message ID (timeUUID) and stores message in Cassandra. 3) Chat server pushes message to user B's WebSocket if B is online. 4) If B is offline, message is stored and later delivered via push notification. 5) Chat server sends acknowledgment back to A (sent receipt). 6) When B receives message, client sends delivery receipt. 7) When B reads message, read receipt is sent. All receipts are stored as special message types. For group chat, the server fans out messages to all group members (up to 256). For large groups, use multicast or separate queues.
5. Presence and Online Status
Presence is tracked using Redis with a key 'presence:{userId}' and TTL (e.g., 30 seconds). Clients send periodic heartbeats (every 10 seconds) to refresh TTL. When a user connects, set presence to 'online'. When they disconnect (WebSocket close), set to 'last seen' timestamp. For privacy, users can configure who sees their status. Presence updates are broadcast to friends' online sessions via WebSocket. To scale, use Redis Cluster with sharding by userId. For millions of users, use Redis with read replicas and local caching on chat servers.
6. Media Sharing and Storage
Media (images, videos, documents) is uploaded directly to a CDN or blob store (e.g., S3) from the client. The client gets a pre-signed URL for upload. After upload, the media URL is sent as a message. For thumbnails, generate on the server side. Use CDN for fast global delivery. For large files, support chunked upload and resume. Media is encrypted at rest and in transit. To reduce storage costs, deduplicate media using hash (SHA256). For interview, mention using S3 with CloudFront CDN.
7. End-to-End Encryption
WhatsApp uses the Signal Protocol for end-to-end encryption. Each user has a public/private key pair. When two users chat, they exchange public keys and establish a session key using the Double Ratchet algorithm. Messages are encrypted with AES-256. The server never has access to plaintext. For group chats, each message is encrypted per recipient (sender keys). In an interview, you don't need to implement the protocol, but explain the concept: keys are stored on client, server relays encrypted messages. Mention key distribution and verification (safety numbers).
8. Scaling and Performance
To scale to billions of users: 1) Horizontal scaling of chat servers with load balancers (e.g., HAProxy). 2) Shard databases: Cassandra partitions by conversationId; use consistent hashing. 3) Use CDN for media. 4) Asynchronous processing with Kafka for message delivery, presence updates, and analytics. 5) Caching: Redis for presence and session data; local caches for user metadata. 6) Geo-distributed data centers to reduce latency. 7) Use connection pooling and multiplexing on WebSocket. 8) For group chat fan-out, use a message broker with topic-based routing.
9. Fault Tolerance and Reliability
Ensure no message loss: 1) Persist messages before acknowledging. 2) Use replication in Cassandra (RF=3). 3) Kafka provides durability with replication. 4) WebSocket reconnection with exponential backoff. 5) Idempotent message delivery: clients deduplicate using messageId. 6) Graceful degradation: if database is slow, queue messages and deliver later. 7) Circuit breakers for external services (e.g., push notifications). 8) Regular backups and disaster recovery across regions.
10. Interview Tips and Common Pitfalls
Common mistakes: 1) Not clarifying requirements (e.g., group chat size). 2) Ignoring offline messaging. 3) Overcomplicating the design (e.g., microservices for everything). 4) Forgetting about encryption. 5) Not discussing trade-offs (e.g., consistency vs availability). Tips: 1) Start with a simple design and iterate. 2) Use whiteboard to draw architecture. 3) Mention numbers (e.g., 100B messages/day). 4) Discuss bottlenecks and how to address them. 5) Show you can think about edge cases (e.g., user blocking, message recall).
WhatsApp Outage: The DNS Hijack That Took Down Messaging
- Network infrastructure failures can cascade globally; design for network diversity.
- Monitor BGP routes and have automated failover mechanisms.
- Use multiple ISPs and peering agreements to avoid single points of failure.
- Implement graceful degradation: show cached messages when servers are unreachable.
- Conduct regular chaos engineering to test network resilience.
ping websocket-server:443tail -f /var/log/chat-server/error.log| File | Command / Code | Purpose |
|---|---|---|
| schema.cql | CREATE TABLE messages ( | 3. Data Model |
| message_flow.py | class ChatServer: | 4. Real-Time Messaging Flow |
| presence.py | class PresenceService: | 5. Presence and Online Status |
Key takeaways
Common mistakes to avoid
5 patternsNot considering offline messaging
Using SQL database for messages
Ignoring delivery receipts
Overcomplicating with microservices
Forgetting about security and encryption
Interview Questions on This Topic
Design WhatsApp's messaging system. How would you ensure messages are delivered exactly once?
Frequently Asked Questions
20+ years shipping production code across the stack, with years spent interviewing engineers. Notes here come from systems that actually shipped.
That's System Design Interview. Mark it forged?
3 min read · try the examples if you haven't