System Design blueprint · Messaging & Collaboration
Real-time chat over WebSockets
Persistent connections, ordered messages and presence: the core of every messaging app.
- Steps
- 6
- Subjective questions
- 32
- Objective questions
- 32
The problem and the outcome
Problem: Polling for new messages wastes battery and still feels laggy.
Outcome: Messages arrive in order in under a second, and offline users still get notified.
Architecture
How it works, step by step
- Each app keeps one persistent WebSocket connection open.
- Gateways hold millions of open connections and keep no other state.
- The chat service stamps a per-conversation sequence ID, so messages stay in order.
- Messages are written to a store built for heavy writes before delivery.
- Pub/Sub routes each message to whichever gateway holds the recipient's connection.
- If presence says the user is offline, a push notification goes out instead.
Tech stack
- WebSocket gateway
- Redis Pub/Sub or Kafka
- Cassandra / ScyllaDB for messages
- Redis presence with TTL
- APNs / FCM for offline push
Key design decisions
- Persistent WebSocket connections, so the server can push messages instantly instead of being polled.
- A sequence number per conversation, so clients order messages and detect gaps after reconnecting.
- A write-heavy store partitioned by conversation ID, sorted by time, for fast 'latest 50 messages' reads.
Common mistakes
- Ordering by client timestamps, which disagree across devices.
- Keeping connection state in a single gateway without a routing layer to find which gateway holds a user.
- Presence updates on every heartbeat broadcast to every contact, which floods the system.
What to measure
- Message delivery latency
- Concurrent connections per gateway
- Messages delivered vs. sent
- Reconnect rate
Most-asked interview questions
Subjective questions 32
1WebSockets, long polling or Server-Sent Events: which would you choose for chat?Asked almost every time
WebSockets give a two-way, low-overhead persistent connection, which suits chat. SSE is one-way, server to client, which suits feeds and notifications. Long polling works everywhere but costs more. Most chat apps use WebSockets, with long polling as a fallback.
2How do you deliver a message when the recipient is connected to a different server?Asked almost every time
Each gateway subscribes to a channel per connected user or conversation (Redis Pub/Sub, Kafka) or registers in a connection map. The sender's gateway publishes the message after storing it, and the recipient's gateway pushes it down the socket. If nobody is connected, a push notification goes out.
3Walk through the architecture of a chat system like WhatsApp.Asked almost every time
Clients keep WebSocket connections to stateless gateway servers. A session service maps users to gateways; messages are written to a partitioned message store keyed by conversation, then routed through pub/sub to the recipient's gateway. Offline users get push notifications and fetch missed messages on reconnect using their last acknowledged sequence number.
4How do you design group chats?Asked almost every time
Store membership per group, write each message once to the group's partition, and fan out delivery to online members' gateways through pub/sub. Very large groups use fan-out on read (members fetch from the group timeline) instead of pushing to every member.
5How do you store messages at scale?Asked almost every time
Use a wide-column store like Cassandra or ScyllaDB partitioned by conversation ID and clustered by message ID or timestamp, so a conversation's recent messages are one sequential read. Keep hot recent data in cache and archive old messages to cheaper storage.
6How do delivery and read receipts work?Asked almost every time
Each message gets states: sent (server stored it), delivered (recipient device acknowledged), read (recipient opened it). Clients send acknowledgements with message IDs; the server updates state and notifies the sender, batching receipts for groups to limit traffic.
7How do you guarantee ordering and no lost messages?Very often asked
The server assigns a per-conversation sequence number when it stores the message, and acknowledges the client. Clients send a client message ID so retries are deduplicated, and on reconnect they ask for everything after the last sequence they have.
8How do you handle reconnects without losing or duplicating messages?Very often asked
Clients remember the last sequence number they received per conversation and request everything after it on reconnect. Messages carry client-generated IDs, so resends after a timeout are deduplicated on the server.
9How does end-to-end encryption change the design?Very often asked
Messages are encrypted on the sender's device with keys only recipients hold (Signal protocol), so the server stores and routes ciphertext it cannot read. Key distribution, multi-device sync and group key management become core concerns; server-side search and moderation are no longer possible.
10How do you scale WebSocket gateways?Very often asked
Keep gateways stateless apart from live connections, spread connections with a load balancer that supports long-lived connections, track user-to-gateway mapping in Redis, and use pub/sub so any service can reach any gateway. Plan for reconnect storms after deploys by draining gradually.
11How do typing indicators and presence avoid overloading the system?Very often asked
Treat them as ephemeral: send them only to members currently viewing the conversation, throttle them on the client, never store them durably, and use heartbeats with timeouts for presence rather than per-event writes.
12How do you send images and files in chat?Very often asked
Upload the file directly to object storage with a pre-signed URL (encrypted on the client for end-to-end encryption), send a message containing the file reference and thumbnail, and let recipients download through a CDN.
13How do push notifications fit into chat?Very often asked
When the recipient has no active connection, the server sends a push via APNs or FCM with minimal content (or an encrypted payload), which wakes the app to sync. Collapse many messages into one notification per conversation.
14How do you assign message IDs that sort correctly per conversation?Very often asked
Use a per-conversation sequence number assigned by the server that owns the conversation, or a time-ordered ID such as a Snowflake or ULID combined with the conversation ID. Clients store the highest sequence they have and ask for everything after it on reconnect.
15How would you design message reactions and replies?Very often asked
Store reactions as small records keyed by message and user so each user has one reaction per emoji, keep aggregated counts on the message for fast display, and send reaction events through the same delivery path. Replies reference the parent message ID and can be shown as a thread.
16How do you support multiple devices per user?Often asked
Track each device's connection and delivery state separately, deliver messages to all active devices, sync history to new devices, and for end-to-end encryption encrypt to every device's key.
17How do you implement message search?Often asked
Without end-to-end encryption, index messages in a search engine like Elasticsearch partitioned by user or conversation with access checks. With end-to-end encryption, search must happen on the device over the local message store.
18How do you moderate or prevent spam in chat?Often asked
Rate-limit message sending per user and new account, detect spam patterns (identical messages to many users), let users block and report, and use reputation scores; with end-to-end encryption rely on metadata and user reports.
19How do you handle message edits and deletes?Often asked
Send edit or delete events referencing the original message ID; clients update their view, and the server stores the change. Deletion for everyone has time limits and cannot guarantee removal from devices that already received it.
20What happens when a gateway server crashes?Often asked
Its clients disconnect and reconnect to other gateways via the load balancer, re-register in the session store, and request missed messages by sequence number. Messages already persisted are not lost.
21How do you estimate capacity for chat?Often asked
With 100 million daily users sending 50 messages each, that is 5 billion messages a day, about 58,000 per second on average and several times that at peak; each gateway might hold 50,000–100,000 connections, so plan thousands of gateway servers at peak.
22WebSockets vs. a protocol like MQTT for chat?Often asked
WebSockets are standard in browsers and simple with custom message formats. MQTT is lightweight with built-in QoS levels and suits constrained mobile networks; WhatsApp historically used a customised XMPP. Most new systems use WebSockets plus their own protocol.
23How do you keep message ordering per conversation?Often asked
Assign a monotonically increasing sequence number per conversation at the server (from the partition leader or a counter) and have clients sort by it, not by device clocks.
24How do you handle a user who is offline for weeks?Often asked
Keep undelivered messages durable in the message store, not in gateway memory, send a push notification summary, and when the user reconnects page through missed messages by sequence number rather than pushing everything at once.
25How do you implement disappearing messages?Often asked
Store an expiry time with the conversation setting or message, have every client delete messages after the timer, and delete server-side copies with a TTL. Make clear in the UI that recipients can still take screenshots.
26How would you add voice and video calls to a chat app?Often asked
Use the chat channel for signalling (offer, answer and ICE candidates), WebRTC for peer-to-peer media, STUN to discover addresses and TURN relays when direct connections fail, and an SFU for group calls so each client uploads one stream.
27How do you show an accurate 'last seen' without heavy load?Often asked
Update last-seen in a fast store only on connect, disconnect and periodic heartbeats, coarsen it to minutes, publish changes only to contacts who have the chat open, and respect privacy settings that hide it.
28How do you archive old conversations?Sometimes asked
Move messages older than a threshold to cheaper storage tiers or compressed blobs, keep an index of where they live, and load them on demand when users scroll back.
29What metrics matter for a chat system?Sometimes asked
Message delivery latency (send to delivered), delivery success rate, connection counts and churn, reconnect rate, push delivery rate, and storage growth.
30How do you test a chat system's reliability?Sometimes asked
Simulate network drops, reconnects and gateway crashes in load tests, check for lost or duplicated messages using sequence numbers, and run soak tests with millions of simulated connections.
31How do read receipts work with privacy settings?Sometimes asked
Respect users who turn read receipts off by not sending read events for them, and often not showing others' receipts to them either, applied consistently on server and clients.
32How do you export a user's chat history on request?Sometimes asked
Run an asynchronous export job that pages through the user's conversations, includes media links or files, packages the result as an encrypted archive in object storage, and sends a time-limited download link.
Objective questions 32
1Which protocol gives full-duplex communication over one TCP connection?Asked almost every time
Why: WebSocket upgrades an HTTP connection to a persistent two-way channel.
2What lets clients fetch exactly the messages they missed?Asked almost every time
Why: Ask for everything after that number.
3Which store fits chat messages partitioned by conversation?Asked almost every time
Why: Recent messages are one sequential read.
4What stops resent messages from appearing twice?Asked almost every time
Why: The server deduplicates by ID.
5How is presence usually tracked?Very often asked
Why: If the heartbeat stops, the key expires and the user shows as offline.
6With end-to-end encryption the server stores…Very often asked
Why: Only recipients hold the keys.
7How are very large groups delivered efficiently?Very often asked
Why: Avoids huge write amplification.
8Typing indicators should be…Very often asked
Why: They are only useful in the moment.
9Which tracks which gateway a user is connected to?Very often asked
Why: Routing needs the mapping.
10What happens to clients when a gateway crashes?Very often asked
Why: Persisted messages are safe.
11When should a push notification be sent?Very often asked
Why: It wakes the app to sync.
12Per-conversation sequence numbers let clients…Very often asked
Why: Missed messages are fetched precisely.
13Where should undelivered messages live for offline users?Very often asked
Why: Gateways restart; messages must survive.
14What ensures correct message order within a chat?Often asked
Why: One authority assigns the order, so every device sees the same sequence.
15Where should message search run with end-to-end encryption?Often asked
Why: The server cannot read messages.
16How are files shared in chat?Often asked
Why: Recipients download via a CDN.
17What should ordering within a conversation use?Often asked
Why: Device clocks drift and disagree.
18Deleting a message for everyone…Often asked
Why: Devices may keep copies.
19How are multiple devices per user handled?Often asked
Why: Each device has its own state.
20About how many messages per second is 5 billion a day?Often asked
Why: 5 billion divided by 86,400.
21Which protocol has built-in QoS levels for mobile messaging?Often asked
Why: Lightweight publish/subscribe.
22What should happen to presence when heartbeats stop?Often asked
Why: Heartbeats with timeouts.
23Which protocol carries call media between users?Often asked
Why: WebRTC handles real-time audio and video.
24When peers cannot connect directly, calls use…Often asked
Why: TURN relays media through a server.
25Reactions are best stored as…Often asked
Why: That enforces one reaction per user and allows counts.
26Disappearing messages are removed from servers by…Often asked
Why: Server copies must expire too, not just client views.
27Which metric best reflects chat user experience?Sometimes asked
Why: Users feel delivery delay.
28How are reconnect storms after deploys reduced?Sometimes asked
Why: Spread the reconnections.
29Old conversations are best kept in…Sometimes asked
Why: Saves cost for rarely read data.
30Spam in chat is limited by…Sometimes asked
Why: Especially for new accounts.
31Read receipts for users who disabled them should…Sometimes asked
Why: Respect privacy settings.
32Group video calls usually use an SFU so that…Sometimes asked
Why: The SFU forwards streams instead of every peer sending to every other.
Track your progress through every blueprint in your dashboard.
Start learning