Case study V.3Intermediate

Design a Chat System Like WhatsApp

Chat introduces stateful servers: millions of open connections that must find each other. The design turns on how a message travels from one socket to another, and what happens when the other side is offline.

The short answer

Requirements. One to one chats and groups of up to 100, delivery and read receipts, online status, push notifications for offline users and history on every device; delivery is fast, messages keep their order within a conversation, and an acknowledged message is never lost.

Messages
23,000 per second50M daily users, 40 each
Storage
200 GB a dayabout 73 TB a year
Connections
10M at onceabout 100 chat servers

The design. Each client keeps one WebSocket open to a stateful chat server, while login, profiles and media stay on ordinary HTTP services. A message gets a time sortable Snowflake ID, is written to a wide column store partitioned by conversation before anyone is told, and then goes to the recipient’s inbox queue, which their chat server consumes and pushes down the socket. Offline users get a push notification, and every device catches up by asking for all messages after the last ID it has seen.

Interview prompt

Design a chat app like WhatsApp or Messenger: one to one and small group chats, online status, and sync across devices.

Requirements and estimates

Functional requirements

  • One to one and group chats of up to 100 members.
  • Delivery and read receipts, online presence.
  • Offline users get push notifications and catch up on reconnect.
  • History syncs across a user’s devices.

Non-functional requirements

  • Low delivery latency.
  • Messages ordered within a conversation.
  • Durable: an acknowledged message is never lost.

Estimates

Messages     50M DAU × 40 per day ÷ 86,400 s    ≈ 23,000 messages/s
Storage      2B messages/day × 100 B            ≈ 200 GB/day, ≈ 73 TB/year
Connections  10M concurrent ÷ 100K per server   ≈ 100 chat servers

WebSockets, long polling or short polling

TechniqueHow it worksTradeoff
Short pollingClient asks “anything new?” every few secondsSimple, but most responses are empty and waste capacity
Long pollingServer holds the request open until a message arrives or a timeoutFewer empty replies; still one request per message and constant reconnects
WebSocket PickOne persistent, two way connection over TCPReal time both ways; servers become stateful and need reconnect handling

Use WebSockets for messages only. Login, profiles, group management and media uploads stay on ordinary stateless HTTP services behind a load balancer.

Whiteboard

What you sketch in the first five minutes, before any of the detail below. Draw the boxes, then walk one message through them out loud.

1. Alice to Bob

Say: chat servers hold open sockets, so they are stateful; everything else is an ordinary service.

Alicechatserver 1ID servicemessagespresenceBob's inboxchatserver 2Bobpushsocket12345onlineoffline: wake the phone23,000 messages/s, 10M open sockets
  1. Alice sends up her open WebSocket. The server gets an ID that sorts by time.
  2. Store the message first. Only then does Alice see one tick.
  3. Ask presence where Bob is. Online: his inbox queue, his chat server, his socket. Offline: a push notification.

Next question: what if Bob's chat server dies? His app reconnects to another one and fetches everything after the last ID it saw.

2. One order for everyone

Say: device clocks drift, so the order comes from IDs the server assigns.

one 64 bit ID per message0timestamp, 41 bitsmachinesequencems since an epoch, about 69 years1,024 servers4,096 per mssame ms, same server: sequence 0, then 1a later millisecond always makes a bigger IDsort by ID = sort by send timeBob'slaptopmessageseverything after the last ID I sawmissed messages, in order
  1. Put the time in the high bits, so a bigger ID means a later message.
  2. Add the machine number and a counter, so servers never need to agree on anything.
  3. Each device remembers the last ID it saw and asks for everything after it.

Next question: two people send in the same millisecond on different servers. Their order is decided by machine number: consistent for everyone, though not the true order.

How a message travels from Alice to Bob

Step through: Alice sends Bob a message

Message order with Snowflake IDs

Client clocks drift, so ordering comes from IDs the server assigns. A Snowflake ID packs a timestamp into the high bits, so sorting by ID sorts by time, and each generator can mint thousands of IDs per millisecond with no coordination.

  • Timestamp, 41 bits: milliseconds since a custom epoch, enough for about 69 years.
  • Machine, 10 bits: up to 1,024 generators.
  • Sequence, 12 bits: 4,096 IDs per millisecond per generator, reset every millisecond.
ID
n/a
Time
n/a
Machine
n/a
Sequence
n/a

Message storage, presence and group chats

Message store

Chat is write heavy and reads are mostly recent. A wide column store (Cassandra, HBase, ScyllaDB) partitioned by conversation_id and sorted by message_id serves “latest 50 messages” from one partition. Discord runs this model on ScyllaDB, and Facebook Messenger ran it on HBase for years.

Multi-device sync

Each device keeps a cursor: the largest message ID it has. On reconnect it asks for everything after that ID, which also covers offline delivery.

Presence

Clients send a heartbeat every 5 seconds. A user is marked offline only after 30 seconds of silence, so a train through a tunnel does not flicker their status. Changes are published to friends over pub/sub; in large groups, presence is fetched only when someone opens the member list.

Groups

Small groups copy each message into every member’s inbox queue. Large channels store once and let members pull, like celebrities in the news feed.

What the interviewer probes next

A chat server crashes. What happens to its users?

Their sockets drop, and clients reconnect with backoff and jitter; service discovery (ZooKeeper, etcd) hands them another server. Messages were persisted before delivery, and clients resend anything unacknowledged with the same client message ID, which the server deduplicates.

Two people in a group send at the same moment. Who comes first?

Whoever the server assigns the lower ID. Using one ID source per conversation gives every member the same order; device timestamps are only for display.

How does end to end encryption change the design?

Keys live only on devices (the Signal protocol), so servers store and forward ciphertext. Server side search and message previews in push notifications no longer work unless the device decrypts them locally.

How do you deploy without dropping 10M connections?

Drain servers one at a time: stop accepting new sockets, ask connected clients to reconnect gradually, then restart. The load balancer must support long lived connections and not cut them on a fixed timeout.

Building blocks used here

Want to run this as a mock interview? See interview prep.

© 2026 Arman Ajabkhani · armantik.com · Privacy