Chat app: 6 million online
Your chat app has 6 million people online at the peak, sending 6,000 req/s. 12% are new messages, 720 a second, and a tenth are photos. People who aren't in the app need a push notification. Today it's one gateway, one database and no way to reach anyone offline.
Play this levelPart of Bugpit Pro. It runs in your browser: build the design, press Run, and watch what breaks.
The lesson
Chat has three jobs. Keep a connection open to everyone online. Save every message. And reach people who aren't looking, with a push notification.
Connections are the first limit. A Realtime Gateway tops out at about 3.6 million, and only lets in 500 new people a second. Connections drop and come back all day, so a full gateway struggles to let people back in. Spread 6 million over three gateways, not two.
Messages never stop arriving, and your primary takes about 600 writes a second. Each chat is fetched by its own ID and never joined, which is a NoSQL job: it spreads writes across partitions by itself, so it grows with you. Pick a key that spreads them out, like the chat ID.
The person sending a message shouldn't wait for a push to go out. Put it in a queue, and let workers save it and call the push provider. Photos go to object storage behind a CDN, like any other file.
What you have to achieve
6,000 requests a second at the busiest hour, 12% of them writes and 10% static files.
- No broken or illegal connections
- Hold 6M open connections
- Use a NoSQL DB
- Use Email & Push
- Use an Object Storage
- No component is overloaded
- Workers clear the queue within the day
- Fewer than 0.1% of requests fail
- p99 response time under 300 ms
What you learn
Look at Monthly cost: much of it pays for connections that sit quiet. Messages, photos and pushes each took their own path, and the sender never waits for any of them. That's the shape behind most chat apps: a live connection in front, a write-friendly store behind, and slow work done later.
Check yourself
6 million people are online. Why run three gateways and not two?
- Three is cheaper
- A full gateway struggles to let people back in after they drop
- Each gateway only serves one country
Show the answer
A full gateway struggles to let people back in after they drop. Connections drop and come back all day, and each gateway only lets 500 new people in a second. Leave room.
Why are messages saved by a worker reading a queue?
- So the sender gets an answer without waiting for the save and the push
- Workers write faster than app servers
- Queues store messages forever
Show the answer
So the sender gets an answer without waiting for the save and the push. The sender only waits for the message to be queued. Saving it and waking the other person happen just after.
Which key spreads chat messages evenly across NoSQL partitions?
- The day it was sent
- The country of the sender
- The chat ID
Show the answer
The chat ID. There are millions of chats, so writes spread out. A day or a country would pile everyone onto a few partitions.