SD-2 · Back to the write-up
Design a Chat App
Keep a WebSocket open to every phone, give each chat one ordered log, and retry each message until the phone confirms it.
Follow a request
Send a message
Deliver to an online phone
Deliver to an offline phone
A gateway crashes
Send a photo
Clients
Edge
Services
Data
PhonesAlice and Bob
Media + CDNEncrypted files
GatewaysHold WebSockets
Push serviceApple, Google
RegistryDevice → gateway
Chat serversStateless, many
Offline inboxUntil acked
Ordered logPartition per chat
Message storeSharded by chat
The numbers, at a glance
1 send ≈ 23K/s5 deliveries ≈ 115K/s
146 TBY1
292 TBY2
438 TBY3
584 TBY4
730 TBY5
0.5% of polls find a message99.5% come back empty
A message, from Alice to Bob
Bob is online. If he were not, the registry would say so and the message would go to his inbox.
Alice
Gateway A
Chat server
Ordered log
Registry
Gateway B
Bob
1 send a1f3
2 forward
3 append
4 seq 42
5 ack 42
6 ✓ sent
7 where is Bob?
8 gateway B
9 msg 42
10 msg 42
11 ack 42
12 ✓✓ delivered
RequestResponseAsync, does not block
How phones hear about new messages
The first decision, and the one the rest depends on.
every 2 s
anything new? → no
anything new? → no
anything new? → msg 42
+ Plain HTTP.− A second of delay; 99.5% of polls are empty.
request held open
msg 42 → reply
open a new request
+ Near real time over HTTP.− A new request after every message.
one open connection
← msg 42→ ack 42
← msg 43
+ Instant, both ways, one connection.− Gateways hold millions of connections.
How it grows
Add a box only when a number forces you to. Highlighted boxes are new at each stage.
Phones
Chat server
Database
Phones
Gateways × N
Chat × NRegistry
Database
Phones
Gateways × NPush service
Chat × NRegistryOffline inbox
Ordered logSharded store
Phones + linked devices
Gateways × 334Push serviceMedia + CDN
Chat × NRegistryPresenceOffline inbox
Ordered logSharded store
Was this walkthrough useful?Your vote tells us what to improve next.