System design · Question 50 of 50
Design a chat app like WhatsApp, for one-to-one messages.
Real-time delivery, guarantees and storage.
The answer
Clients keep a WebSocket open to a chat server, and a registry (Redis) records which server each online user is connected to. To send, the sender's server saves the message, with an id the client made up so a retry is never stored twice, then routes it to the recipient's server, which pushes it down their socket. If the recipient is offline, the message waits in storage and a push notification goes out; when they reconnect, the app fetches everything after the last message it has.
The ticks are acknowledgements travelling back: sent, delivered, read. Order comes from a sequence number per conversation. Storage: a wide-column store partitioned by conversation and sorted by time, because the question is always "the latest messages in this chat". With end-to-end encryption, the servers only ever hold ciphertext.
The picture
Asha ═ ws ═▶ [ chat server 1 ] ─ save ─▶ [ message store ]
│ where is Ravi? ─▶ [ Redis: Ravi → server 2 ]
▼
[ chat server 2 ] ═ ws ═▶ Ravi ✓✓ delivered
Ravi offline? A push notification; he syncs from his last message idGroup chats with a thousand members?
Show the answer
Fan out on the server to each member's connection or inbox; for very large groups, fan out on read, with members pulling from the group's timeline.