Chat: Scaling It
Between minute twenty and minute thirty, nothing about the numbered rows changes, and everything around them does. The clock goes first: the interviewer counts your polls and asks where else that money could go. With sockets in place, they put a sender and a recipient on different servers and ask how a message crosses. Next, two people in a group send together, and their phones list the two messages in opposite orders. Last, a phone comes back from a whole day dark, and its owner's laptop wakes up beside it.
Each of those adds to the board, and nothing gets added without one of them. The board at the end shows what moved.
Polling at Ten Million
"Three million requests a second to tell people nothing. What would you rather pay for?" Pay for messages. Each device holds one WebSocket, a connection that stays open and carries data both ways, to a gateway, and the server pushes when something arrives. The 3.3 million empty requests a second become at most 144,000 pushes at peak: 72,000 deliveries, at two connections each. At most, because only a quarter of daily users are online at peak, and the rest have no connection to push to.
That takes the 200 gateways from Scope, each with 100,000 connections and no business logic, so they rarely need a deploy. A gateway keeps its open sockets and passes sends along. It keeps no messages and no positions, so a restart costs connections and never data.
This is the exception Chapter 2's Stateless Services, Many Copies named, a tier that is stateful by design. The load balancer places a connection once, and that server has it until it drops. Assume a heartbeat every 30 seconds to tell live from dead, about 3,300 small frames a second on each gateway, and none of them touches a store.
What it costs: a tier you cannot restart casually, since a deploy drains 100,000 clients, and a new question, which server holds the recipient. History reads stay plain HTTP on the app servers, so the first design's path survives for them.
Which Gateway
"She's on gateway twelve, he's on a hundred and forty. What carries the message over?" A connection registry, kept in memory, says which gateway holds each device. A stateless message service takes each send, reads the members from the conversation's row, which it caches, looks up each member's devices, and calls those gateways.
Size it by operations. Deliveries are 72,000 lookups a second at peak. Assume gateways also renew 20 million entries every five minutes, about 67,000 a second. That is about 140,000 operations a second. At half of one cache node's 100,000, it takes 3 nodes. At an assumed 100 bytes an entry the whole registry is 2 GB, so memory decides nothing.
An entry has a TTL, an expiry, so a stale one dies by itself. A push to a gateway that lost the device simply fails, and the device counts as offline. That is safe because of the next two probes. Lose a registry node and its devices look offline until their next renewal, at most five minutes, and their messages sit in the history meanwhile.
A member with nothing connected gets a push notification from Chapter 15's Notification System, a black box here. Three members in four are away at peak, so one alert per message would be up to 54,000 a second. Send one per conversation every few minutes instead, with the time kept on the member's row. A phone that is away needs one nudge, because the history holds the rest. What it costs: a second message inside those minutes brings no alert of its own. If the product wants one per message in one-to-one chats, price that share first.
What it costs: a lookup per delivery, and a registry that may be wrong for a moment.
One Order for Everyone
"They both sent at once, and the phones disagree. Who is right?" Neither phone, and neither clock, as Chapter 5's Clocks and Ordering says. Give each conversation one owner, a server chosen by hashing the conversation ID. The message service hands it each send, it stamps the next number, and every phone sorts by that number.
A reply is sent after its parent has arrived, so its number is higher. That is the causal promise of Chapter 4's Consistency Models That Come Up, kept inside one conversation.
The owner writes one conversation's messages in number order, so a number below the highest always exists. Each write is conditional on its number being unused. An old owner that wakes from a pause and tries a taken number fails and stops, the fencing idea of Chapter 5's Leases, Locks and Fencing. If the number happens to be free, its message is simply next, and nothing is wrong. A new owner that loses that race to its predecessor's last write reads the newest rows again, client-made IDs included. If the message is already there, it answers with that number, and if not, it takes the next.
The one table becomes a partitioned store, keyed by conversation ID then number, as Chapter 3's Picking the Partition Key chose. A month joins the key, so a long conversation stops growing one partition, and the conversation's row notes the number each month starts at, so a read knows which partition to open. The bytes set the machines: 394 TB at an assumed 2 TB is about 200 sets of three copies, Scope's 600 nodes, on full disks, so that is a floor. Writes press on nothing: 24,000 at peak over 200 sets is 120 each, against one key-value partition's limit of 1,000.
A number also finds a gap, the use Chapter 6's Delivery Semantics at Scale gives it. A phone holding 12 that receives 14 keeps 14 back, reads 13 from the store, and shows all three in order. It drops any number it already has. So a lost push costs one read, and never a message. A gap needs a later number to show itself, and the last message before a quiet spell leaves none. So a device also runs the list read whenever its app comes to the front, and a gateway whose renewal finds an entry gone tells that device to do the same. A retried send carries its client-made ID and gets its first number back, and the deep dive shows how.
This is the moment to say what the push is worth: "A push can fail and nothing is lost. The registry is a hint, and the numbered history is the truth."
What it costs: order only inside a conversation, every send through an owner, one write at a time inside a conversation, and a short pause when an owner fails. The coordination service of Chapter 5's Leader Election and Consensus holds the owners' list and their leases, beside the path, and the send path never calls it. When a lease lapses, the hash moves that server's conversations to the others. Each new owner reads the highest number before stamping anything. Assume a 10-second lease, and those conversations wait up to ten seconds, once.
A Day Away
"The phone's been dark since yesterday. How much work is it to catch up, and for whom?" Little, and it falls on the phone. Each device keeps its last number per conversation. It reads the member table, keyed by user, one small row per conversation with the last number and time, the table Chapter 3's Picking the Partition Key adds for this query. It compares, and reads on from its own position in the conversations that moved.
An average member takes in 90 messages a day, the 30 they sent themselves among them. Assume they fall in ten conversations. That is one list read and about ten range reads.
The laptop does the same with its own numbers. A new device starts from zero, taking a page below each last number and reading further back as its owner scrolls. The server holds no per-device queue, for a phone or for anything else.
What it costs: each send updates every member's row, a timestamp and a number, and never a copy of the message. The sender's row moves too, which is how the sender's laptop learns of it. That is 72,000 row writes a second at peak. At one key-value partition's 1,000 writes a second, that is 72 partitions, and 144 if you assume each runs at half its limit. The rows are small, so writes set that count and bytes do not.
A delivered tick is a second number on that same row, the highest the member's devices have taken in. Devices report it in batches every few seconds, so a busy conversation writes one mark per member per batch, not one per message. A quiet conversation gains nothing from the batch, so that adds up to 12,000 writes a second at peak: the two members who did not send, at the one in four who is online. The sender's own mark rides on the write its row already takes. Eighty-four thousand writes at half of 1,000 is 168 partitions, not 144.
The Board at Minute Thirty
Put it together. A device opens one socket through the load balancer to a gateway. A send goes from the gateway to the message service, to the conversation's owner, and into the partitioned store, and the answer carries its number back. The message service updates every member's row, finds each device in the registry, and pushes to its gateway without waiting. A member with nothing connected goes to the notification system.
A device coming back, or scrolling, takes the plain HTTP path: load balancer, app servers, its rows in the member table, then the store from its own number. Polling is gone, and the one table is now a partitioned store. The same two reads from Scope do all of the catching up. Beside the path, a conversation table holds each conversation's members and month map, which the message service and the app servers read and cache, and the coordination service holds the owners' leases. One gap stays open on this board: a crash after the write and before the push. The next lesson closes it with one more box.
- Giving the gateways business logic, so that every deploy kicks 100,000 clients.
- Treating the registry as truth, when it is a hint and the numbered history is the truth.
- Copying the message into a table per user, fan-out on write with a three-year bill: about 3.5 PB against 1.2. Chapter 17's News Feed copies IDs for a different problem.
- The real-time path said to be allowed to lose a push, with the reason that is safe.
- The owner offered together with its cost: order inside a conversation, and nothing else.
- One mechanism serving a phone back from a day away and a brand-new laptop.
Knowledge Check
A phone comes back after a day away. What does it read to catch up, and what pays for that being so cheap?
- A per-device queue drained on reconnect, paid for by a copy of each message per device
- A list read and about ten range reads, paid for by a row write per member per send
- The last day of each conversation, paid for by range reads on chats that did not move
- Every gateway's recent messages, paid for by keeping a day of history in their memory
A registry entry is stale and names a gateway that has lost the device. What does that cost?
- The send fails, and the sender's phone shows the message as not delivered
- The message is lost, unless the sender retries it under the same client ID
- The message waits in a queue on that gateway until its device reconnects
- One failed push, and the device counts as offline until it catches up by number
A phone holds 12, and a push for 14 arrives. What does that tell it, and what does it do?
- A push for 13 went missing, so it reads 13 from the store and shows them in order
- Number 13 was never sent, so it shows 14 at once and carries on from that
- The sender's phone is behind, so it asks the owner to have that phone send again
- Its order is now wrong, so it drops its copy of the conversation and reads it all
An old owner wakes from a long pause and writes number 40, which the new owner has already used. What stops it?
- The coordination service sees it wake, and cancels the writes it has in flight
- The store compares clocks, and refuses any write stamped older than its newest
- The store applies a write only if its number is unused, so this write fails
- Each phone notices two messages under one number, and hides the later one
You got correct