# System Design Interview: Designing a Chat App (One-to-One and Group) "Design WhatsApp" or "design a chat system" tests real-time connections, message ordering and delivery guarantees. ## Requirements - One-to-one and small group chats (up to about 100 members), with online presence. - Delivered and read receipts; history synced across devices. - Messages must not be lost, and must appear in order within a conversation. - Scale example: 50M daily users, 40 messages each a day, roughly 25,000 messages per second at peak. ## Connections Clients keep a **WebSocket** (or a similar long-lived connection) to a **chat server**. A **session service** (backed by Redis) maps `user_id` to the server holding that user's connection. ## Sending a message 1. The sender's chat server assigns a message ID that increases within the conversation (per-conversation sequence numbers, or a Snowflake ID). 2. The message is written to the **message store** before it is acknowledged, so it can't be lost. 3. Look up the recipient's session: if they're online, forward the message to their chat server; if they're offline, send a push notification. 4. The recipient's device acknowledges it, which produces the "delivered" receipt. ## Storage A wide-column store (Cassandra, HBase) partitioned by `conversation_id` and sorted by `message_id` fits the access pattern: "give me the latest 50 messages of this chat." ## Group chats For small groups, fan out at write time to each member's inbox or connection. For very large channels, store the message once and let clients pull. ## Points that impress interviewers - **Ordering:** order by server-assigned sequence numbers, not client clocks. - **Idempotency:** clients attach a client message ID so retries don't create duplicates. - **Multi-device sync:** each device tracks the last sequence number it has seen and asks for anything newer. - **End-to-end encryption:** the server only relays ciphertext, which limits server-side search. --- *Disclosure: I work on [Linkjob AI](https://www.linkjob.ai/), which offers real-time interview help.*