Lesson 1 · Minutes 0-10

Chat: Scope

Problem

Two different crowds size this system, and the question mentions neither: the people connected at this moment, and the messages they send. Minutes zero to ten pull those apart, then find out how many devices one person brings, because that answer reaches further than it sounds. Six questions do it. After them you agree the terms, do the sums, and settle two reads before anything is drawn.

Six Questions Before Any Box

Every one of these changes the board, and the answer given with it is the one this lesson agrees.

  1. How many are online at the same moment, and how many messages go through in a day? Online users set the connection tier, and messages set the store. Agreed: 40 million daily users, 10 million online at peak, 30 messages a user a day.
  2. How large can a group get? The cap sets every per-member cost. Agreed: 1,000 members, with anything much larger left for a curveball. A message reaches about three members on average, and the sender counts as one, because the sender's other device needs it too.
  3. Are several devices online at once, and do they all see everything? The answer means routing to connections, not users, and a position per device. Agreed: assume two a user, a phone and a laptop, both seeing the full history.
  4. What does real time promise: how fast, and in what order? The answer sets what polling can and cannot do. Agreed: under a second when both ends are online, and one order per conversation on every device.
  5. How long is history kept, and on whose machines? On the server, it is the source for a new device. On devices alone, it is a far smaller store and a different product. Agreed: on the server, for three years.
  6. Are receipts and presence in? Each is its own traffic. Agreed: sent and delivered ticks are in, presence is parked, and end-to-end encryption is off until someone asks.

Candidates skip the third one, and it is cheap only now. One way to put it: "Is one person one connection, or are a phone and a laptop online together? That changes what I route to and what I keep track of."

The Agreement and What It Leaves Out

Five things are functional. Send a message to a conversation. Receive it live on every connected device. Read a conversation's history. List a user's conversations, latest first. Create a group and change its members.

Four are non-functional. Delivery takes under a second when both ends are online. An acknowledged message is never lost. Every device shows one order for a conversation, with a reply below what it answers. A device that was away catches up completely.

Rule these out in one breath, while objecting is still cheap: voice and video, media bytes, search across messages, spam and abuse. A photo is a link in a message, with the file in an object store.

Two Crowds, Two Sums

Start with the messages. Forty million users at 30 a day is 1.2 billion a day. Divide by about 100,000 seconds in a day, and you get 12,000 a second. Assume a peak of twice that, 24,000.

Each one reaches about three members, so that is 72,000 deliveries a second at peak. The routing, whatever it turns out to be, gets built for this number.

Now the other crowd. Ten million online at two devices each is 20 million devices. If each of them ever holds a connection open, assume one server can keep 200,000, and run it at half. That is 100,000 each, so 200 of them, and from here on a server that holds connections is a gateway. Little else in the design is sized by users online, and nothing else sizes this tier.

Storage is the sum that ends the one-machine idea. Assume 300 bytes a message, and 1.2 billion a day is 360 GB a day. A year of that is about 131 TB, and three years about 394 TB. Three copies make about 1.2 PB. Assume 2 TB a node, which makes about 600 nodes. One node's 2 TB would fill in under a week, so this store outgrows a machine in days.

Bandwidth takes one sum and then goes. Seventy-two thousand deliveries, at two connections and 300 bytes, is about 43 MB a second across 200 gateways. It decides nothing, and that sum is your reason for skipping it.

The estimates, and what each one decides
Writes40 million users, 30 messages a day
About 12,000 a second, 24,000 at peak. A send's own cost is small.
Readsa read here is a delivery, to 3 members
About 36,000 a second, 72,000 at peak. The routing is built for this.
Storage3 years, 300 bytes, 3 copies
About 1.2 PB. At an assumed 2 TB a node, about 600 storage nodes.
What sizes the gateways, which no message count can show
Onlineat peak
10 million users, at an assumed 2 devices each.
Connectionsheld open
20 million, whether or not anyone is typing.
Gatewaysassume 200,000 a server, run at half
100,000 connections each, so 200.

API and Entities

Leave sending and live delivery open on purpose. First Design has to draw them, and nothing has forced a choice yet. Two reads can be settled now. A GET on conversations returns a user's list. A GET on one conversation's messages, with an "after" position, returns history from that number. A POST on conversations makes a group, and a call on its members changes who is in it.

What can be settled now
GET  /conversations                           a user's list, latest first
GET  /conversations/{id}/messages?after=<n>   history from a position
POST /conversations                           make a group
POST /conversations/{id}/members              add or remove a member

Three entities carry the design. A message has its conversation ID, a sequence number, the sender, a message ID the client makes itself, a body and a time. A conversation has an ID and its members. A member's row has the user, the conversation, the last number and the last time.

The key is already chosen. Chapter 3's Picking the Partition Key worked this case, and its answer holds here: messages keyed by conversation ID, with the number as the sort key.

Interview Pitfalls
  • Saying WebSockets in minute one. Nothing yet says why, and it costs you the question that decides it.
  • Sizing only messages a second, when the connection count sizes the tier everyone is asking about.
  • Leaving devices out, so that "the user" turns into two connections and two positions at minute twenty-seven.
What Interviewers Listen For
  • Users online kept apart from messages a second, each said to size a different tier.
  • 1.2 billion a day turned into a store that outgrows a node in days.
  • The question about devices asked before anything is drawn.

Knowledge Check

Only one clarifying question fits before the interviewer wants a drawing. Which answer moves this design the most?

  • How long a message body can get before it has to become a link to a file
  • Whether read receipts are wanted as well as sent and delivered ticks
  • How many people are online at once, and how many devices each one brings
  • Which regions the first users are in, and which of them must launch first

The sums come out at 200 gateways. What number sizes that tier?

  • Peak deliveries: 72,000 a second, which is 360 on each gateway
  • Open connections: 20 million, at 100,000 held on each gateway
  • Peak sends: 24,000 a second, which comes to 120 on each gateway
  • Peak bandwidth: about 43 MB a second, or 216 KB on each gateway

Storage comes to about 1.2 PB over three years. What does that decide?

  • History moves to the devices, as the servers cannot hold three years of it
  • Gateways get local disks, so a message survives a dropped connection
  • Retention drops to one year, as three years is too much for any one region
  • Messages go in a partitioned store, as one node's disk fills in under a week

Which of these can you rule out of scope out loud with the least risk?

  • Search across every message in a user's three years of history
  • Reading a conversation's history on a device added only today
  • Showing one order per conversation on each of a user's devices
  • The delivered tick that tells a sender a message reached someone

You got correct