Medium · 12 steps · ~75 min
Design a chat app
Messages delivered in order, with ticks, to phones that are often offline.
Chat needs open connections for instant delivery, but a phone may be offline, so messages must be stored until delivered.
Same problem as WhatsAppTelegramSignal
How it works
- Send: the message reaches a gateway holding the connection.
- Deliver: the recipient's gateway pushes it, or it waits in storage.
What you'll build
- The API and the data behind it
- A gateway and a database for 500 requests a second
- A queue, workers and a cache for 15,000, then 50,000
- A rate limiter that stops a spam flood
- A design that survives a cold cache, a lost gateway, a lost worker and a lost primary
Steps
- 1Design the API
- 2Design the data model
- 3Handle 500 messages/sec
- 4Handle 15,000 requests/sec for $1,400 a month
- 5Handle 50,000 requests/sec
- 6Survive a spam flood on a budget
- 7Survive a cache restart
- 8Lose a realtime gateway
- 9Lose a worker
- 10Lose the primary database
- 11Messages arrive twice and out of order
- 12Deep dive: pushing to an offline phone