Asked at
URL Shortener (TinyURL)
Asked from Junior level upFoundations & Storage
Distributed Rate Limiter
Asked from Mid level upAPI Gateway & Security
Distributed Key-Value Store
Asked from Senior level upFoundations & Storage
Distributed Notification Service
Asked from Mid level upMessaging & Queues
Distributed Web Crawler
Asked from Mid level upDistributed Systems
Metrics & Monitoring Platform
Asked from Senior level upObservability & Time-Series
Real-Time Chat App (like WhatsApp or Slack)
Asked from Mid level upReal-Time & WebSockets
Social News Feed (like X/Twitter)
Asked from Mid level upSocial & Feed Systems
Ride-Hailing App (like Uber)
Asked from Senior level upGeospatial & Real-Time
Low-Latency Stock Exchange Matching Engine
Asked from Staff level upFintech & Low Latency
Global Video Streaming Platform (like YouTube or Netflix)
Asked from Senior level upMedia & Streaming
Distributed Hotel & Ticket Reservation System
Asked from Mid level upE-Commerce & Transactions
Shared Document Editor (like Google Docs)
Asked from Staff level upReal-Time & Distributed State
Game Leaderboard
Asked from Junior level upCaching & Data Structures
File Sync Service (Dropbox / Google Drive)
Asked from Mid level upStorage
Payment System
Asked from Senior level upPayments & Transactions
Ad Click Aggregation
Asked from Senior level upStream Processing
AI Support Assistant (RAG)
Asked from Senior level upAI Systems
LLM Inference Platform
Asked from Senior level upAI Systems
Enterprise Semantic Search
Asked from Mid level upAI Systems
AI Code Completion
Asked from Senior level upAI Systems
LLM Gateway
Asked from Senior level upAI Systems
AI Agent That Takes Actions
Asked from Senior level upAI Systems
LLM Evaluation Platform
Asked from Senior level upAI Systems
AI Document Processing Pipeline
Asked from Mid level upAI Systems

Design a Real-Time Chat App (like WhatsApp or Slack)

Asked from Mid level upDomain: Real-Time & WebSockets
Design it: check it against goals
Concepts to know first:

Design a 1-on-1 and group chat system with online presence, read receipts, and offline message synchronization.

An analogy

Think of a chat system like a walkie-talkie relay tower. When you press speak, your voice goes straight out over an open radio channel to your friend, without waiting for an email reply.

Where this design ends up. This is version 3 of 3. Part 5 builds it up from version 1 and says why each part was added.
  1. Users
    • Alice (Sender)
    • Bob (Receiver)
  2. Edge WS Fleet
    • WS Gateway A
    • WS Gateway B
  3. Message Bus
    • Redis Pub/Sub
  4. Storage
    • Cassandra Cluster

1. Requirements and scope

Functional Requirements
  • 1-on-1 and group messaging (up to 500 members).
  • Real-time message delivery with online/offline presence tracking.
  • Read receipts and push notifications for offline users.
Non-Functional Requirements & SLAs
  • Low latency: an example target is under 100 ms from sender to receiver.
  • No message lost once the server has acknowledged it.
  • End-to-end encryption support.
Key Traffic Assumptions: 500 Million DAU, 10 Billion messages sent daily (~115,000 messages/sec average).

2. Back-of-the-envelope numbers

Writes and reads each second
  • Writes: 115,000 messages a second, on average (28%)
  • Reads: 300,000 reads a second (72%)

Balanced: reads and writes are close. Both paths need care, so neither can be slow.

Write Throughput
115,000 messages/sec (Avg), 300,000 messages/sec (Peak)
Read Throughput
300,000 reads/sec
Storage Growth
10B msgs/day × 200 bytes × 365 = ~730 TB / year
RAM Cache Footprint
Redis records which gateway server each user is connected to (user_id to gateway), for 500M users

3. API

WS/ws/chatA WebSocket connection that carries messages both ways, plus small heartbeats that show who is online
POST/api/v1/messagesFallback HTTP REST message dispatch

4. Data model and storage

Storage Strategy: Apache Cassandra / ScyllaDB for chat history + Redis for user presence & session gateway mapping.
MESSAGES: (channel_id, message_id, sender_id, content, created_at) PRIMARY KEY (channel_id, message_id DESC)

5. The design, in three versions

Version 1: HTTP Polling with Relational DB

Clients poll server every 2 seconds for new messages.

Chat v1: HTTP Polling Server Version 1 of 3

The picture follows one request through the parts of this version, in order. Under it, switch a part off to see what the request does without it.

One request, step by step

