System Design · Practice problem · Medium
Deliver Chat Messages to Whoever Is Online
Hold each user's WebSocket on one connection server, write down which server holds it in a shared store, and route each message there. Save every message before pushing it, so someone offline can read it later.
Medium means two needs that pull against each other. You solve it on a canvas that runs your design. The canvas needs JavaScript; everything on this page works without it.
What is the situation?
You run a chat app with 5,000 users, and each message goes to one person. Over the first 25 seconds of the evening people open the app at 200 a second. Some open it twice, so about 3,200 end up online and everyone else stays offline. Each phone that is online holds one live connection to one Connection Server. A Connection Server has room for 2,000 connections and does 200 pieces of work a second, where opening a connection and pushing a message each count as one piece. People send 200 messages a second through POST /messages, to anyone, online or not. Design the path from POST /messages to whichever server holds the recipient's connection, so senders are answered quickly, everyone online gets their messages quickly, and anyone offline finds their messages saved in your Messages database. A phone whose connect is refused tries again about a second later. A phone that loses its connection reconnects by itself, and you choose how many seconds the clients of a dead server spread their reconnects over. Nothing here refreshes a record after it is written, because phones send no heartbeats, so a store that forgets an entry early has forgotten where that person is.
What must your design do?
- Senders are answered within 100 ms at the 99th percentile, and at least 99.9% of messages are accepted, even while a Connection Server is down.
- Every message is saved in Messages, so someone offline loses nothing.
- Messages for someone online reach them. At least 99% must get there, and at most 20 in any test may go astray.
- A message reaches someone online within 200 ms at the 99th percentile, including while other clients are reconnecting.
- The system holds at least 2,700 connections at once.
- After a Connection Server dies, the system is back to 99% of the connections it held within 30 seconds.
- The system costs $650 a month or less.
What does Run test?
Run works without an account. It checks your design against 2 tests you can read before you start:
- An ordinary evening. People connected at 200 a second for the first 25 seconds, and 200 messages a second went out for 55 seconds.
- A lively ten seconds. At second 35, messages doubled to 400 a second for 10 seconds, and another 100 people a second connected for those 10 seconds.
What does Submit add?
Submit needs an account. It runs 2 more tests you cannot read in advance, and stops at the first one your design fails, showing what happened and why. Passing every test, or giving up, unlocks the written solution.
Which parts can you use?
Already on your canvas: Messages.
- Load Balancer: about 50,003 a second, $18 a month each
- App Server: about 2,000 a second, $45 a month each
- Connection Server: about 200 a second, $60 a month each
- Counter Store: about 100,003 a second, $30 a month each
- Cache: about 100,003 a second, $30 a month each
- Database: about 5,000 a second, $120 a month each
- Queue: effectively unlimited, $25 a month each
- Worker: about 1,000 a second, $45 a month each
- Waiting Room: effectively unlimited, $25 a month each
Every connection takes 1 ms, and the budget is $650 a month.
What does this problem leave out?
- A phone going offline by choice, presence and typing indicators.
- Heartbeats, and anything that rebuilds a directory after it loses its entries.
- How a phone catches up on saved messages when it comes back. We grade that each message is saved, not that read.
- Moving clients back to a server that has returned. No test brings one server back while the others stay up.
- Publishing each message to every server. The canvas has no way to draw it.
- Group chats, message order and read receipts: each message here goes to one person.
Where does this problem fit?
Its category is Real-time messaging: Push to connected users. It practises partitioning, real-time delivery and reliability & ops.
For the whole system, read the interview breakdown Design a Chat System (WhatsApp, Messenger).
By GOAT Engineer. Explore System Design · Privacy Policy · Contact.