Design a chat system
Deliver messages in real time, in order, to users who come and go.
- Choose a real-time transport and route messages between connection servers.
- Order messages within a conversation with sequence numbers.
- Deliver to offline users and track presence.
Requirements. One-to-one and small group chats, real-time delivery, message history across devices, online presence, and delivery to users who are offline. Non-functional: low latency, messages must never be lost once acknowledged, and ordering must be consistent within a conversation.
Connections, routing, and storage
- Transport: clients hold a persistent WebSocket so the server can push messages instantly. Long polling is a fallback.
- Stateful gateways: WebSocket servers keep connections open, so they are stateful. A session registry (for example in Redis) maps each online user to the gateway holding their connection. Keep business logic in separate stateless services.
- Routing: the chat service persists the message, looks up each recipient’s gateway, and publishes to it (via pub/sub). Offline recipients get a push notification.
- Storage: messages are partitioned by
conversation_idand ordered by a per-conversation sequence number - a natural fit for a wide-column store.
A worked example: a client holding messages 1, 2, 4 and 5 can see that 3 is missing, and asks the server for it.
Sequence numbers let clients detect gaps and sync on reconnect by asking for “messages after sequence N.” The server assigns them, because client clocks disagree. The server stores a message before acknowledging it to the sender, so an acknowledged message survives a crash. Presence uses heartbeats: a user who misses several heartbeats is marked offline. For group chats, the service fans each message out to every member’s gateway.
Practice: follow one message
Try it
Up to 15 XPWalk a message from end to end
Being able to do this out loud is the core of the interview answer. Predict each step before it happens.
Practice: debug a reconnect
Try it
Up to 15 XPRead a client sync log
A user says that messages went missing after their train left a tunnel. Click the line that explains it.
Where should the client have noticed that it needed to sync?
Put it together: a mock design
Try it
Up to 15 XPPresent a design out loud
Talk through your design as you would in the room, and walk through one message from end to end.
“Design a chat app like the ones you use every day.”
Testing: Real-time delivery
The question stays hidden until you start. Answer it out loud as you would in the room.
Key takeaways
Persistent connections make gateways stateful; a registry maps users to gateways.
Store before acknowledging, and order by server-assigned sequence numbers.
Offline users get push notifications and sync missed messages on reconnect.
Lesson quiz
6 questions · pass with 5 correct · up to 50 XP
Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.
Questions about this lesson
Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.
Loading posts…