Real-time chat with delivery receipts
A support tool polls for messages every few seconds. Customers now expect real-time chat with groups and read receipts.
To design real-time chat, keep persistent connections for delivery and a durable message store as the source of truth. Route each message to the gateway holding the recipient, order messages with per-conversation sequence numbers, make sends idempotent with client IDs, and batch receipts so groups do not flood the system.
The system today: A customer-support tool has a chat box. The browser asks the server for new messages every five seconds, and messages are rows in one SQL table. It works for a few hundred concurrent users.
What has to change: Rebuild it as real-time chat for 2 million concurrent users: one-to-one and group conversations, messages that arrive in order, sent and delivered and read receipts, and no lost messages when someone is offline or reconnects.
Questions to ask first
- How large can a group be, and is there a message size limit?
- Do messages need end-to-end encryption, or is server-side storage fine?
- How long do we keep history, and does it sync across a user's devices?
- What ordering guarantee do we need: per conversation, or global?
Where the interviewer digs in
- Persistent connections (WebSockets) and routing a message to the server that holds the recipient's connection
- Ordering and idempotency: sequence numbers per conversation and retries
- Offline delivery and receipts without a write storm
What a strong answer covers
- Uses persistent connections for delivery and keeps a durable store as the source of truth, so a dropped connection never loses a message.
- Handles routing: a presence or session registry that says which gateway server holds each user, and a pub/sub layer between gateways.
- Orders messages with a per-conversation sequence number and makes sends idempotent with a client-generated ID so retries do not duplicate.
- Explains receipts as small events batched on the client, and group fan-out limits for large groups.
Common mistakes
- Relying on timestamps for ordering across devices with different clocks.
- Treating the connection as the store: a message only in memory on a gateway is lost when it restarts.
- Sending a read receipt per message per person in a large group.