Design a Chat Application: WebSockets, Receipts and Ordering
From polling to WebSockets, how single, double and blue ticks really work, what happens when the receiver is offline, and how messages cross between chat servers.
A chat application lets users exchange messages in real time, one-to-one or in groups, and shows the status of each message: sent, delivered and read. Messaging apps display these as a single tick, a double tick and blue ticks.
The design combines real-time delivery, strict ordering, durability and a very large number of open connections. These notes cover the connection model, the path of a message, delivery receipts, offline users, routing between servers and ordering.
1. Clarify the requirements
- Functional: one-to-one chat and group chat with text messages.
- Functional: delivery status: sent, delivered and read.
- Functional: messages sent while the receiver is offline are delivered when they come back.
- Non-functional: real-time delivery, typically well under a second.
- Non-functional: no message is ever lost, and messages in a conversation appear in the correct order.
- Workload: every message is a write, so this system is write-heavy.
- Out of scope unless asked: media, voice and video calls, end-to-end encryption.
2. Estimate the scale
Assume 50 million daily active users sending 40 messages each. That is 2 billion messages per day, or roughly 20,000 per second on average and perhaps 60,000 at peak. At around 100 bytes per message, storage grows by about 200 GB per day. If 10 million users are online at once, the system holds 10 million open connections. If one chat server handles around 100,000 connections, that is at least 100 chat servers. These numbers tell us two things: we need a write-optimised message store, and connection management is a first-class problem.
3. Polling, long polling and WebSockets
With polling, each client asks the server for new messages on a timer. With millions of users asking every second, the server handles millions of requests per second, and almost all of them receive the answer 'nothing new.' It wastes server capacity, drains the phone's battery, and still delivers messages up to a second late.
Long polling improves this: the server holds the request open until a message arrives. It reduces empty responses but still requires a new request for every message, and it is awkward for sending data in both directions.
The right tool is a WebSocket. The client opens one connection to a chat server and keeps it open. It is persistent and bidirectional: the client can send at any time, and the server can push a message to the client the moment it arrives, with no request needed.
4. High-level design
Clients connect to stateful chat servers over WebSockets. Everything else, such as login, profiles and group management, goes through ordinary stateless HTTP services.
Alice --WebSocket--> Chat server 1 ---+ +--- Chat server 2 --WebSocket--> Bob
| | | ^
| v v |
| +--------------------------+ |
| | Pub/Sub (Redis / Kafka) | ----+
| +--------------------------+
v
+---------------------+ +-----------------------------+
| Message store | | Session registry (Redis) |
| (Cassandra-style) | | userId -> chat server id |
+---------------------+ +-----------------------------+
|
v receiver offline
Push notification service (APNs / FCM)messages ----------------------------------------------------------- conversation_id BIGINT -- partition key sequence_no BIGINT -- clustering key, ascending message_id UUID -- generated by the client, used for de-duplication sender_id BIGINT body TEXT created_at TIMESTAMP PRIMARY KEY (conversation_id, sequence_no) Query: "latest 50 messages of a conversation" = one partition, one range scan
5. The life of a message, and the three ticks
Alice sends a message to Bob. Her app sends it over the WebSocket with a client-generated message ID. The chat server writes it to the message store and only then sends an acknowledgement back to Alice. Her app shows a single tick: the server has the message and it will not be lost.
The server then delivers the message to Bob's device. Bob's app sends an acknowledgement that it received the message. The server forwards that event to Alice, and her app shows a double tick: delivered.
When Bob opens the conversation, his app sends a read event. The server forwards it to Alice, and the ticks turn blue. These acknowledgements are called delivery receipts, and they are just small messages flowing in the opposite direction.
The important ordering is store first, then acknowledge. If the server acknowledged before writing and then crashed, Alice would see a tick for a message that no longer exists.
6. Offline receivers
If Bob has no active connection, the message is already safe in the message store, marked as undelivered for him. The system sends a push notification through the platform's push service so that his phone shows an alert. When Bob's app comes online and reconnects, it tells the server the last sequence number it has for each conversation, and the server sends everything after that. The same mechanism handles a brief network drop and a week-long absence.
7. Routing between chat servers
With a hundred chat servers, Alice and Bob are almost certainly connected to different ones. Chat server 1 needs to know where Bob is. A session registry, typically in Redis, maps each online user to the chat server holding their connection. It is updated on connect and disconnect, with a heartbeat and TTL so that entries for crashed servers expire.
To hand the message over, servers use publish and subscribe. Each chat server subscribes to its own channel. Chat server 1 looks up Bob in the registry, finds chat server 2, and publishes the message to that server's channel. Chat server 2 receives it and pushes it down Bob's WebSocket.
8. Ordering and duplicates
Networks do not guarantee that messages arrive in the order they were sent, and clocks on phones cannot be trusted. So the server assigns each message a sequence number that increases within its conversation. Clients sort by sequence number, and a gap in the numbers tells a client that it is missing a message and should fetch it.
Clients retry when they do not receive an acknowledgement, so the server may see the same message twice. The client-generated message ID lets the server recognise the duplicate and return the original acknowledgement instead of storing it again. This makes sending idempotent.
9. Group chat
For a group, the server stores the message once in the group's conversation and then fans it out to each member: for every member, look up their chat server and deliver, or mark for push if offline. This works well for groups of a few hundred members. For very large groups or channels with hundreds of thousands of members, fan-out on every message is too expensive, and clients instead pull new messages when they open the group.
10. Additional considerations
- Presence (online and last seen): driven by heartbeats on the WebSocket and stored with a TTL; publish changes only to users who are currently viewing that contact.
- Load balancing: WebSocket connections are long-lived, so balance by least connections and drain servers gracefully before deployments.
- Storage: a write-optimised wide-column store fits the workload, partitioned by conversation so that a conversation's history is a single range read.
- Media: upload files to object storage and send only the URL in the message.
Interview summary
- Requirements: real-time, ordered, no message loss, write-heavy.
- Connection: WebSocket, not polling.
- Message store and receipts: persist first, then sent, delivered and read acknowledgements.
- Offline users: messages stay pending, a push notification is sent, the client syncs on reconnect.
- Scale: session registry, pub/sub between chat servers, per-conversation sequence numbers.
Summary
A chat system keeps one persistent WebSocket per client so the server can push messages instantly. Each message is stored before it is acknowledged, receipts travel back as small events, offline users are covered by pending messages and push notifications, a session registry with pub/sub routes messages between servers, and per-conversation sequence numbers keep everything in order.
Puneet Tiwari
Full Stack Developer
