Case Study: WhatsApp Jaisa Chat App

Full design with a video: real-time messaging, 1-1 and group delivery, online status, media, and scale.

Advanced10 min read
Language
Theme

EP 05: WhatsApp Jaisa Chat App Kaise Banta Hai?

User AWebSocketChat serviceMessage DBCassandraB online?WS server (B)User BOffline storedeliver on reconnectMediaobject storage + URLPresence (Redis heartbeats) + read receipts flow over the same WS channel
Key idea at a glance

The problem, stated properly

Design the backend of a chat app at WhatsApp scale: 1-on-1 and group messages, online presence, delivery guarantees (sent, delivered, read ticks), media sharing, and offline delivery, for billions of users who expect messages to arrive in under a second anywhere on Earth. The shape that emerges: a massive persistent-connection layer, surprisingly little data in flight, and a storage layer optimized for write-heavy, chat-history access.

Scale first

WhatsApp published enough over the years to anchor real numbers:

  • Messages: ~100 billion/day. That is roughly 1.2 million messages/sec average, several million at peak (New Year's Eve is the famous stress test).
  • Connections: 2+ billion users. If 50% are online at peak, you need 1 billion concurrent connections. A single tuned server holds roughly 1-2 million connections (WhatsApp ran FreeBSD + Erlang and famously supported ~2M connections per box with ~50 engineers total), so the connection tier is ~500-1,000 boxes, not 100,000.
  • Storage: text messages are tiny (~100 bytes), so 100B/day is ~10 TB/day of text before media. Media dominates: with even 10% of messages carrying a 100 KB average image, that is ~1 PB/day. Media goes to blob storage with CDN; only references live in the message store.

Why WebSockets (and why not HTTP polling)

Chat is bidirectional and latency-sensitive. The server must push "user X is typing" and incoming messages instantly. HTTP polling means every client asks "anything for me?" every few seconds: at 1 billion online users even a 5-second poll is 200 million requests/sec of mostly-empty responses, and it still adds up to 5 seconds of latency. Long-lived WebSocket (or a custom TCP protocol, which is what WhatsApp actually runs) keeps one connection per client and pushes events in milliseconds, at the cost of holding connection state on servers.

The connection tier's job is deliberately thin: hold connections, authenticate, forward envelopes. No business logic, so it scales horizontally and a dead node just triggers client reconnect to another.

The message path, step by step

  1. Alice sends to Bob: client encrypts (Signal protocol, end-to-end) and pushes the envelope over its WebSocket.
  2. Her connection server hands it to the message service, which assigns a monotonically increasing message ID (per conversation, Snowflake-style or Lamport clocks), persists it to the store, and ACKs Alice. Her client shows one tick (sent).
  3. The service looks up Bob's active connection (a routing directory, often Redis-backed: user_id maps to server_id) and pushes the envelope to that server, which delivers it. Bob's device ACKs, and Alice's client gets two ticks (delivered).
  4. If Bob is offline, the message waits in a per-user queue in the store. On reconnect, his client syncs pending messages, ACKs them, and the queue drains. Read receipts (blue ticks) are just another tiny message flowing back through the same pipe.

Notice what is not here: no fan-out at write time for 1:1 chat. The message is stored once, delivered once. Fan-out pain belongs to groups.

Groups: where fan-out actually lives

WhatsApp caps groups at 1,024 members. Each group message fans out to every member's delivery queue (or, for offline members, sits in their pending store). A 1,024-member group at 1 message/sec is 1,024 deliveries/sec from one message, so the cap exists precisely to bound fan-out cost. Broadcast lists and channels (one-to-millions) need a different model entirely: fan-out on read or a pub/sub tier, because writing a copy per follower does not scale.

Presence and typing indicators

Online/offline status changes constantly and must reach contacts fast, but it is ephemeral: never write presence to the main store. A separate presence service keeps it in memory (Redis with TTL heartbeats: client pings every N seconds, key expires if pings stop) and fans changes out to watchers over the same connection tier. Accept staleness here; "last seen 1 minute ago" being 30 seconds wrong harms nobody.

Tradeoffs worth naming

  • Eventual vs strong consistency: cross-device ordering is best-effort. WhatsApp uses per-conversation sequencing; a globally ordered timeline is impossible without killing availability.
  • ACK-at-every-hop costs bandwidth but gives the tick UI users trust, and drives the retry logic that makes delivery at-least-once. Clients dedupe by message ID.
  • Erlang/BEAM (WhatsApp's choice): millions of lightweight processes map beautifully to millions of connections, and hot code reloading means deploying without dropping a billion sockets. The cost: a niche talent pool.

Production failure modes

  • Reconnect storms: a connection node dies and 2 million clients reconnect at once, DDoS-ing the remaining tier. Defenses: exponential backoff with jitter on clients, and a load-balancing layer that spreads reconnects.
  • Offline-queue bloat: a user offline for months accumulates a huge pending store. Real systems cap retention (~30 days) and drop older messages.
  • Hot group: one very active group pins load to the partition that owns it. Group IDs must hash evenly across shards, and abusive groups get rate-limited.
  • Clock skew breaking order: never trust client clocks for ordering. Server-assigned sequence numbers only.

Common interview traps

  • Suggesting HTTP polling for message delivery. It is the single most penalized answer in this question.
  • Fan-out-on-write for channels with millions of followers without questioning the cost.
  • Storing presence in the database. It is ephemeral state; it belongs in memory with TTLs.
  • Forgetting that offline delivery is the hard half: anyone can deliver to a connected socket.

Interview Q&A

Q: How do you guarantee exactly-once delivery?
A: You don't, and saying so is the right answer. You guarantee at-least-once (persist before ACK, retry until ACK) and make clients idempotent: every message carries a unique client-generated ID, and the receiver dedupes on it. Exactly-once delivery over unreliable networks is provably impossible; exactly-once processing is what you actually build, via idempotency.

Q: How does WhatsApp scale to billions of users with so few servers?
A: Three compounding choices: one lightweight process per connection via Erlang (millions per box instead of thousands per box with threads), a deliberately thin connection tier with zero business logic, and aggressive separation of concerns (media to blob storage, presence to in-memory stores, messages to a write-optimized store). Each layer scales independently on its own bottleneck.

Key takeaways

Interview pointer

Walk through one message's life: sender's device to receiver's device, naming each component. That story is 80% of the answer.