Each numbered arrow is one step. Press "Simulate Request Flow" to follow them one at a time.

Alice (Sender)Bob (Receiver)WS Gateway AWS Gateway BRedis Pub/SubHBase / Mongo12345
  • The order of the steps
  • A request or its reply
  • Background work: the answer does not wait for it

The parts: switch one off

1. Users

Alice (Sender)WebSocket Client
Bob (Receiver)WebSocket Client

2. Edge WS Fleet

WS Gateway AAlice Connection
WS Gateway BBob Connection

3. Message Bus

Redis Pub/SubCross-Node Routing

4. Storage

HBase / MongoChat Message History

The steps in words0 of 5 steps shown

  1. Alice to WS Gateway AWS Frame · 8ms

    SEND { to: "Bob", text: "Hi!" }

  2. WS Gateway A to Redis Pub/SubPUBLISH · 2ms

    Publish to channel:user_bob

  3. Redis Pub/Sub to WS Gateway BMESSAGE · 1.5ms

    Broadcast to Bob socket

  4. WS Gateway B to BobWS Frame · 7ms

    Push message to Bob screen

  5. WS Gateway A to Chat DBAsync Write · 12ms

    Append to Cassandra message table

Why this version
Simple implementation.
Next bottleneck
Every poll opens an HTTP request, and most polls come back with no new message.

Write path, read path and failures

Each path follows one request through the final design (version 3), one hop at a time.

Write pathSend a message

  1. Sender
  2. WebSocket server
  3. Kafka
  4. Chat history
  5. Presence store
  6. Recipient
  1. Sender to WebSocket server

    Asha's app sends the message over its open WebSocket connection.

  2. WebSocket server to Kafka

    The server gives the message an id and writes it to Kafka, in the partition for this chat. Kafka keeps the chat's messages in order.

  3. WebSocket server to Sender

    Once Kafka confirms, the server tells Asha's app "sent".

  4. Kafka to Chat history

    A worker saves the message to the chat history in Cassandra.

  5. Kafka to Presence store

    A delivery worker looks up which server holds Ben's connection.

  6. WebSocket server to Recipient

    That server pushes the message down Ben's connection. If Ben is offline, a push notification goes out.

Read pathOpen a chat and load history

  1. Recipient
  2. WebSocket server
  3. Presence store
  4. Chat history
  1. Recipient to WebSocket server

    Ben opens the app. It connects and sends the id of the last message it has.

  2. WebSocket server to Presence store

    The server records that Ben is online and which server holds his connection.

  3. WebSocket server to Chat history

    It reads the messages after that id, the newest 50 first.

  4. WebSocket server to Recipient

    It sends the missed messages. Older ones load as Ben scrolls up.

What breaks, and what happens then

  • A WebSocket server crashes

    What users see
    Every user on it is disconnected.
    What the design does
    Apps reconnect to another server and send their last message id. Missed messages are read from history, so none are lost.
  • The recipient is offline

    What users see
    The message cannot be pushed down a connection.
    What the design does
    It stays in the history. A push notification is sent, and the app fetches the message when it next connects.
  • A message is sent twice after a retry

    What users see
    The chat could show a duplicate.
    What the design does
    The app puts a unique id on each message. The server and the receiving app drop an id they have already seen.
  • The presence store is down

    What users see
    Servers cannot find who holds a connection.
    What the design does
    Messages are still saved. Delivery falls back to push notifications and to the fetch on reconnect.

6. Deep dives

Message Ordering & Snowflake ID Generation
Clocks on different servers disagree, so timestamps cannot order a chat. Snowflake IDs (41-bit timestamp, 10-bit node ID, 12-bit sequence) are unique and roughly time-ordered. But IDs from two nodes can still come out of order when their clocks differ. So route each channel to one owner, such as the partition for its channel ID, which hands out increasing IDs. Each channel is then in strict order.

7. Follow-up questions

  • How do you manage presence for 500M users without overwhelming Redis? (Answer: Clients send heartbeats every 30 seconds; heartbeat failure marks offline after 60s timeout; presence checks are pulled on-demand rather than broadcasted globally).

What it costs

Open connections and message storage drive the cost: millions of WebSocket connections need many gateway servers, and history grows by about 730 TB a year. Media files cost more than text, so store them in object storage, not in the database.

Another way to design it

A small chat product can use long polling and one SQL database. It is far simpler to run. It runs out when users expect instant delivery, or when one database can no longer hold the history.

8. How answers are judged

A passing answer at Mid level. A complete design with numbers: about 115,000 messages a second on average, and 300,000 at peak. It explains how chat history is stored and goes deep on one part, such as ordering or presence.

