SYSTEM DESIGN SKETCHBOOK / PRACTICE SHEET / NO. 05
Design a chat app
Two people message each other from their phones. Phones drop off the network all the time, sometimes right after tapping send and before hearing back, and people expect every message to show up exactly once, in the same order on both screens, even after a long time offline.
Requirements
- Send and receive text messages in one-to-one conversations.
- Keep message history and let a client catch up on everything it missed after a disconnect.
- A send that is retried after a timeout must never show up as a second message.
- Show the sender when a message has been stored by the server and when it has reached the recipient's device.
Constraints and assumptions
- 100 million daily active users sending about 40 messages each a day: roughly 46,000 messages per second on average and 150,000 at peak.
- Up to 30 million clients connected at the same time over long-lived connections.
- Messages are text, at most 4 KB, about 100 bytes on average. History is kept indefinitely.
- When both people are online, a message should appear on the recipient's screen within 500 ms at p99.
- Assume a client can drop and reconnect at any point, including after sending but before receiving an acknowledgement. Start with one device per user.
Out of scope for the first sketch
Group conversations · Photos, voice notes and other media · End-to-end encryption, typing indicators and online presence
Draw your design
Rough math and notes
SYSTEM DESIGN SKETCHBOOK / FOLLOW-UPS
Design a chat app
Finish your first sketch before answering these. Cross out freely.
- The recipient is offline for an hour. How does their client recover the messages it missed?
- The sender's client times out and retries the send. Where does your design detect the duplicate?
- Both people send a message at nearly the same moment. What order do both clients show, and who decides it?
- A user signs in on a second device. What changes in delivery and in catch-up?
- A gateway server holding 500,000 open connections crashes. What happens to those users and to messages in flight?
Compare afterwards: worked approaches and reading at sketchbook /questions/chat-app/review