Courses System design · Part 6 · Case study 3
Case Study: A Chat App
We want to build a chat app: one-to-one chats and groups of up to 200 people, on phones and the web. A message must arrive in under a second, in the right order, and if the receiver is offline, when they come back.
1. Requirements
Functional:
- Send a text message to one person or a group.
- See the chat history on any device.
- Ticks: sent, delivered, read.
- Online status (“last seen”).
Non-functional:
- Low latency: under 500 milliseconds between two online users.
- No message is lost, and the messages of one chat are shown in order.
- Photos and voice calls are out of scope for now.
2. Numbers
- 5 million daily active users, 40 messages each a day: 200 million messages a day, about 2,300 a second, perhaps 10,000 in the busy hour.
- Each message is about 200 bytes with its metadata: 200 million × 200 bytes = 40 GB a day, about 15 TB a year.
- At the peak, perhaps 2 million users are connected at once. If one connection server keeps 50,000 connections open, we need 40 connection servers.
The result: there are many writes and the data grows large, so storage must be sharded. And the number of open connections is a main problem by itself.
3. API
Apps open a long-lived connection (WebSocket) to the server and send and receive on it:
→ send {chatId, clientMsgId, text}
← ack {clientMsgId, msgId, ts} (stored on the server: one tick)
← message {chatId, msgId, from, text, ts}
→ received {msgId} → read {chatId, upToMsgId}
GET /chats/{chatId}/messages?before=msgId (history)
clientMsgId is an idempotency key: if the connection drops and the app sends the message again, it is not stored twice.
4. Data model
- Messages:
(chatId, msgId, from, text, ts). The most frequent query: “the last 50 messages of chat X”. So the shard key ischatId, and each chat’s messages are stored together, sorted bymsgId. A database such as Cassandra fits this pattern (many writes, range reads within one key). - Membership: who is in each chat.
- Last read message for each user in each chat (for ticks and unread counts).
msgId must be unique and in time order: the “time + machine + sequence” method from the unique IDs lab.
5. High-level picture
- The user’s app connects through a load balancer to one of the connection servers and stays connected.
- A presence registry (for example in Redis) records which connection server user X is on right now.
- When Ali sends Sara a message:
- Ali’s connection server gives the message to the message service.
- The message service makes an ID, stores the message, and tells Ali “stored”.
- Then it puts the message in a partitioned log with the key
chatId. - Consumers take the message, ask the presence registry where Sara is, and hand the message to her connection server.
- If Sara is offline, the message stays in storage and a push notification is sent. When Sara connects, her app fetches the messages after its last
msgId.
6. Deep dive: order, and not losing messages
Order
Because messages go into the log with the key chatId, all messages of one chat are in one partition and are read in that order, as we saw in the log lab. Order between different chats is not guaranteed, and it does not need to be.
If two people write in a group at almost the same moment, the server decides the order (by msgId), not the phones’ clocks. Apps sort messages by msgId, so everyone sees the same order.
Not losing anything
- A message is stored first (with replication on several servers), and only then does the first tick come back. So if the connection server fails after the tick, the message is safe.
- Delivery is “at least once”: if the receiver does not confirm, the message is sent again. The receiving app recognises a duplicate by
msgIdand does not show it twice.
When a connection server fails
50,000 users are disconnected at the same moment, and all of them reconnect. If they all try immediately, they knock over the other servers: exactly the thundering herd from the retries lab. The app must reconnect with a doubling wait and random jitter.
Groups
For a group with 200 members, each message is delivered 200 times but stored once. For channels with millions of members this does not work, and a design like the news feed is needed.
7. Trade-offs
- A long-lived connection versus asking again and again: WebSocket gives low latency and low load, but connection servers hold state and are harder to move.
- An exact “last seen” is expensive: every app open and close is a write. It is usually updated every few seconds.
- End-to-end encryption (only the two sides can read a message) greatly improves security, but makes server-side search and syncing many devices harder.
Labs used
Partitioned log · Replication · Unique IDs · Retries · Load balancer · Consistent hashing