GuidesDesign a chat app
Design a chat application: a system design interview walkthrough
11 min read · Updated 1 December 2026
You type "running 10 minutes late" and press send. Half a second later your friend's phone buzzes, and a moment after that you see two ticks: delivered.
Most web apps work by asking. The browser sends a request, the server answers, and the conversation ends. Chat breaks that pattern. The server has to reach out to your friend the instant a message arrives, even though your friend never asked for anything.
So a chat interview is about a few hard questions. How do you keep millions of phones connected? How does the server find the right connection for one person? What happens when that person is offline, or the server holding their connection crashes? This guide answers them in the order you would in an interview, for an app in the style of WhatsApp or Messenger.
Key takeaways
- Keep a long-lived WebSocket per device on a fleet of connection servers; everything else stays stateless.
- A presence store maps each user to the connection server holding their socket.
- Give every message an ID that is ordered within its conversation, and store messages partitioned by conversation.
- Save the message first, then deliver it; offline users get a push notification and catch up on reconnect.
- Small groups fan out on write; very large groups need a different approach.
Step 1: Pin down what you are building
Agree on scope first:
- Must have: one-to-one messages, group chats (up to a few hundred members), delivery to users who are offline, message history on every device.
- Nice to have: online and last-seen status, delivered and read receipts, typing indicators.
- Out of scope: voice and video calls, payments, stories. Mention that end-to-end encryption changes what the server can see, and park it unless the interviewer wants it.
Then the qualities. Messages must arrive quickly (under about half a second when both people are online), never be lost once the sender sees "sent", and appear in the same order for everyone in a conversation.
Step 2: Estimate the load
Assume 100 million daily users, each sending 40 messages a day.
- Messages: 100 million × 40 = 4 billion a day. 4 billion ÷ 86,400 seconds ≈ 46,000 messages a second, so plan for about 150,000 a second at peak.
- Connections: if 20% of users are online at the busiest moment, that is 20 million open connections. If one connection server holds around 100,000 idle connections, you need roughly 200 servers, plus spares.
- Storage: about 200 bytes per message with its metadata. 4 billion × 200 bytes ≈ 800 GB a day, or roughly 290 TB a year.
What the numbers decide: the write rate and the storage size rule out a single database, and the connection count means connections need their own fleet of servers.
| Quantity | Value | What it means |
|---|---|---|
| Messages at peak | ~150,000 / s | A write-heavy store that scales by adding nodes |
| Open connections | ~20 million | A dedicated fleet of connection servers |
| Storage per year | ~290 TB | Partition messages across many machines |
Step 3: Keeping a live connection
The server needs a way to push a message to a phone without being asked.
One option is for the phone to ask "anything new?" every few seconds. This is called polling. With 20 million online users asking every 5 seconds, that is 4 million requests a second, almost all answered "no".
A better option is a WebSocket: a connection the phone opens once and keeps open. Either side can send data at any time over it. The phone sends messages up, and the server pushes messages down, with no new request each time.
These connections live on connection servers (sometimes called gateway servers). Their only job is to hold sockets and pass data in and out. They do not store messages or run business logic. That keeps them simple, so a crash only means "these users reconnect".
The phone also sends a small heartbeat every 30 seconds or so. If the heartbeats stop, the server closes the socket and marks the user offline.
Step 4: Finding the receiver
Now there's a problem. Alice is connected to server A. Bob is connected to server B. Alice's message arrives at A. How does A know that Bob is on B?
Keep a presence store: a fast in-memory map of user → connection server, such as bob → ws-b-17. When Bob connects, B writes the entry. When he disconnects, B removes it. Entries also carry a short expiry, so a server that dies without cleaning up does not leave stale entries behind for long.
To actually move the message from A's side to B, use pub/sub (publish and subscribe). Each connection server subscribes to its own channel. The chat service looks up Bob, sees ws-b-17, and publishes the message to that channel. Server B receives it and writes it down Bob's socket.
The same store gives you online status. If Bob has an entry, he is online. Store a last-seen time when he leaves.
Step 5: The high-level design
Follow one message: Alice's phone sends it over her WebSocket to connection server A. A hands it to the chat service, which gives it an ID, saves it to the message store, and only then replies "sent" to Alice. Next the chat service looks Bob up in the presence store and publishes the message to Bob's connection server, which pushes it down his socket.
The order matters: save first, deliver second. If anything fails after the save, the message is still safe and can be delivered later.
Step through the diagram for the normal path, an offline receiver, and a server crash.
Save the message
Alice's message travels over her open WebSocket to server A, then to the chat service. The chat service gives it an ID, writes it to the message store, and tells Alice it was sent.
Step 6: Message IDs, ordering and storage
Two people send a message at almost the same moment. Everyone in the chat must see them in the same order.
Phone clocks cannot be trusted for this, since they drift and users change them. Instead, the server gives each message an ID that only ever goes up within its conversation. You don't need one global order across all chats, which would be a bottleneck. You only need order inside each conversation. Two common ways:
- A per-conversation sequence number (message 1, 2, 3…), handed out by whichever server owns that conversation.
- A time-based unique ID, such as a Snowflake-style ID (a timestamp plus a machine number and a counter), which sorts roughly by time.
The client also attaches its own random ID to each message. If the phone retries after a network blip, the server sees the same client ID and does not store a duplicate.
For storage, every read is "the latest 50 messages in conversation X", and every write appends to the end. So partition by conversation ID and sort by message ID inside each partition. A wide-column store such as Cassandra or HBase suits this well: a conversation's messages sit together on disk, and you scale by adding nodes. See database sharding for how to split the data.
Step 7: Receipts, groups and what can go wrong
Receipts. "Delivered" and "read" are just small messages going the other way. When Bob's phone receives a message, it sends back "delivered up to message 812". When he opens the chat, it sends "read up to 812". Sending one "up to" value instead of one receipt per message keeps the traffic small.
Group chat. When Alice writes to a group of 200, the chat service saves the message once, in the group's partition, then looks up all 200 members and sends it to each online member's connection server. This is fan-out on write. It works well for groups of a few hundred. For channels with 100,000 members, sending 100,000 copies per message is too expensive, so members pull new messages when they open the channel instead.
What can go wrong:
- A connection server dies. Its users reconnect elsewhere and catch up from their last message ID, as in the diagram.
- Pub/sub drops a message. The message is saved, and the receiver's app notices a gap in IDs and fetches what is missing.
- A huge group goes busy. Fan-out spikes. Put the fan-out work on a queue so it can lag a little without slowing new sends.
- Push notifications fail or arrive late. They are a hint, not the delivery path. The real delivery happens when the app syncs.
Trade-offs to say out loud
- WebSockets vs polling: one long-lived connection per device, at the cost of a stateful fleet that must handle reconnect storms.
- Per-conversation order vs global order: cheap and enough for users, at the cost of no single timeline across chats.
- Save before deliver: a few extra milliseconds per message so nothing is lost when a server fails.
- Fan-out on write vs read for groups: instant delivery for small groups; pulling scales for very large ones.
- Wide-column store: fast appends and range reads per conversation, at the cost of flexible queries such as global search, which needs a separate search index.
Questions people ask
Why do chat apps use WebSockets instead of HTTP?
Plain HTTP only lets the client ask and the server answer, so the server cannot push a new message on its own. A WebSocket stays open in both directions, so the server can deliver a message the moment it arrives. Polling over HTTP works but wastes most requests on empty answers.
How does a chat app deliver messages to offline users?
The message is saved to storage before delivery is attempted. If the receiver has no live connection, the server sends a push notification through the phone platform's notification service. When the app opens, it asks for every message newer than the last one it has.
How do you keep chat messages in order?
The server, not the phone, gives each message an ID that increases within its conversation. Clients sort by that ID. You only need ordering inside one conversation, which avoids a single global counter.
What database is best for a chat application?
The access pattern is append a message and read the latest messages in one conversation. A wide-column store partitioned by conversation ID, such as Cassandra or HBase, fits that well and scales by adding nodes. A sharded relational database can also work if you explain how it is partitioned.
How do read receipts work?
When a phone shows a conversation, it sends back a small event saying it has read up to a given message ID. The server stores that and forwards it to the sender, whose app marks every message up to that ID as read.