Lesson 1 · Minutes 0-10

File Sync: Scope

Problem

The largest number in this problem is 400 petabytes, and it decides almost nothing. What shapes the design is how often idle devices ask whether anything has changed. You have until minute ten to get to that sum, and to ask the one question that makes this a conflict problem.

Six Questions and What They Move

Ask these six and keep the agreed answers, because every later sum starts from them.

  1. How many users, how many devices each, and how much data? The answer sets the bytes held and the load from devices. Agreed: 20 million users, 3 devices each, 20 GB and 5,000 files per user.
  2. How big do files get, and how often do they change? Large files that change a little make whole-file upload wrong. Agreed: a mean of 4 MB, some of many gigabytes, and ten changed files per user a day.
  3. How soon must a change reach the other devices? Minutes allow slow polling, and seconds do not. Agreed: seconds, for a device that is online.
  4. Can a device edit offline, and may an edit ever be lost? A yes and a no make this a conflict problem. Agreed: yes, and nothing is lost silently, though the user may be asked.
  5. Do we hold the keys to the files? Keys held by the user rule out finding duplicates across accounts. Agreed: the service holds them.
  6. Do users share folders? Sharing moves the partition key. Agreed: one user for now, and a shared folder waits for a curveball.

The fourth is the one that shows you see the hard part, so put it plainly: "Can a device edit while offline, and is losing an edit ever acceptable? If the answers are yes and no, this is a conflict problem as much as a storage one."

What You Commit To

Three things are functional: add, change, move and delete files on a device; have that appear on the user's other devices; read an earlier version. Three are non-functional. A confirmed change survives the loss of any device or machine. A change reaches online devices in seconds. No edit is lost without saying so.

Then rule out, by name, search inside files, previews, billing and storage plans, and real-time co-editing, which you park as a curveball. If the interviewer wants one of them back, you hear about it now.

The Sums, and the One That Decides

Start with the bytes, and do not stay long. Twenty million users at 20 GB each is 400 petabytes. That goes in an object store and is not a question of nodes.

Metadata is next. Five thousand files a user is 100 billion files. Assume 500 bytes a row, and that is 50 terabytes, or 150 with three copies. With 2 TB assumed for a node, it takes 75 nodes. It is also 8,000 times smaller than the bytes, which is why the two live apart.

Now the changes. Ten changed files a day for 20 million users is 200 million a day. Spread over about 100,000 seconds, that comes to 2,000 a second, and twice the average at peak makes 4,000.

The polls are the number that decides. Three devices each is 60 million devices. Assume half are online at the peak, so 30 million. A poll every ten seconds is then 3 million requests a second. Each change reaches two other devices at most, so at peak at most 8,000 of those polls have news. That is one in 375 at best, and the design has to answer for the rest.

Bandwidth gets one pass, once only changes travel. Assume a change adds 1 MB of new bytes. Four thousand a second is 4 gigabytes a second in, and at most twice that out, about 64 gigabits a second, under one 100-gigabit link. It decides nothing. Neither does growth: 200 terabytes a day, at most 73 petabytes a year on 400. Say so, and move on.

The estimates, and what each one decides
Changes10 files a day, 20 million users
2,000 a second, 4,000 at peak.
Deliveries2 other devices a change
At most 4,000 a second, 8,000 at peak.
Growth1 MB of new bytes a change, a year
At most 73 PB a year. Decides nothing.
Bandwidth1 MB of new bytes a change
2 GB a second in, 4 GB at peak; 4 GB out, 8 GB at peak. One 100 Gbps link. Decides nothing.
What the estimator cannot show
Polls30 million online, every 10 s
3 million requests a second, at best one in 375 with news. This decides the design.
Metadata100 billion files, 500 bytes, 3 copies
50 TB, 150 TB with copies. At 2 TB a node, 75 nodes. 8,000 times smaller than the bytes.

Three Calls, Two Entities

A PUT on a file stores a new version, and carries the bytes. A move or a delete is the same call with a new path or a delete mark, and no bytes. A GET on a file returns a version's bytes. A GET on changes, with a position, returns what has changed for this account after that position.

The three calls
PUT /files/f-91            {"path": "/docs/plan.doc"} + the bytes
                           201 {"version": 8, "position": 1042}

GET /files/f-91?version=8  200 the bytes

GET /changes?since=1041    200 {"position": 1042,
                                "changes": [{"file": "f-91", "version": 8}]}

There are two entities. A file has a stable ID, an owner, a path and its latest version. A version has the file's ID, a number, a size, an object key, the device and the time. The ID stays the same when a file moves. Say that now, because the conflict section relies on it.

Interview Pitfalls
  • Spending the minutes on 400 PB and never reaching the polls. The interviewer watches the clock go on a number that decides nothing.
  • Leaving the offline question unasked, then meeting it at minute 25 with no version on any write.
  • Treating the path as the file's identity. A move then looks like a delete and an add, and the conflict probe finds it.
What Interviewers Listen For
  • Device counts turned into 3 million polls a second, with how few of them carry news.
  • 400 PB of bytes separated from 50 TB of rows, with a note that the two want different homes.
  • Whether a loss is ever acceptable, asked outright, which shows the candidate sees the requirement behind the hardest part.

Knowledge Check

You have time for one more question before you draw. Which answer changes this design the most?

  • Which file types the service should be able to preview
  • Which storage plans the company will sell to its users
  • Whether a device can edit offline and an edit may be lost
  • How long the service should keep a user's deleted files

Thirty million online devices polling every ten seconds make 3 million requests a second. What does that number tell you?

  • Almost all the load is devices hearing that nothing changed
  • The metadata has to take 3 million new rows each second
  • The object store is about to serve 3 million downloads
  • Ten seconds is too quick an interval to promise anyone

Which pair of numbers says that metadata and bytes belong in different homes?

  • 4,000 changes a second against 8,000 fetches a second
  • 20 GB per user against the mean file's size of 4 MB
  • 60 million devices against the 30 million that are online
  • 400 PB of bytes against 50 TB of rows, 8,000 times smaller

Which of these is safest to rule out of scope out loud in the first ten minutes?

  • Reading an earlier version of a file after it changes
  • Searching the text inside the files a user has stored
  • Telling a user when two offline edits have collided
  • Getting a change to online devices within a few seconds

You got correct