File Sync: Scaling It
Minute twenty is when the interviewer starts pricing your limits. They begin with the smallest possible edit to a very large file, and ask what crosses the network. Then they add up every online device asking its question at once. After that, a laptop and a phone both change one document with no connection, and both come back. Last, two strangers upload one video, and the question is how much of it you keep and who may read it.
Every box below answers one of those four, and each comes with a bill you read out.
One Byte in Two Gigabytes
"Somebody fixes a typo in a 2 GB file. How many bytes does that cost you, and are you fine with it?" Today it costs 2 GB, and you are not. So the agent stops treating a file as one thing. It cuts each file into chunks of about 1 MB and names each chunk by a hash of its content, assumed to be 32 bytes. A version becomes an ordered list of hashes.
The upload changes shape. The agent asks which hashes the service lacks. It sends only those straight to the object store, at short-lived addresses the API hands back, each bound to its hash, so the store refuses bytes that do not match. Then it commits the new list, and the API writes no bytes at all. The commit's based-on field waits for the third probe.
POST /chunks/missing {"hashes": ["9f2c", "41ab", "c07e"]}
200 {"missing": {"41ab": "https://store.example/41ab?sig=x"}}
PUT https://store.example/41ab?sig=x the chunk's bytes
POST /files/f-91/commit {"based_on": 7, "list": ["9f2c", "41ab", "c07e"]}
201 {"version": 8, "position": 1042}
Two gigabytes is 2,000 chunks. A one-byte edit changes one of them, so 1 MB goes up. At 10 megabits a second, that is about 0.8 seconds, against 27 minutes. Other devices read the new list, find one hash they lack, and fetch that one chunk.
The list has a size too. The 2 GB file's is 2,000 hashes, 64 KB, so the agent stores it as an object of its own and the row points at it. The mean file's is four hashes, 128 bytes, and it fits in the row.
What it costs: hashing on the device, and a chunk index the first design did not have, one row per chunk in each account, saying that it exists and naming its object. The object store reports each finished upload, and that report writes the row, so a row never claims bytes that have not landed. Four hundred petabytes at 1 MB is 400 billion chunks. Assume 50 bytes a row, and that is 20 terabytes, or 60 with three copies. At 2 TB a node, that is 30 nodes, split by hash. Those disks would be full, so 30 is a floor for now, and the fourth probe gives some of it back.
The object store's limit stays out of this. It takes 3,500 writes a second per key prefix, the start of the key. Chunk keys begin with the hash, so 4,000 writes a second at peak spread over all the prefixes and never come close.
One thing you leave open, and say so: "Call a chunk about a megabyte. Where a chunk ends is a real question, and it gets its own slot later."
Thirty Million Devices Asking
"Thirty million devices, one question every ten seconds. What does your database do all day?" It answers "nothing" 3 million times a second, and has news at best one time in 375. So stop the asking. Each online device holds one long-lived connection to a signal tier, and a commit sends one message through it: this account moved. The signal carries nothing else.
The device then reads the account's journal from its position. The journal is the ordered list of an account's commits. Every commit gets the next number in it, because one owner numbers a scope, the move in Chapter 5's Clocks and Ordering. The owner here is whatever leads the account's partition, and the position from the first design is now that number.
Chapter 18 pushes the message itself down its held connection, because a chat message is small and wanted now. A change here can be gigabytes, so the signal carries nothing and the device pulls what it lacks. The tier itself is the same job. Assume 100,000 connections a server, half the limit Chapter 18 assumes, and 30 million connections is 300 servers. Chapter 18 designs holding them and finding them in full, so you point there and move on.
A lost signal is harmless, because the position is the truth. But a signal can vanish with no later one to expose it, so a device still asks every five minutes. Thirty million over 300 seconds is 100,000 a second, thirty times fewer than 3 million. When a signal server dies, its devices reconnect elsewhere and each one reads from its position on the way in. No commit waits on this tier, so its failure costs freshness and nothing else: at worst, every device is back to its five-minute ask.
The journal is read by account, so the metadata splits by account, into the 75 nodes from Scope. Everything one commit writes belongs to one account: the file's head, meaning its latest version, the new version row, and the journal entry. So all three sit on one partition and commit together.
Count it the way Chapter 3's When One Machine Is Not Enough does. Reads are the 100,000 asks plus at most 8,000 pulls with news. At half of one key-value partition's 3,000 reads a second, that is 72 partitions. Writes are 4,000 commits at three rows each, 12,000 a second. At half of 1,000, that is 24 partitions. So reads set the count at 72, spread across the nodes, and the bytes still set the machines.
Those 75 nodes are full disks, so they are a floor as well. Assume replaced versions and the journal are kept for 30 days. Two hundred million commits a day, at 500 bytes for the version row and 500 for its journal entry, is 200 gigabytes a day, 6 terabytes over the 30 days, and 18 with copies. That is nine more nodes. A device away for more than those 30 days has no position left to read from, so it reads the account's whole file list and compares.
What it costs: a stateful tier in a design that had none, and a change up to five minutes late when a signal is lost. An account also cannot cheaply span partitions, the bill Chapter 5's Transactions Across Partitions sets out.
Two Offline Edits
"Your laptop and your phone both changed that file with no connection, and now they are both back. What does the user end up with?" Both edits. Every commit names the version it was based on, and applies only if that is still the latest. That is a conditional write, which Chapter 5's Leases, Locks and Fencing offers in place of a lock.
Both devices edited from version 7. The phone commits version 8. The laptop's commit says based on 7, the head is 8, and it fails. The laptop keeps its work as a new file beside the original, a conflict copy named for the device and the date, and fetches version 8 under the real name. Both appear everywhere, and nothing was lost. Only a person can decide between them, the case Chapter 4's Concurrent Writes and Conflicts leaves to the user.
One failed check is not always a conflict. A commit's answer can be lost on the way back, and the retry then fails against its own success. So the agent compares first: if the head's list is the list it was committing, there is nothing to keep apart, which also covers two devices making the same edit.
One number is enough here, where Chapter 4's version vectors are not needed, because every commit for an account goes through one place. The trigger for vectors is a second place taking commits.
What it costs: a surprising extra file, a user who has to do the merge, and one linearizable write per commit, which means that once it has answered, every later read sees it. Chapter 4's Consistency Models That Come Up says why that goes to one place, and from a user across the Atlantic from the account's home, that place is about 85 milliseconds away. The other cases can wait, and you park them by name: "Edit against delete has a rule of its own, and I'll take it with the rest in the deep dive."
The Same File Twice
"Two people who have never met upload one 5 GB video. How many copies do you keep, and what can that break?" One, once you widen the chunk index from one account to all of them. A chunk whose hash exists is not uploaded again, and each list refers to it. That is deduplication.
A 5 GB video is 5,000 chunks. The second user sends 5,000 hashes, 160 KB, in place of 5 GB. Assume 30 percent of bytes are duplicates across accounts, and 400 petabytes becomes 280. The index shrinks with it, to 280 billion rows and 14 terabytes, which gives the 30 nodes the room they lacked.
A hash alone must not unlock bytes, or anyone who learns a hash can fetch the file. So the client proves it holds the chunk, by uploading it or by answering a challenge on a random range: the service names a stretch of the chunk, and the client returns a hash of those bytes. A read address goes only to an account whose own list names that chunk, so the commit first checks each hash new to that list, and refuses one this account has not proved. That is about one index read per changed chunk, about 4,000 a second at peak.
What it costs: a reference count on each chunk row and a sweep for those that reach zero. The count is how many kept versions name that chunk, and a row starts at zero. A commit cannot move those counts in its own transaction, because chunk rows are split by hash and sit on other partitions, so a reader of the journal moves them afterwards: up when a version commits, and down only when a replaced version's 30 days end.
That reader can crash and repeat a batch, and subtracting one is not idempotent, the warning in Chapter 6's Delivery Semantics at Scale. A repeated rise only keeps a chunk too long. A repeated fall can free a chunk another account still reads. So each fall writes a dedup record on the chunk's own partition, named by the account and journal position, in the same transaction, and a second copy finds it and stops. The bill is one short row per fall, kept for an assumed week: at most 16,000 falls a second at peak and 8,000 on average, which at 50 bytes a row is under half a node with copies. A fast upload also tells an attacker that a file already exists.
And it costs all of this if the user holds the keys, since two users encrypting under their own keys produce different bytes and no hash ever matches. So the trigger back to one account is a promise that the service cannot read the files.
The Design After Four Probes
A device agent watches the folder, cuts files into chunks and keeps a local list of hashes. Its calls go through the load balancer to the API, which asks the chunk index what is missing and commits to the metadata store. That store is split by account and holds the file rows, the version rows and the journal. The chunk index is split by hash. Chunks go straight between devices and the object store, which reports each upload to the index, and the API carries none of them. A commit's signal reaches the signal tier, which holds one connection per device, and a device that hears it reads the journal from its position and fetches what it lacks.
- Putting file contents in the signal. It then has to be reliable and ordered, and the journal already is.
- Answering the conflict probe with a vector clock on every file, when one place orders the commits. The interviewer asks what the second counter is for.
- Treating deduplication as free. The interviewer asks what stops a client that only knows a hash.
- Saying "split the metadata" without the key. By file breaks the journal, which is read by account.
- Each change priced before and after: 27 minutes against 0.8 seconds, 3 million against 100,000.
- What happens when a signal is lost, said before the interviewer asks.
- The failed commit walked step by step, along with what the losing device does next.
Knowledge Check
Chunking brings in a chunk index. What does that index cost at 1 MB chunks, before any deduplication?
- One more row per file, 50 TB on the same 75 metadata nodes
- About 400 PB of new storage, a copy of every chunk it knows
- About 20 TB of rows, which comes to 30 nodes with three copies
- Nothing, because a chunk's name can be checked in the store
A signal to a connected device is lost. What does that cost the user?
- The change itself, unless the commit is retried until it lands
- A change up to five minutes late, since the position is the truth
- Nothing, as the tier keeps each signal until it has been read
- A full rescan of the account, as the device has lost its place
Why is one version number enough here, when Chapter 4 reaches for version vectors?
- Two devices rarely edit one file offline, so the odd loss is fine
- Device clocks are close enough to order whatever is left over
- Vectors need text, and most files in a sync folder are not text
- Every commit for an account goes through one place, in one order
What does deduplicating across accounts give up?
- Any promise not to read the files, and secrecy about which files exist
- The isolation of accounts, as two users can read each other's folders
- Strong consistency on the head, as a shared chunk is written twice
- Sync of small edits to large files, as shared chunks cannot change
You got correct