Lesson 2 · Minutes 10-20

File Sync: First Design

Problem

Picture the crudest program that could be called sync: it notices a changed file and sends all of it to a server. From minute ten to minute twenty, that program is your design. Nothing is chunked and nothing is pushed, and at small scale it keeps its promises.

Trace one edit from a laptop to a phone, grant what this design does well, and then give the interviewer its price before they ask. Their four pushes are the quiz at the end. Settle your own answer to each before the options can steer you.

An Agent, a Store and a Table

An agent on each device watches a folder. When a file changes, the agent uploads the whole file to a stateless API tier behind a load balancer. The API puts the bytes into an object store under a new key. Then it adds a version row to one relational metadata database, and moves the file's latest version to that row.

Other devices ask the API every ten seconds for changes since the position they last saw. A position is an increasing number the database gives each new version. Each change means a download of the whole file, which the API answers by redirecting the device to the object store.

The first design: whole files, one database, a poll
Second devicepolls every 10 s Devicewatches a folder Object storeevery version, whole Load balancerany server API serversstateless Metadataone database whole file changes since n whole file new version every 10 s readwrite

One Edit, Laptop to Phone

Someone saves a 4 MB document on the laptop. The agent sees the change and sends a PUT with the whole file. The load balancer picks any API server. That server writes the bytes to the object store under a new key and waits for the store to confirm. Only then does it add version 8 at position 1,042, move the file's latest version to 8, and answer. The order is the point: the bytes first, then the row. The other way round, a phone could be told about a file that is not there.

The phone's next poll, at most ten seconds later, asks for changes since 1,041. The database returns one row: this file, version 8. The phone sends a GET for that version, follows the redirect and downloads all 4 MB. It replaces its copy and remembers 1,042. A phone that was off for a week sends the same question with an older number, and gets a longer list.

What It Already Does

A file is on every online device within about ten seconds. It survives the loss of any device, because the object store holds it and not the device. Every old version is still there. For a few thousand users whose files are a few megabytes, nothing here needs changing: assume 10 megabits a second up, and a 4 MB file takes about 3 seconds. Grant all of that out loud, because the interviewer is about to take it apart.

Four Limits, Named and Not Fixed

A change to any part of a file resends all of it. Every device asks the same question every ten seconds, and nearly every answer is nothing. Two offline edits end with the later upload as latest, and nobody is told, which breaks what Scope agreed. Identical files are stored as many times as they are uploaded. You leave all four standing for now.

The first one you offer yourself, in about ten seconds: "Whole-file upload is fine at a few megabytes, where it takes seconds. A one-byte edit to a 2-gigabyte file is 27 minutes at 10 megabits up, and I'd see upload bytes run far ahead of the bytes that actually changed. That path is my first change."

Interview Pitfalls
  • Opening with chunks and a sync protocol before one whole file has gone end to end. The interviewer cannot yet tell what the chunks are for, and they push you to justify each one.
  • Writing the metadata row before the object store has the bytes, so another device can be told about a file that is not there.
  • Letting last-upload-wins stand without saying so, when Scope has agreed nothing is lost silently. Name it as a limit and move on; it is the third push.
  • Having no position in the change query, so a device that was offline for a week has no way to catch up.
What Interviewers Listen For
  • A change traced from the watching agent to the second device.
  • The order of the two writes stated: the bytes first, then the row.
  • Twenty-seven minutes for one byte, offered before the interviewer gets there, and where it would show on a graph.

Knowledge Check

"Someone changes one byte in a 2 GB file. How many bytes does your design send, and are you happy with that?"

  • "Cut files into chunks named by their hash, and upload only the chunks that changed."
  • "Compress the whole file before upload, so a 2 GB file costs far fewer bytes to send."
  • "Send a binary diff against the last version, and apply it on the server afterwards."
  • "Hold the edits back for an hour, so that many small edits share one full upload."

"Every device polls every ten seconds, and thirty million of them are online. What does your metadata database see?"

  • "Stretch the polling interval to five minutes, which cuts that load thirty times."
  • "Put a cache in front of the change query, and let the polls read cached answers."
  • "Hold a connection per device, signal a change, and let it pull past its position."
  • "Partition the metadata until the polling fits, and keep the ten-second interval as it is."

"A laptop and a phone both edit the same file offline, then both come back. Whose edit wins?"

  • "The one with the later timestamp, since that edit is the more recent of the two."
  • "The first to reach the server, and the second device replaces its copy with that."
  • "Merge both versions automatically, the way a source control tool merges its text."
  • "Neither one: the second commit fails a version check, and both survive as files."

"Two users upload the same 5 GB video. How many copies do you store, and what could go wrong?"

  • "Two, because each account's data has to stay apart to keep the accounts isolated."
  • "One, found by chunk hash, once the client has proved it holds those very bytes."
  • "One, found by matching the file's name and size, which is quick to check each time."
  • "One, found by a nightly job that scans all stored files and merges any duplicates."

You got correct