Design a chat app (like WhatsApp)
One-to-one and group messaging, with delivery receipts and online status.
Why interviewers ask it
It tests real-time connections, routing between servers, storage for enormous write volumes, ordering and offline delivery.
Functional requirements
- Send and receive messages in real time, one-to-one and in groups.
- Message history across devices; delivery to users who were offline.
- Sent, delivered and read receipts; online and last-seen status.
Non-functional requirements
- Low latency (under about 200 ms).
- Messages are never lost, and stay in order within a conversation.
- Billions of messages a day.
From version 1 to the final design
- Version 1: one server; clients poll a database for new messages.
- Version 2: WebSockets to that server, pushing instead of polling.
- Version 3: many gateway servers plus a registry of who is connected where, with messages routed between gateways.
- Version 4: a partitioned message store, push notifications for offline users, receipts, presence and groups.
The design, step by step
- Each client keeps a WebSocket connection to a chat gateway server.
- Sending: gateway → chat service gives the message a time-ordered ID and stores it, then routes it.
- Routing: look up the recipient's gateway in Redis and push to it; if they're offline, keep it stored and send a push notification.
- Groups: send a copy to each member (small groups), or have members fetch it (very large groups).
- Receipts and presence are small events that travel the same path.
Deep dives
- Ordering: a sequence number per conversation; clients sort by it.
- Retries without duplicates: the client creates a message ID, so resending is idempotent.
- Presence: heartbeats with a TTL in Redis; don't broadcast every status change to everyone.
- End-to-end encryption: the server only ever stores ciphertext.
Common mistakes
- Polling for new messages at scale: millions of clients asking "anything new?" every few seconds, mostly for nothing.
- Forgetting offline users: their messages must be stored and delivered when they reconnect.
- Having no plan for message order or duplicates: retries and several servers can deliver a message twice, or out of order.
Not true: Delivery: clients poll every few seconds, which scales well to millions of users.
In fact: Almost every poll would come back empty, yet each one costs a request. Keep a WebSocket open to each client, and push messages as they arrive.
In the interview
Clients hold WebSocket connections to gateway servers, and Redis maps each user to their gateway. The chat service stores every message in a Cassandra-style store, partitioned by conversation, with a time-ordered ID. Then it routes the message to the recipient's gateway. If they're offline, it keeps the message and sends a push notification. Client-generated message IDs make retries safe.
Part of Unit 8: High-level design questions in the free System Design course.