Getting everything ready for you…

Loading courses, problems and your progress. This takes just a moment.

0s

Notes › System Design

Design a Chat Application

A classic interview question: design something like WhatsApp or Messenger. Follow the usual steps: requirements → estimates → API → high-level design → deep dives → scaling.

1. Requirements

Functional

Non-functional

2. Estimates (back-of-envelope)

3. Why WebSockets

Option How Verdict
Short polling ask "any messages?" every few seconds wasteful, slow
Long polling request stays open until a message arrives better, but a new request each time
WebSocket one persistent two-way TCP connection ✅ best for chat
Server-Sent Events server → client only good for feeds, not chat
Clients keep a WebSocket to a chat server; everything else (login, profile, history, media upload) uses normal HTTPS APIs.

4. High-level design

            HTTPS                                  ┌──────────────┐
Client ───────────────► API servers ─────────────► │ User / group │  (SQL)
   │                                               │   service    │
   │ WebSocket                                     └──────────────┘
   ▼
 Load balancer ──► Chat servers ◄──► Session store (Redis: userId → chat server)
                       │   ▲
                       ▼   │   pub/sub or queue between chat servers
                  Message service ──► Message DB (Cassandra / ScyllaDB)
                       │
                       ├──► Push service (FCM / APNs) for offline users
                       └──► Media: upload to S3 via pre-signed URL, CDN for downloads
Presence service (Redis, heartbeats)

5. Message flow — A sends "hi" to B

  1. A's app creates a client message id (UUID) and sends {to: B, clientMsgId, text} over its WebSocket to chat server S1.
  2. S1 assigns a server message id (sortable, e.g. Snowflake: time + machine + sequence), saves the message to the message DB, and replies to A with an ack → ✓ sent.
  3. S1 looks up B in the session store: B is connected to server S7.
  4. S1 forwards the message to S7 (Redis pub/sub channel per server, or a message queue — see Message Queues and Event-Driven Systems).
  5. S7 pushes it down B's WebSocket. B's app acks → A gets ✓✓ delivered.
  6. When B opens the chat, B sends a read receipt → A sees read.
  7. If B is offline: the message stays stored as undelivered; the push service sends a notification; when B reconnects, the app asks "give me everything after my last message id" and syncs.

Deduplication: if A's connection drops and A resends, the server sees the same clientMsgId and doesn't store it twice.

6. Data model

Messages are written a lot and read by conversation, newest first → a wide-column store like Cassandra/ScyllaDB/HBase fits (massive write throughput, partition by conversation).

messages
  partition key : conversation_id
  clustering key: message_id DESC          (time-sortable)
  columns       : sender_id, type, text, media_url, created_at

conversation_members (conversation_id, user_id, last_read_message_id)
user_conversations   (user_id, conversation_id, last_message_at)   ← chat list screen

7. Group chat

8. Presence (online / last seen)

9. Ordering

Ordering is needed within a conversation, not globally. Use per-conversation sequence numbers or time-sortable ids assigned by the server; clients sort by them. Partitioning queues/DB by conversation_id keeps one chat's events together.

10. Reliability and scaling

11. Security

12. API sketch

POST /api/v1/media/upload-url          → pre-signed URL
GET  /api/v1/conversations?cursor=…    → chat list
GET  /api/v1/conversations/:id/messages?before=<msgId>&limit=50   → history (cursor pagination)
WS   send:    {type:"message", to, clientMsgId, text}
WS   receive: {type:"message", id, from, conversationId, text, sentAt}
WS   receive: {type:"receipt", messageId, status:"delivered"|"read"}

Trade-offs to mention

Decision Choice Why
Transport WebSocket real-time, two-way
Message store Cassandra huge write volume, query by conversation
Delivery at-least-once + client dedupe never lose messages
Groups fan-out on write for small groups fast delivery
Presence heartbeats + TTL in Redis cheap, self-expiring

Interview one-liners

More System Design notes