Skip to content
BytePatterns

Design a Chat App

System Design Cases: lesson 3 of 20

Finding the socket is harder than sending the message.

Lesson 3 of 20 · 7 min

Design a Chat App

Step 1 of 12

A million phones, each holding one open socket to one of many gateways.

The Idea

Deliver a message in under a second and let any device read it back later. Assume one million connected users and 50 000 messages a second at peak, so every phone holds an open socket to one of many gateways.

Real-World Example

A hotel switchboard. Rooms never dial each other directly: the board knows which line each guest is on, connects the two, and writes the message down when nobody picks up.

The Tradeoff

Persisting the message before acknowledging puts a write on the hot path, and it is the only reason delivery survives a crashed gateway. Acknowledging first is faster and loses messages. The registry mapping user to gateway is the piece that goes stale the moment a phone drops off a train.

Your turn

Put the steps in the right order.

  1. Ask the session registry which gateway holds the recipient
  2. Persist the message and give it a sequence number
  3. Push it down that socket, or queue a notification if there is none
  4. The sender's open socket carries the message to its gateway

Mini quiz

1 / 3

Storing the message before acknowledging the sender costs a write on the hot path, and buys:

New lessons land every few weeks

Leave an address and we will tell you when the next one is up. That is the only reason we will use it.

One address, stored so we can email you. Nothing else, ever.