Design a Real-Time Chat App (like WhatsApp or Slack)
Design a 1-on-1 and group chat system with online presence, read receipts, and offline message synchronization.
Think of a chat system like a walkie-talkie relay tower. When you press speak, your voice goes straight out over an open radio channel to your friend, without waiting for an email reply.
- Users
- Alice (Sender)
- Bob (Receiver)
- Edge WS Fleet
- WS Gateway A
- WS Gateway B
- Message Bus
- Redis Pub/Sub
- Storage
- Cassandra Cluster
1. Requirements and scope
- 1-on-1 and group messaging (up to 500 members).
- Real-time message delivery with online/offline presence tracking.
- Read receipts and push notifications for offline users.
- Low latency: an example target is under 100 ms from sender to receiver.
- No message lost once the server has acknowledged it.
- End-to-end encryption support.
2. Back-of-the-envelope numbers
- Writes: 115,000 messages a second, on average (28%)
- Reads: 300,000 reads a second (72%)
Balanced: reads and writes are close. Both paths need care, so neither can be slow.
3. API
4. Data model and storage
MESSAGES: (channel_id, message_id, sender_id, content, created_at) PRIMARY KEY (channel_id, message_id DESC)
5. The design, in three versions
Version 1: HTTP Polling with Relational DB
Clients poll server every 2 seconds for new messages.
Chat v1: HTTP Polling Server Version 1 of 3
The picture follows one request through the parts of this version, in order. Under it, switch a part off to see what the request does without it.
One request, step by step
Each numbered arrow is one step. Press "Simulate Request Flow" to follow them one at a time.
- The order of the steps
- A request or its reply
- Background work: the answer does not wait for it
The parts: switch one off
1. Users
2. Edge WS Fleet
3. Message Bus
4. Storage
The steps in words0 of 5 steps shown
Alice to WS Gateway A
SEND { to: "Bob", text: "Hi!" }
WS Gateway A to Redis Pub/Sub
Publish to channel:user_bob
Redis Pub/Sub to WS Gateway B
Broadcast to Bob socket
WS Gateway B to Bob
Push message to Bob screen
WS Gateway A to Chat DB
Append to Cassandra message table
Write path, read path and failures
Each path follows one request through the final design (version 3), one hop at a time.
Write pathSend a message
- Sender
- WebSocket server
- Kafka
- Chat history
- Presence store
- Recipient
Sender to WebSocket server
Asha's app sends the message over its open WebSocket connection.
WebSocket server to Kafka
The server gives the message an id and writes it to Kafka, in the partition for this chat. Kafka keeps the chat's messages in order.
WebSocket server to Sender
Once Kafka confirms, the server tells Asha's app "sent".
Kafka to Chat history
A worker saves the message to the chat history in Cassandra.
Kafka to Presence store
A delivery worker looks up which server holds Ben's connection.
WebSocket server to Recipient
That server pushes the message down Ben's connection. If Ben is offline, a push notification goes out.
Read pathOpen a chat and load history
- Recipient
- WebSocket server
- Presence store
- Chat history
Recipient to WebSocket server
Ben opens the app. It connects and sends the id of the last message it has.
WebSocket server to Presence store
The server records that Ben is online and which server holds his connection.
WebSocket server to Chat history
It reads the messages after that id, the newest 50 first.
WebSocket server to Recipient
It sends the missed messages. Older ones load as Ben scrolls up.
What breaks, and what happens then
A WebSocket server crashes
- What users see
- Every user on it is disconnected.
- What the design does
- Apps reconnect to another server and send their last message id. Missed messages are read from history, so none are lost.
The recipient is offline
- What users see
- The message cannot be pushed down a connection.
- What the design does
- It stays in the history. A push notification is sent, and the app fetches the message when it next connects.
A message is sent twice after a retry
- What users see
- The chat could show a duplicate.
- What the design does
- The app puts a unique id on each message. The server and the receiving app drop an id they have already seen.
The presence store is down
- What users see
- Servers cannot find who holds a connection.
- What the design does
- Messages are still saved. Delivery falls back to push notifications and to the fetch on reconnect.
6. Deep dives
7. Follow-up questions
- How do you manage presence for 500M users without overwhelming Redis? (Answer: Clients send heartbeats every 30 seconds; heartbeat failure marks offline after 60s timeout; presence checks are pulled on-demand rather than broadcasted globally).
What it costs
Open connections and message storage drive the cost: millions of WebSocket connections need many gateway servers, and history grows by about 730 TB a year. Media files cost more than text, so store them in object storage, not in the database.
Another way to design it
A small chat product can use long polling and one SQL database. It is far simpler to run. It runs out when users expect instant delivery, or when one database can no longer hold the history.
8. How answers are judged
A passing answer at Mid level. A complete design with numbers: about 115,000 messages a second on average, and 300,000 at peak. It explains how chat history is stored and goes deep on one part, such as ordering or presence.
| What is judged | Junior | Mid | Senior | Staff |
|---|---|---|---|---|
| BreadthHow much of the system does the answer cover? | Names the core parts: clients, servers, a database and a cache. | Covers the whole main flow, from the request to the stored data and back. | Builds a complete design without being led through it. | Covers the system, the systems around it and who owns each part. |
| DepthHow far does it go into the hard parts? | Explains what each part does and why it is there. | Goes deep on one part, such as the database or the cache. | Goes deep on the two or three riskiest parts. | Goes deepest where the risk is largest, and explains why the risk is there. |
| DrivingWho leads the conversation? | Follows the interviewer, step by step. | Moves forward alone, with an occasional nudge. | Leads the conversation from start to finish. | Changes the problem when the question asked is not the right one. |
| Trade-offsAre choices compared and justified? | Names more than one option. | Says why one option was picked over another. | Compares options in detail, with what each one costs. | Ties each choice to cost, speed and risk for the business. |
| Unclear questionsWhat happens when the question is vague? | Answers the questions that are asked. | Asks questions to make the problem clear. | Sets the scope and says what is left out. | Questions the assumptions behind the problem itself. |
| ScopeWhat does it consider beyond the normal path? | Covers the normal path, when everything works. | Adds what happens when a part fails. | Adds growth: what breaks at ten times the load, and the fix. | Adds abuse, cost and the daily work of running the system. |
Requirements and scope
Before I design, I want to check the scope. Chats can be 1-on-1 or groups of up to 500 people. Do offline users need every message once they reconnect? Do we need read receipts and end-to-end encryption? I will assume messages must never be lost, even if delivery is delayed.
Users send a message from their phone, and it goes straight to the other person's phone over a live connection. Let me start sketching the WebSocket servers and the database.
Shows: Unclear questionsThe difference: The weak answer skips whether groups, offline delivery and encryption are in scope, so the design might not fit real chat needs.
Estimates
Ten billion messages a day works out to about 115,000 a second on average, matching the 300,000 peak given. At 200 bytes a message, a year of history is about 730 TB, so Cassandra fits the write pattern better than a relational database.
Ten billion messages a day is a huge number. So the gateway fleet needs to accept about ten billion connections at once, and we should size hardware for that many live sessions.
Shows: DepthThe difference: The weak answer treats the daily total as connections needed at once. The strong one converts it to a rate: about 115,000 messages a second.
Core design
Each message is written to a table keyed by channel ID, then message ID, so I can fetch the newest messages for one channel fast. A cache maps each connected user to the gateway server holding their socket, so a message finds the right server to push through.
I would store every message as a row in one table, with a unique message ID as the key. Recent messages come from querying by that ID.
Shows: DepthThe difference: A table keyed only by message ID cannot fetch one channel's recent messages fast; it needs a scan. Keying by channel ID first fixes that.
Deep dive
The hard part is ordering, since each server's clock drifts, so stamps from many servers can disagree. I would give each channel one sequencer. Kafka is partitioned by channel ID, so one worker owns each channel's messages, and it gives each message the next number for that channel. Clients sort by that number, not by any clock.
Each of the many gateway servers stamps a message with its own clock when it arrives. I sort messages for a channel by those timestamps before showing them to users.
Shows: DepthThe difference: The weak answer trusts many clocks, which drift apart, so a channel can show messages out of order. One sequencer per channel gives one increasing number instead.
Failures and trade-offs
If a gateway server crashes, sockets on it drop, so messages must be written to Kafka before we tell the sender it sent. A worker then delivers each message from Kafka and stores it in Cassandra, so offline users still get it once they reconnect.
If the recipient is offline, we push through Apple or Google's notification service. We keep retrying until it succeeds, so the message always gets through in the end.
Shows: ScopeThe difference: Retrying the push alone does not survive a gateway crash before the message is saved, so it can still be lost. Writing to Kafka first fixes that.
We wrote this guide in our own words, informed by public interview guides such as AlgoMaster and Hello Interview. We are not connected to either site. Interviewers differ. The guide and the example answers show the direction each level takes, not a checklist.
9. One problem, four levels
The same question, "design a chat system", answered at four levels. Each column shows only what that level adds to the one before it.
| Part of the answer | Junior | Mid adds | Senior adds | Staff adds |
|---|---|---|---|---|
| Requirements and scope |
|
|
|
|
| Estimates |
|
|
|
|
| Core design |
|
|
|
|
| Deep dive |
|
|
|
|
| Failures and trade-offs |
|
|
|
|
Requirements and scope
- A working design lets one person message another over a live connection, and shows whether they are online.
- New at MidSplits must-haves (delivery, presence) from qualities: no message may be lost, and group chats can hold up to 500 people.
Estimates
- Says the system must handle far more delivered messages than messages sent, since group chats deliver one message to many members.
- New at MidAbout 115,000 messages a second on average, and 300,000 at peak, with about 730 TB of history stored a year.
Core design
- A live connection per user, an API server, and one table that stores each conversation's messages, with a way to show who is online.
- New at MidMessages are keyed by conversation, then by their own ID, so recent messages for one conversation load without scanning the whole table.
Deep dive
- Explains why a live connection helps: a message reaches the other person right away, without them needing to ask for it.
- New at MidExplains presence: each device sends a heartbeat every 30 seconds, and missing two in a row marks that user offline.
Failures and trade-offs
- Says messages are saved right away, so they are not lost if the app closes before sending finishes.
- New at MidIf a connection drops, the app reconnects and asks for any messages sent while it was away.
10. Check your design with your own AI
Write your design first. Then copy this prompt into the AI chat you already use, and add your design at the end. It asks for a score in each area, one fix for each, and two follow-up questions.
The site sends nothing anywhere. An AI review can be wrong, so check what it says against sections 8 and 9.
See the prompt
You are a system design interviewer. Review my design for the interview problem below. Problem: Design a Real-Time Chat App (like WhatsApp or Slack) Design a 1-on-1 and group chat system with online presence, read receipts, and offline message synchronization. What users must be able to do: - 1-on-1 and group messaging (up to 500 members). - Real-time message delivery with online/offline presence tracking. - Read receipts and push notifications for offline users. How well it must work: - Low latency: an example target is under 100 ms from sender to receiver. - No message lost once the server has acknowledged it. - End-to-end encryption support. Numbers to assume: - 500 Million DAU, 10 Billion messages sent daily (~115,000 messages/sec average). Judge my design at the Mid level. A passing answer at this level: A complete design with numbers: about 115,000 messages a second on average, and 300,000 at peak. It explains how chat history is stored and goes deep on one part, such as ordering or presence. For each area below, say whether my design is strong, fine or weak. Give one reason and one fix for each: 1. Requirements and scope 2. Estimates 3. Core design 4. Deep dive 5. Failures and trade-offs Then ask me the two follow-up questions an interviewer would ask next. Do not write a full model answer for me. My design: [Describe your parts, how one request moves through them, and why you chose each one.]