Bugpit

Lessons › Big architectures

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 level

Part 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.

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?

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?

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?

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.

Try it yourself

More in Big architectures

← Video platform: upload to play Ticket sale: one show, everyone at once →