Quick revision
The short answer
Clients keep a WebSocket connection to a gateway tier and send messages as frames carrying a conversation id, a client message id and the body; the server stores the message under (conversation_id, seq) and acks with that sequence number, while history and conversation lists use plain HTTP. For an online recipient, a session registry tells the message service which gateway holds their connection, and the gateway pushes the message and waits for the device's ack before marking it delivered. For an offline recipient, the message stays stored, a push notification goes out, and on reconnect the client fetches everything after its last seen sequence number.
What to remember
In this answer, we designed a chat service for 50 million daily users and about 2 billion messages a day. Clients use WebSocket frames to send, acknowledged with a server sequence number, and HTTP for history. Messages are stored by (conversation_id, seq) in a wide-column store, with read state as per-user cursors. Gateways hold connections, a session registry finds an online recipient's gateway, and the message service stores first and routes second. Offline recipients get a push notification and catch up from their last seen seq, and client message ids make retries idempotent.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Clients keep a WebSocket connection to a gateway tier and send messages as frames carrying a conversation id, a client message id and the body; the server stores the message under (conversation_id, seq) and acks with that sequence number, while history and conversation lists use plain HTTP. For an online recipient, a session registry tells the message service which gateway holds their connection, and the gateway pushes the message and waits for the device's ack before marking it delivered. For an offline recipient, the message stays stored, a push notification goes out, and on reconnect the client fetches everything after its last seen sequence number.
Why this question is asked
A chat service looks simple, a box and a send button, but it combines several hard problems: millions of open connections, a high write rate, ordering, delivery receipts and history across devices. That is why it is one of the most common system design questions. At this level, interviewers want a clean API, a data model that fits the access patterns, sensible numbers and a clear delivery path. In this answer, we will set the requirements and rough numbers, define the API, design the data model, draw the components, and then follow one message to an online recipient and one to an offline recipient, finishing with how retries stay safe.
1Requirements and Rough Numbers
The service must support one-to-one and small group messages from phones and browsers, show sent, delivered and read states, and keep history so a new device sees past conversations. Messages should arrive within a fraction of a second when the recipient is online, and must never be lost once the sender sees them as sent.
50 million daily users sending 40 messages each is 2 billion messages a day. Divided by 86,400 seconds, that is about 23,000 writes per second on average, and peaks are usually several times the average, so we plan for around 100,000 writes per second. If a message with its metadata is about 200 bytes, that is about 400 GB of new data a day, well over 100 TB a year before replication. If a quarter of daily users are online at the busiest moment, that is about 12 million open connections, which at 50,000 connections per gateway server means a few hundred gateway servers.
2Defining the API
Real-time traffic travels over a persistent WebSocket connection, a long-lived two-way connection over which both client and server can send frames at any time. A client opens it to the gateway tier when the app starts and authenticates once. Sending is a frame; the server replies with an ack carrying the server's sequence number. Everything that is a plain request and response, such as loading history or the conversation list, uses ordinary HTTP.
Notice the client_msg_id. The client generates it, a UUID, before sending. We will see at the end why it makes retries safe.
3Designing the Data Model
The most frequent read is open a conversation and show its latest messages, and the next is give me everything after sequence N. So messages are stored keyed by (conversation_id, seq): the conversation id is the partition key, which keeps a conversation's messages together on the same nodes, and seq orders them inside the partition. Loading history becomes one scan of one partition. At 100,000 writes per second with this simple access pattern, a wide-column store such as Apache Cassandra or ScyllaDB fits well: it spreads partitions across many nodes and handles high write rates. Very busy conversations can be split further into time buckets within the partition key.
Next to messages, we keep conversations and conversation_members, and a per-user table of the user's conversations ordered by last activity for the conversation list. Delivery and read states are stored per user and conversation as two numbers, last delivered seq and last read seq, rather than as a flag on every message. Since messages in a conversation are ordered, marking seq 1044 as read means everything up to 1044 is read, which is one small write instead of many.
| Table | Key | Holds |
|---|---|---|
| messages | (conversation_id), seq | sender, client_msg_id, body, sent_at |
| conversation_members | (conversation_id), user_id | role, joined_at |
| user_conversations | (user_id), last_activity | conversation_id, preview |
| read_state | (user_id), conversation_id | last_delivered_seq, last_read_seq |
4The High-Level Components
Gateways hold the WebSocket connections and nothing else: they authenticate, forward frames to the message service and push frames down to clients. Keeping them thin lets us restart and scale them easily. A session registry, for example in Redis, maps each user id to the gateways holding that user's connections, with an expiry refreshed by heartbeats. The message service assigns sequence numbers, stores messages and routes them. The history service answers the HTTP reads. A push service sends notifications through the phone platforms' push services, Apple Push Notification service and Firebase Cloud Messaging.
- Phone and browser clientsWebSocket for live traffic, HTTP for history
- Gateway tierholds connections, authenticates, pushes frames
- Message serviceassigns seq, stores, routes to recipients
- Session registry (Redis)user id -> gateways, expires without heartbeats
- Message store (Cassandra or ScyllaDB)messages by (conversation_id, seq), read state
- Push serviceAPNs and FCM notifications for offline users
6 layers, from the top down. Step through them one at a time.
5Following a Message to an Online and an Offline Recipient
Alice sends a message to Bob. Her gateway forwards it to the message service, which first stores it with the next sequence number for the conversation, then acks Alice: from this moment the message is durable, so her app shows it as sent. Storing before routing is the key rule, because a message that was routed but not stored could be lost in a crash.
For an online Bob, the message service looks him up in the session registry, finds his gateway and sends the message there. The gateway pushes the frame to Bob's device and waits for the device to reply with a delivered frame. Only then is Bob's last delivered seq updated, and Alice sees delivered. When Bob opens the conversation, his app sends read, and Alice sees read.
For an offline Bob, the registry has no entry. The message is already stored, so nothing is lost. The push service sends a notification through APNs or FCM, which shows the message preview on his phone. When Bob's app reconnects, it asks for each conversation's messages after its last seen seq, receives everything it missed in order, and sends delivered for them.
8 messages in order. Play them, or step through them one at a time.
Store first, then route; an offline recipient catches up from the store.
6Making Retries Safe
Networks drop things. Alice's message may be stored, but the ack lost on the way back. Her app then sends the same message again. Without protection, Bob would see it twice. The client_msg_id prevents this: the message service remembers recent (conversation_id, client_msg_id) pairs, and if it sees a pair it has already stored, it simply returns the original seq instead of storing again. The send operation is idempotent, which means doing it twice has the same effect as doing it once.
7Follow-up questions
How do messages to a 500-member group change the write and delivery path?
The write stays the same: one message stored once in the group's partition. Delivery changes, because one message now fans out to 500 members. The message service looks up all members' gateways, groups them by gateway and sends one batch per gateway rather than 500 separate calls. Offline members get push notifications, often throttled or collapsed into one notification per conversation. Read receipts change too: showing who read every message to 500 people is expensive, so large groups usually show read counts or receipts only on request. For very large groups or channels, members often pull new messages when they open the conversation instead of having each one pushed.
Why not store delivered and read flags on every message?
Because a group message would need one flag per member, so each read receipt would update many rows. Since messages are ordered per conversation, one number per user and conversation, last read seq, says the same thing in one small write.
8Conclusion
In this answer, we designed a chat service for 50 million daily users and about 2 billion messages a day. Clients use WebSocket frames to send, acknowledged with a server sequence number, and HTTP for history. Messages are stored by (conversation_id, seq) in a wide-column store, with read state as per-user cursors. Gateways hold connections, a session registry finds an online recipient's gateway, and the message service stores first and routes second. Offline recipients get a push notification and catch up from their last seen seq, and client message ids make retries idempotent.
Open with
I would start with numbers: 50 million users times 40 messages is 2 billion a day, about 23,000 writes per second on average and maybe 100,000 at peak, and roughly 12 million open connections at the busiest time.
Then, if they want more
Clients keep a WebSocket to a gateway tier. Sending is a frame with conversation id, a client-generated message id and the body, and the server acks with a sequence number. History and conversation lists use plain HTTP, like GET messages before or after a seq.
For data, messages are keyed by conversation id and seq in a wide-column store like Cassandra, so a conversation's history is one partition scan, and read and delivered states are per-user cursors, last delivered and last read seq.
The flow: the message service stores the message first, acks the sender, then checks a session registry in Redis that maps users to gateways. If the recipient is online, their gateway pushes the frame and waits for the device's ack before marking it delivered.
If they are offline, the message stays stored, we send a push notification through APNs or FCM, and on reconnect the client fetches everything after its last seen seq. Finally, deduplicating on the client message id makes retries idempotent, so a resend after a lost ack is not stored twice.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Define the API, the main data model and the high-level components of a chat service for...": https://scrivio-interview-library.netlify.app/architecture/system-design-problems/b492b5c6c7/