Case studies
Case study: chat system
Design a WhatsApp or Slack style messaging system. Connection gateways, per-conversation ordering, wide-column storage and large-channel fan-out are the parts that matter.
A chat system delivers messages between people in near real time, stores their history, and works when the recipient is offline. WhatsApp, Slack, Discord and iMessage differ in product, but they share one skeleton. A large real-time connection layer pushes messages to devices. Careful ordering and delivery keep conversations correct. Storage holds trillions of small messages without melting on hot channels.
The common interview mistake is to name Kafka and Cassandra first. Clarify the product first. Encrypted one-to-one chat and searchable workplace rooms share a skeleton, but they diverge sharply on fan-out, search and encryption.
Concepts on this topic
| Step | Concept | What it answers |
|---|---|---|
| 1 | Requirements and scale | What must the system do, and how big is it? |
| 2 | Client message IDs | How do retries avoid duplicate messages? |
| 3 | Presence and typing | How do best-effort signals stay cheap? |
| 4 | Connection layer | How do gateways and the registry deliver to devices? |
| 5 | Message ordering | How does every device show messages in order? |
| 6 | Chat storage | How is history partitioned and retained? |
| 7 | Large channels and receipts | How does fan-out work for huge rooms? |
Ask these first
Is it end-to-end encrypted? If yes, the server is a relay of ciphertext it can't read. Signal-style apps use the Signal protocol, so the server can't search message bodies or make rich previews. Group encryption adds complexity, and large open communities often skip it. Notifications then say "New message" unless the device decrypts locally.
Is history searchable? Workplace chat needs search, retention policies and exports. Consumer messengers may limit search when encryption is on.
Is the room small or large? That decides between per-member fan-out and a topic. See the large channels page.
Failure modes
| Failure | Effect | Mitigation |
|---|---|---|
| Gateway crash | Dropped connections | Clients reconnect. Registry entries expire. Resume from history |
| Hot channel | Latency spike | Split buckets, cache recent messages, use the large-channel path |
| Fan-out bus lag | Late delivery | Monitor lag. Degrade typing and presence first. Never lose persisted messages |
| Push provider outage | Offline users stay quiet | Retry with backoff. Show a badge on next open |
| Clock skew on IDs | Odd ordering | Use per-channel sequencers, or hybrid logical clocks, not wall clocks alone |
Under a festival spike, shed typing and presence first, then delay receipts. Keep accepting and saving the message bodies.
Other parts
- Push notifications for offline users go through the notification system. Collapse several messages into one push.
- Media uploads straight to object storage with a presigned URL. The message holds the key.
- Link previews run in a separate, rate-limited worker. Never fetch attacker-controlled URLs on the send path.
- Moderation and retention run as background jobs that mark rows, and they rely on the deleted flag from the storage page.
Key takeaways
- Split the connection layer, gateways plus registry, from durable message storage.
- Persist, then ACK. Deliver at least once, and deduplicate with
clientMsgIdandmessageId. - Order messages per conversation, not globally.
- Treat large channels as a different fan-out problem from small groups.
- Presence and typing are best-effort. Push covers offline users. Encryption changes what the server can see.
Further learning
Discord's post on storing trillions of messages is the best public storage case study for chat.
How Discord Stores Trillions of MessagesEngineer Bo Ingram shares insight into how Discord shoulders its traffic and provides a platform for our users to communicate.discord.com
Slack's real-time messaging architecture explains gateway and channel fan-out in a workplace chat product.
Real-time Messaging (Slack Engineering)Did you know that ground stations transmit signals to satellites 22,236 miles above the equator in geostationary orbits, and that those signals are then beamed down to the entire North American subcontinent? Satellite…slack.engineering
Gaurav Sen's walkthrough of a messaging design covers gateways, sessions, group fan-out and retries clearly.
Related
Real-time delivery
How a server pushes updates to clients that did not ask, from polling to WebSockets, and the gateway, pub/sub and reconnect design that serves millions of open connections. Start here for the real-time map.
Case study: notification system
Designing push, email and SMS delivery at scale, covering the pipeline, preference checks, idempotency, provider rate limits, retries and priority lanes.
Sharding and partitioning
Split data across machines by choosing a shard key, hashing or ranging it, routing requests, handling hot spots, resharding without downtime and generating IDs that stay unique.