BOOKS DEEP-READ · SYSTEM DESIGN INTERVIEW VOL 1 · CH 12

Design a Chat System

System Design Interview Vol 1 · Ch 12 · Alex Xu · 2020

中文 →

What is this chapter about?

You send someone a message on WhatsApp and it lands with a ping. This chapter is about building that ping. It looks simpler than the news feed — surely it is just carrying one sentence from A to B — but underneath sits a more basic obstacle: under the rules the web actually runs on, a server has no way to come find you.

A comparison: the post office only answers when you walk in

Think of the server as a post office with one rigid rule: it answers only when you walk in and ask "is there anything for me?" It will never knock on your door. So sending is no trouble at all — walk in, hand the letter over. Receiving is the hard part: how do you know when something has arrived?

Why the obvious ways hurt

The crudest approach is walking to the post office every few seconds to ask. It doesn't tire you; it tires the post office. With tens of millions of people asking, it fields tens of millions of questions a second, and ninety-nine times out of a hundred the answer is "nothing". A slight improvement is to ask and then refuse to leave — stand at the counter and wait, get your letter the moment it arrives, and if nothing comes, be sent home after a while so you can walk back. Fewer wasted trips, but you still shuttle back and forth constantly — and the clerk still cannot tell whether you went home or collapsed on the way.

The core idea: run a phone line that never hangs up

The modern answer is a third one: run a line between you and the post office that is never hung up. You still place the call (the rule is not broken), but once the line is open, either end can simply start talking — the post office can finally speak first. Messages arrive with a ping because that line is always open, so nothing has to be dialled again.

Three more things to manage around that line

One: what if you're not there. The line drops, so the message is kept on the server in an inbox that belongs to you; you collect it whenever you next come online. If it really can't wait, it goes out as a phone push notification — and that road belongs to somebody else.

Two: group chat. For a small group, the cheapest thing is to make N copies: one message, one copy dropped into every member's inbox. With few members the arithmetic is fine. With many, it collapses completely.

Three: knowing who is still there. You cannot call someone offline the instant their line drops — one wobble in a subway tunnel and their avatar goes grey, comes back, and flickers the whole way. So real systems use a heartbeat: whoever is online chirps every few seconds, and only after tens of seconds of silence are they treated as gone.

The honest cost: that always-open line is not free. The server must hold memory for every person online, one machine tops out at a few million lines — and when that machine needs a restart, everyone on it turns around and reconnects in the same second.

One line to remember

The hard part of a chat system is never sending, it is receiving. Sending is no different from loading a web page; but to let the server reach you first, you must first run it a line that never hangs up — and everything that follows (which machine that line lands on, where messages go while it is down, how a group copies them, how you tell someone is still there) is cleanup after that one line.

Want the stateful edge layer, message-ID ordering and the real numbers? → Switch to the deep read