Design a Chat System
2 min read
Design a Chat System (WhatsApp / Discord)
Real-time messaging changes the game: the server must push to clients, not wait to be polled.
1. Requirements
- Functional: 1:1 and group messages; online/last-seen status; delivery & read receipts; message history.
- Non-functional: low latency (< 100 ms), highly available, ordered delivery per conversation, durable (no lost messages).
2. The core challenge: real-time delivery
HTTP is request/response — the server can't initiate. Options:
| Technique | How | Verdict |
|---|---|---|
| Polling | Client asks repeatedly | Wasteful, laggy |
| Long polling | Hold the request open | Better, still clunky |
| WebSocket | Persistent, bidirectional connection | ✅ The standard |
Clients hold an open WebSocket to a connection/gateway service that tracks which user is on which server.
3. High-level design
User A ══ws══▶ Gateway 1 ─▶ Message service ─▶ DB (persist)
│
▼
User B ◀══ws══ Gateway 2 ◀── routed via presence registry
- Presence registry (Redis):
user_id → gateway_server. To deliver, look up B's server and forward. - If B is offline: persist the message; deliver on reconnect (and send a push notification).
4. Data model
messages: id | conversation_id | sender_id | content | created_at | status
(partition by conversation_id; sort by created_at)
Use a write-optimised store (Cassandra) — chat is write-heavy and append-only. A time-sortable ID (Snowflake) gives per-conversation ordering.
5. Deep dives
- Ordering: assign a monotonic sequence per conversation so clients render in order despite network jitter.
- Delivery receipts:
sent → delivered → read, each an ack event. - Group chat: fan-out the message to each member's connection; for huge groups, treat like feed fan-out.
- Scale: connection servers are stateful (they hold sockets) — use consistent hashing and a shared presence store so any gateway can route.
Takeaway: the heart of a chat system is maintaining persistent connections and a presence registry to route messages to the right server in real time.