← Create New Paste

Designing a Chat Messaging System: A System Design Interview Walkthrough

3 views
"Design a chat app like WhatsApp or Slack" is one of the most common system design interview questions. It tests real-time communication, storage, delivery guarantees, and scale. Here is a structured walkthrough you can adapt. 1. Clarify requirements Functional: one-to-one chat, group chat (say up to 500 members), online presence, delivery and read receipts, message history on multiple devices. Non-functional: low latency (under 200 ms for online users), high availability, and messages must never be lost. Estimate scale: for example 50 million daily users sending 40 messages each, around 2 billion messages per day, or roughly 25,000 per second on average with higher peaks. 2. Connection layer Clients keep a persistent connection, usually WebSocket, to a fleet of chat gateway servers. A session service records which gateway each user is connected to. Gateways are stateful, so put them behind a load balancer that supports long-lived connections, and plan for reconnection storms when a gateway restarts. 3. Message flow When user A sends a message to user B: - A's gateway receives it and assigns a message ID (a time-sortable ID like a Snowflake works well). - The message is written to durable storage before acknowledging to A. This is what guarantees "never lost." - The system looks up B's gateway in the session service. If B is online, the message is pushed through that gateway. If not, it goes to a push notification service and stays in B's inbox until B reconnects. - B's client acknowledges receipt, which updates the delivery status. 4. Storage Messages are write-heavy and read mostly by recency within a conversation. A wide-column store partitioned by conversation_id and sorted by message_id fits well. Keep user and group metadata in a relational database. Media files go to object storage, with only the link stored in the message. 5. Group chat For small groups, fan out on write: deliver a copy of each message to every online member's gateway. For very large channels, fan out on read is cheaper: members fetch new messages from the channel when they open it. 6. Ordering and duplicates Order messages per conversation using the time-sortable ID. Clients include a client-generated ID so retries do not create duplicates; the server dedupes on it. 7. Presence Presence changes are frequent and low-value per event. Use heartbeats with a timeout, and only push presence updates to friends who are currently online, possibly batched. 8. Multi-device sync Each device tracks the last message ID it has seen per conversation. On reconnect, it asks for everything after that ID. 9. Trade-offs to mention End-to-end encryption limits server-side search; fan-out strategy affects cost; and stateful gateways complicate deployment. Naming these shows senior-level thinking. Practice tip The best answers are structured and calm, even when the interviewer jumps to a deep-dive on presence or encryption. Practice out loud and time yourself. Some candidates also use Linkjob AI (https://linkjob.ai), which offers real-time help during live job interviews and technical interviews, to keep their design discussion organized under pressure.
Created Nov 15, 2026 13:27 Expires Nov 15, 2027 13:27