38. Design a Chat App (WhatsApp)
1
The interview prompt2
38.1 What do you ask first?3
38.2 How big is this, really?4
38.3 What are the doors?5
38.4 What do we store?6
38.5 What does the whole system look like?7
38.6 How does the WebSocket gateway work?8
38.7 How do message IDs and ordering work?9
38.8 How do delivery and read ticks work?10
38.9 How does offline delivery work?11
38.10 How does group chat work?12
38.11 How does media work?13
38.12 What would you say in the last five minutes?14
Score yourself15
Variations to practise16
Sources17
Code lab38. Design a Chat App (WhatsApp)
38.7 How do message IDs and ordering work?
3 min read+10 XP
Two IDs per message, each solving one problem:
- client_id (made by the sender's app, unique per device): de-duplication. Mobile networks drop acks; the app retries; the server's unique key
(chat, sender, client_id)turns the retry into a no-op. In the chapter's code, the second send ofa-002returns "duplicate" with the same seq, and the chat still has 2 messages. - seq (assigned by the server, per chat): ordering. Every member sees messages in seq order; gaps tell a client it missed something; "sync after N" is exact.
Assigning seq needs one writer per chat: route all messages for a chat to the shard that owns it (Chapter 15), and use an atomic counter there. That's fine because even busy groups send a few messages a second, not thousands.
The sender's app shows its own message immediately (optimistically) with a clock icon, then replaces it with the server's seq and one tick when the ack arrives.
Memory code: "The client ID stops repeats; the chat's sequence number sets the order."