Designbench

Lesson 7.2 · 25 min

Chat app

Size a messenger, see why polling wastes 98% of requests, lose messages on a flaky network until sequence numbers and client ids fix the screen, and tune presence on a train ride.

The idea in plain words

A chat app is a post office that must deliver instantly, never lose a letter, never deliver one twice, and keep every conversation in the same order on everyone’s phone and laptop.

The problem it solves

Messages must show up instantly on the other person’s screen, in the same order for everyone, exactly once, on every device, even when phones go offline.

How it works

  1. Each online device keeps an open connection (a WebSocket) to a gateway server, so the server can push.
  2. Sending: the message goes to the chat service, gets the next sequence number for that conversation, and is stored.
  3. Delivering: look up which gateways the recipients are connected to and push it there.
  4. Offline phones get a push notification and fetch what they missed when they reconnect.
  5. Each message carries a client id, so a resend after a bad connection is recognised and not shown twice.

Example

Asha sends “hi” on a train and the signal drops before she sees a tick. Her phone resends with the same client id; the server sees it already stored that id and just acknowledges. Ben sees “hi” once, numbered 1031, right after message 1030.

Key terms

WebSocket
A connection that stays open so the server can push messages to the app the moment they arrive.
Gateway
A server that holds many users’ open connections.
Sequence number
A counter per conversation that puts messages in one agreed order.
Presence
Showing who is online or typing.
Push notification
A message sent through Apple or Google to wake a phone that isn’t connected.

In this lesson you'll learn

Sections

  1. Requirements and numbers
  2. Reaching the client
  3. API and data
  4. Architecture
  5. Deep dive: delivery
  6. Deep dive: groups and presence
  7. Design review

Cheat sheet

Server pushWebSocket per device
Orderseq per conversation
No duplicatesclient_msg_id + seq
Offlinepush wakes, then sync after last seq
Presenceheartbeat + grace; fetch for big groups

The interactive simulations and the scored design review load with JavaScript.