System design case study C05 · WebSockets · Ordering

Design a Chat App (WhatsApp-style)

One-to-one and group messaging for hundreds of millions of people, with delivery ticks, offline sync and end-to-end encryption.

What you'll be able to explain
  • Run a fleet of WebSocket gateways and route messages between them
  • Store messages for fast per-conversation reads
  • Guarantee ordering and exactly-once display with sequence numbers and acks
  • Fan out group messages efficiently
  • Handle offline delivery, receipts, presence and media

Scope

Functional

  • 1:1 and group chats (up to 1,000 members); text and media.
  • Sent / delivered / read receipts; online and last-seen presence.
  • Offline users receive messages on reconnect; push notification when offline.
  • Multi-device sync (phone + web).

Non-functional

  • Delivery latency < 500 ms p99 when both users are online.
  • No message loss once the sender sees one tick; per-conversation ordering.
  • End-to-end encryption: servers cannot read content.

Out of scope: voice/video calls, payments, channels/communities.

Size

  • 500M DAU, 40 messages/day each → 20B messages/day ≈ 230k/s average, ~700k/s peak.
  • ~150M concurrent connections; at ~200k per gateway → ~750 gateway servers.
  • Text storage: 20B × 200 B = 4 TB/day. Many products keep messages only until delivered; we'll keep a server copy for multi-device sync with retention.

APIs and data model

Over the WebSocket (binary frames, protobuf):

→ send   {client_msg_id, conv_id, ciphertext, type}
← ack    {client_msg_id, server_seq, ts}             (one tick)
← msg    {conv_id, server_seq, sender, ciphertext}
→ recv_ack {conv_id, up_to_seq}                       (two ticks)
→ read     {conv_id, up_to_seq}                       (blue ticks)
→ sync     {conv_id → last_seq, ...}                  (on reconnect)
Store Key Notes
Messages (Cassandra/ScyllaDB) PK (conv_id, bucket), clustering server_seq DESC Append-only, per-conversation ordered
Inbox / sync state user_id → {conv_id → last_delivered_seq} Drives offline sync
Conversations / members (SQL) conv_id, member list Low volume, relational
Session registry (Redis) user_id/device → gateway TTL + heartbeats

The architecture

CLIENTSenderCLIENTRecipientSERVICEGateway ASERVICEGateway BSERVICEChat serviceCACHESequencerper conversationDATABASEMessage storeCACHESession registryEXTERNALPush serviceFCM / APNsWebSocketWebSocketsendnext seqpersistlookup recipientdeliverif offline
Gateways hold connections; the chat service orders, stores and routes messages.

The 30-second summary

Say it in the interview

"Clients keep WebSocket connections to stateless-ish gateways; a registry maps users to gateways. The chat service, sharded by conversation, assigns per-conversation sequence numbers, persists to a wide-column store before acking, then routes to recipients' gateways or triggers push. Clients dedupe by client message ID and order by sequence, and sync by last-seen sequence on reconnect. Groups store once and fan out notifications per gateway."

Follow-up questions the interviewer may ask

  1. How do group chats with 1,000 members work?
  2. How do read receipts work without flooding the system?
  3. How do you support end-to-end encryption?
  4. How do you sync history to a new device?

Model answers to each of these are in the book.