How each level is judged
What is judgedJuniorMidSeniorStaff
BreadthHow much of the system does the answer cover?Names the core parts: clients, servers, a database and a cache.Covers the whole main flow, from the request to the stored data and back.Builds a complete design without being led through it.Covers the system, the systems around it and who owns each part.
DepthHow far does it go into the hard parts?Explains what each part does and why it is there.Goes deep on one part, such as the database or the cache.Goes deep on the two or three riskiest parts.Goes deepest where the risk is largest, and explains why the risk is there.
DrivingWho leads the conversation?Follows the interviewer, step by step.Moves forward alone, with an occasional nudge.Leads the conversation from start to finish.Changes the problem when the question asked is not the right one.
Trade-offsAre choices compared and justified?Names more than one option.Says why one option was picked over another.Compares options in detail, with what each one costs.Ties each choice to cost, speed and risk for the business.
Unclear questionsWhat happens when the question is vague?Answers the questions that are asked.Asks questions to make the problem clear.Sets the scope and says what is left out.Questions the assumptions behind the problem itself.
ScopeWhat does it consider beyond the normal path?Covers the normal path, when everything works.Adds what happens when a part fails.Adds growth: what breaks at ten times the load, and the fix.Adds abuse, cost and the daily work of running the system.

Requirements and scope

Strong answer
Before I design, I want to check the scope. Chats can be 1-on-1 or groups of up to 500 people. Do offline users need every message once they reconnect? Do we need read receipts and end-to-end encryption? I will assume messages must never be lost, even if delivery is delayed.
Weak answer
Users send a message from their phone, and it goes straight to the other person's phone over a live connection. Let me start sketching the WebSocket servers and the database.

Shows: Unclear questionsThe difference: The weak answer skips whether groups, offline delivery and encryption are in scope, so the design might not fit real chat needs.

Estimates

Strong answer
Ten billion messages a day works out to about 115,000 a second on average, matching the 300,000 peak given. At 200 bytes a message, a year of history is about 730 TB, so Cassandra fits the write pattern better than a relational database.
Weak answer
Ten billion messages a day is a huge number. So the gateway fleet needs to accept about ten billion connections at once, and we should size hardware for that many live sessions.

Shows: DepthThe difference: The weak answer treats the daily total as connections needed at once. The strong one converts it to a rate: about 115,000 messages a second.

Core design

Strong answer
Each message is written to a table keyed by channel ID, then message ID, so I can fetch the newest messages for one channel fast. A cache maps each connected user to the gateway server holding their socket, so a message finds the right server to push through.
Weak answer
I would store every message as a row in one table, with a unique message ID as the key. Recent messages come from querying by that ID.

Shows: DepthThe difference: A table keyed only by message ID cannot fetch one channel's recent messages fast; it needs a scan. Keying by channel ID first fixes that.

Deep dive

Strong answer
The hard part is ordering, since each server's clock drifts, so stamps from many servers can disagree. I would give each channel one sequencer. Kafka is partitioned by channel ID, so one worker owns each channel's messages, and it gives each message the next number for that channel. Clients sort by that number, not by any clock.
Weak answer
Each of the many gateway servers stamps a message with its own clock when it arrives. I sort messages for a channel by those timestamps before showing them to users.

Shows: DepthThe difference: The weak answer trusts many clocks, which drift apart, so a channel can show messages out of order. One sequencer per channel gives one increasing number instead.

Failures and trade-offs

Strong answer
If a gateway server crashes, sockets on it drop, so messages must be written to Kafka before we tell the sender it sent. A worker then delivers each message from Kafka and stores it in Cassandra, so offline users still get it once they reconnect.
Weak answer
If the recipient is offline, we push through Apple or Google's notification service. We keep retrying until it succeeds, so the message always gets through in the end.

Shows: ScopeThe difference: Retrying the push alone does not survive a gateway crash before the message is saved, so it can still be lost. Writing to Kafka first fixes that.

We wrote this guide in our own words, informed by public interview guides such as AlgoMaster and Hello Interview. We are not connected to either site. Interviewers differ. The guide and the example answers show the direction each level takes, not a checklist.

9. One problem, four levels

The same question, "design a chat system", answered at four levels. Each column shows only what that level adds to the one before it.

What each level adds, part by part
Part of the answerJuniorMid addsSenior addsStaff adds
Requirements and scope
  • A working design lets one person message another over a live connection, and shows whether they are online.
  • Splits must-haves (delivery, presence) from qualities: no message may be lost, and group chats can hold up to 500 people.
  • Asks about offline delivery, read receipts and end-to-end encryption before designing, and says which of these this answer leaves out.
  • Questions whether every message needs end-to-end encryption before designing. Encrypted history cannot be searched or moderated, which changes what the design must support.
