Design a Chat System
Intermediate · 30 min read · High-Level Design, Case Studies: Core
WebSockets, message ordering, presence, groups and offline delivery.
Design a chat application like WhatsApp, Messenger or Slack: one-to-one and group messaging delivered in real time, with message history across devices, delivery and read receipts, online presence, and offline delivery via push notifications.
1. Clarify the problem
2. Requirements
Functional: send/receive messages in 1:1 and groups; message history with pagination; delivery/read receipts; presence; push notifications when offline; media sharing.
3. Capacity estimation
So: a write-heavy, time-ordered, append-only message store partitioned by conversation (wide-column); a large fleet of connection gateways; routing between gateways is the key infrastructure problem.
4. API
Real-time over a persistent WebSocket connection (bidirectional, low overhead):
5. Data model
Only chat history goes in this store. Profiles, settings and contact lists are ordinary relational data. Chat history is huge, append-heavy, mostly read when recent, with a read:write ratio near 1:1 for one-to-one chat — the profile of a wide-column/key-value store (Facebook Messages used HBase, Discord Cassandra and…
6. High-level design
Send flow
7. Deep dives
Which gateway should a client connect to? After login, a service discovery layer (backed by a registry such as ZooKeeper, etcd or Consul) returns the best gateway for this client — nearby, healthy and not overloaded — and the client opens its WebSocket there.
8. Failure modes
9. Trade-offs summary
10. What interviewers look for
Interview questions
- How do you guarantee that all participants see messages in the same order?
- A user's phone was offline for two days. How does it catch up?
- How would you design presence for 500M users without overwhelming the system?
Practice
Write your answer before opening the suggested one. Anything you mark "Partly" or "Missed it" comes back in your review deck.
Further reading
Key takeaways
- Thin, stateful WebSocket gateways plus a session registry route messages across a large connection fleet.
- Order messages with per-conversation sequence numbers, not timestamps.
- Persist before acknowledging; deliver at-least-once; dedupe with client message IDs; sync on reconnect.
- Store messages once per conversation in a wide-column store partitioned by conversation and time bucket.
- Fan out to small groups; use fan-out on read for huge channels; keep presence and typing indicators cheap and ephemeral.
Previous: Design a News Feed · Next: Design Cloud File Storage (Dropbox)