| @@ -0,0 +1,39 @@ | |||
| 1 | + | # System Design Interview: Designing a Chat App (One-to-One and Group) | |
| 2 | + | ||
| 3 | + | "Design WhatsApp" or "design a chat system" tests real-time connections, message ordering and delivery guarantees. | |
| 4 | + | ||
| 5 | + | ## Requirements | |
| 6 | + | ||
| 7 | + | - One-to-one and small group chats (up to about 100 members), with online presence. | |
| 8 | + | - Delivered and read receipts; history synced across devices. | |
| 9 | + | - Messages must not be lost, and must appear in order within a conversation. | |
| 10 | + | - Scale example: 50M daily users, 40 messages each a day, roughly 25,000 messages per second at peak. | |
| 11 | + | ||
| 12 | + | ## Connections | |
| 13 | + | ||
| 14 | + | 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. | |
| 15 | + | ||
| 16 | + | ## Sending a message | |
| 17 | + | ||
| 18 | + | 1. The sender's chat server assigns a message ID that increases within the conversation (per-conversation sequence numbers, or a Snowflake ID). | |
| 19 | + | 2. The message is written to the **message store** before it is acknowledged, so it can't be lost. | |
| 20 | + | 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. | |
| 21 | + | 4. The recipient's device acknowledges it, which produces the "delivered" receipt. | |
| 22 | + | ||
| 23 | + | ## Storage | |
| 24 | + | ||
| 25 | + | 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." | |
| 26 | + | ||
| 27 | + | ## Group chats | |
| 28 | + | ||
| 29 | + | 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. | |
| 30 | + | ||
| 31 | + | ## Points that impress interviewers | |
| 32 | + | ||
| 33 | + | - **Ordering:** order by server-assigned sequence numbers, not client clocks. | |
| 34 | + | - **Idempotency:** clients attach a client message ID so retries don't create duplicates. | |
| 35 | + | - **Multi-device sync:** each device tracks the last sequence number it has seen and asks for anything newer. | |
| 36 | + | - **End-to-end encryption:** the server only relays ciphertext, which limits server-side search. | |
| 37 | + | ||
| 38 | + | --- | |
| 39 | + | *Disclosure: I work on [Linkjob AI](https://www.linkjob.ai/), which offers real-time interview help.* | |
linkjobai / System Design Interview: Designing a Chat App (One-to-One and Group)
Last active 5 days ago
Interview prep notes
linkjobai revised this gist 5 days ago · 093d934
1 file changed, 39 insertions