Lesson 5 of 17
38. Design a Chat App (WhatsApp)

38.4 What do we store?

3 min read+10 XP
messages chat_id, seq, sender, client_id, ciphertext, sent_at (sharded by chat_id) unique (chat_id, sender, client_id) (de-duplication) cursors (user, device, chat_id) → last delivered seq, last read seq chats chat_id, members, type (direct | group), group key info sessions (user, device) → gateway server (in-memory store, TTL) devices user → devices, push tokens media encrypted blobs in object storage; the message carries the blob ID and its key

Interviewer pushback:

  • "Why a sequence number per chat instead of timestamps?" → Clocks on phones and servers disagree, and two messages can share a millisecond. A per-chat counter assigned by the server gives one clear order for everyone in the chat and makes "give me everything after 2" exact.

Memory code: "One socket per device; every message gets a client ID and a chat sequence number."