Estimates
  • Says the system must handle far more delivered messages than messages sent, since group chats deliver one message to many members.
  • About 115,000 messages a second on average, and 300,000 at peak, with about 730 TB of history stored a year.
  • Uses the numbers to decide: database reads (300,000 a second) nearly match peak writes, so storage must suit heavy writes. Group fan-out adds deliveries over live connections, not database reads.
  • Adds cost: 730 TB a year is cheap to store, so the money goes on connection servers and moderating abuse in large groups.
Core design
  • A live connection per user, an API server, and one table that stores each conversation's messages, with a way to show who is online.
  • Messages are keyed by conversation, then by their own ID, so recent messages for one conversation load without scanning the whole table.
  • App servers are stateless, while connection servers hold only live sockets, with a cache mapping each user to their server. Fan-out spreads a large group's messages across many workers.
  • Fans out small groups on write for fast delivery. A very large group instead reads its recent messages on demand, without writing to every member.
Deep dive
  • Explains why a live connection helps: a message reaches the other person right away, without them needing to ask for it.
  • Explains presence: each device sends a heartbeat every 30 seconds, and missing two in a row marks that user offline.
  • Goes deep on ordering across servers: clocks drift between machines, so one sequencer per channel numbers every message in order. That is the owner of the channel's Kafka partition.
  • Goes deep on presence at scale: checks are pulled on demand instead of broadcasting online status to every contact for hundreds of millions of users.
Failures and trade-offs
  • Says messages are saved right away, so they are not lost if the app closes before sending finishes.
  • If a connection drops, the app reconnects and asks for any messages sent while it was away.
  • If a connection server crashes, messages already written to a queue are still delivered, so offline users get every message once they reconnect.
  • Weighs a push notification service to wake sleeping phones against its cost and the risk that a vendor outage delays every offline alert.
Whole answer at:

Requirements and scope

  • A working design lets one person message another over a live connection, and shows whether they are online.
  • New at MidSplits must-haves (delivery, presence) from qualities: no message may be lost, and group chats can hold up to 500 people.

Estimates

  • Says the system must handle far more delivered messages than messages sent, since group chats deliver one message to many members.
  • New at MidAbout 115,000 messages a second on average, and 300,000 at peak, with about 730 TB of history stored a year.

Core design

  • A live connection per user, an API server, and one table that stores each conversation's messages, with a way to show who is online.
  • New at MidMessages are keyed by conversation, then by their own ID, so recent messages for one conversation load without scanning the whole table.

Deep dive

  • Explains why a live connection helps: a message reaches the other person right away, without them needing to ask for it.
  • New at MidExplains presence: each device sends a heartbeat every 30 seconds, and missing two in a row marks that user offline.

Failures and trade-offs

  • Says messages are saved right away, so they are not lost if the app closes before sending finishes.
  • New at MidIf a connection drops, the app reconnects and asks for any messages sent while it was away.

10. Check your design with your own AI

Write your design first. Then copy this prompt into the AI chat you already use, and add your design at the end. It asks for a score in each area, one fix for each, and two follow-up questions.

The site sends nothing anywhere. An AI review can be wrong, so check what it says against sections 8 and 9.

See the prompt
You are a system design interviewer. Review my design for the interview problem below.

Problem: Design a Real-Time Chat App (like WhatsApp or Slack)
Design a 1-on-1 and group chat system with online presence, read receipts, and offline message synchronization.

What users must be able to do:
- 1-on-1 and group messaging (up to 500 members).
- Real-time message delivery with online/offline presence tracking.
- Read receipts and push notifications for offline users.

How well it must work:
- Low latency: an example target is under 100 ms from sender to receiver.
- No message lost once the server has acknowledged it.
- End-to-end encryption support.

Numbers to assume:
- 500 Million DAU, 10 Billion messages sent daily (~115,000 messages/sec average).

Judge my design at the Mid level.
A passing answer at this level: A complete design with numbers: about 115,000 messages a second on average, and 300,000 at peak. It explains how chat history is stored and goes deep on one part, such as ordering or presence.

For each area below, say whether my design is strong, fine or weak. Give one reason and one fix for each:
1. Requirements and scope
2. Estimates
3. Core design
4. Deep dive
5. Failures and trade-offs

Then ask me the two follow-up questions an interviewer would ask next.
Do not write a full model answer for me.

My design:
[Describe your parts, how one request moves through them, and why you chose each one.]