What happens betweenAna’s phone and Ben’s?
Design a messaging system step by step, from one server to a million people online, one decision per chapter. Server and client both, in English and Chinese.
A messaging system does two things: receive every message safely, then deliver it to every device, including one that comes back after being offline.
Not drawn: voice and video calls, end-to-end encryption, multi-region failover, retention and compliance, search.
- Send: Ana’s message enters the IM over her connection
- Once stored, reply “sent” (not “delivered”): Ana waits for nothing after this
- Push to Ben’s online devices; offline ones get a notification, only a hint
- Any device that missed something pulls to catch up when it returns
Point at or tap a layer to see its job, why it is hard, and its chapters.
Want every part and every hop? See the engineering diagram in chapter 0
Written so far: the opening (chapter 0), first draft.
Every chapter goes in this order
- The problemA concrete scene, such as “the same message appeared twice”.
- Watch it failRun the simple design in a simulator on the page.
- EstimateA number you can check on paper explains why it fails.
- Fix itSwitch the fix on in the same simulator and compare.
- The costWhat the fix costs, and what other products choose.
Why this exists
There are many articles about messaging architecture. Most show a final diagram, or one company’s history. Few explain, step by step, why a system ends up the way it does.
Here, one chat app grows from 1,000 users to 1,000,000 people online. The four layers and the business layer beside them start crowded into one program, and each step of growth forces one of them out and deeper.
The last chapters change the product (toB workplace chat, community servers, live rooms, a private messenger, customer service, an IM platform for other businesses) and show the same decisions flipping. There is no single right architecture: the product decides.
Chapter map
The trunk follows one app from v0 to v3; each stop gives its scale and why it has to grow. Then a focus on reliability, and finally the branches change the product. The dot before each chapter is the layer it is mostly about, in the same colours as the diagrams.
connectionmessagedispatchstoragebusinessclientall layersBlue titles: published (first drafts for now)
Opening
See the whole system first.
v0: one server
1,000 users
“We want chat in our app.” Make it work first.
- 1The smallest chat
- 2Push instead of poll
v1: correct on phones
100,000 users
On mobile networks, messages get lost, repeated and out of order.
- 3Acks and retries
- 4Duplicates
- 5Ordering
- 6Offline sync
- 7The local database
- 8Images and files
- 9v1: a reliable channel
v2: groups and devices
100,000 users, groups of 500
Product adds groups of 500 and a desktop app.
- 10Fan-out
- 11Joining and leaving a group
- 12Who may message whom
- 13Unread counts and read receipts
- 14Many devices
- 15Waking an offline phone
- 16Recall and edit
- 17The conversation list
- 18v2: groups at 100,000 users
v3: many servers
1,000,000 online
One server cannot hold them, and every deploy disconnects everyone.
- 19Split the gateway
- 20Presence
- 21A million connections
- 22Heartbeats
- 23Reconnect storms
- 24Accounts: the app’s users and the IM’s
- 25App lifecycle
- 26The web client
- 27Sharing out the conversations
- 28A queue before storage
- 29One SDK, many platforms
- 30Old apps, new servers
- 31v3: a million online
Focus: reliability
Once the system is big, the hard part is staying up: knowing messages arrive, and losing nothing through overload, deploys, failing dependencies and a lost data centre.
- 32Where did my message go?
- 33Overload: limits and degradation
- 34Deploys without disconnects
- 35When a dependency fails
- 36Losing a data centre
- 37Drills and postmortems
Branches: other products
The same system for toB, communities, live rooms, customer service, a platform: which decisions flip?
- 38Workplace chat (toB)
- 39Community servers
- 40Live rooms
- 41Private messenger
- 42Customer service
- 43IM as a platform
- 44Same decisions, different products
Side trips: on the wireoptional
Optional reading on the protocol.
- W1Framing
- W2Encoding
- W3TCP or UDP
- W4Group encryption