Loading
0xC0Lesson 13 of 13

Design a chat system

Deliver messages in real time, in order, to users who come and go.

20 min 6-question quiz
By the end of this lesson you can
  • 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_id and 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 XP

Walk 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.

Message 1 of 6Predicted 0/0
Ana
Gateway A
Chat service
Gateway B
Bo

Practice: debug a reconnect

Try it

Up to 15 XP

Read a client sync log

A user says that messages went missing after their train left a tunnel. Click the line that explains it.

Round 1 of 1: A gap in the sequenceScore 0/0

Where should the client have noticed that it needed to sync?

Client log for conversation 8841

Put it together: a mock design

Try it

Up to 15 XP

Present a design out loud

Talk through your design as you would in the room, and walk through one message from end to end.

Question 1

“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…