Design a chat app (like WhatsApp)

One-to-one and group messaging, with delivery receipts and online status.

Why interviewers ask it

It tests real-time connections, routing between servers, storage for enormous write volumes, ordering and offline delivery.

Functional requirements

Non-functional requirements

From version 1 to the final design

  1. Version 1: one server; clients poll a database for new messages.
  2. Version 2: WebSockets to that server, pushing instead of polling.
  3. Version 3: many gateway servers plus a registry of who is connected where, with messages routed between gateways.
  4. Version 4: a partitioned message store, push notifications for offline users, receipts, presence and groups.

The design, step by step

  1. Each client keeps a WebSocket connection to a chat gateway server.
  2. Sending: gateway → chat service gives the message a time-ordered ID and stores it, then routes it.
  3. Routing: look up the recipient's gateway in Redis and push to it; if they're offline, keep it stored and send a push notification.
  4. Groups: send a copy to each member (small groups), or have members fetch it (very large groups).
  5. Receipts and presence are small events that travel the same path.

Deep dives

Common mistakes

Not true: Delivery: clients poll every few seconds, which scales well to millions of users.
In fact: Almost every poll would come back empty, yet each one costs a request. Keep a WebSocket open to each client, and push messages as they arrive.

In the interview

Clients hold WebSocket connections to gateway servers, and Redis maps each user to their gateway. The chat service stores every message in a Cassandra-style store, partitioned by conversation, with a time-ordered ID. Then it routes the message to the recipient's gateway. If they're offline, it keeps the message and sends a push notification. Client-generated message IDs make retries safe.

Part of Unit 8: High-level design questions in the free System Design course.